blob: 9b8253d50ae3f133fea024ad5f6babc63101553a [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
26
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040028#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080037#include "shell.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020039#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020040#include "../shared/config-parser.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020041#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Jonas Ådahl04769742012-06-13 00:01:24 +020046struct focus_state {
47 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040048 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020049 struct weston_surface *keyboard_focus;
50 struct wl_list link;
51 struct wl_listener seat_destroy_listener;
52 struct wl_listener surface_destroy_listener;
53};
54
Xiong Zhang6b481422013-10-23 13:58:32 +080055struct shell_output {
56 struct desktop_shell *shell;
57 struct weston_output *output;
58 struct wl_listener destroy_listener;
59 struct wl_list link;
60};
61
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050062enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020063 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050064 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030065 SHELL_SURFACE_POPUP,
66 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020067};
68
Scott Moreauff1db4a2012-04-17 19:06:18 -060069struct ping_timer {
70 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -060071 uint32_t serial;
72};
73
Philip Withnall648a4dd2013-11-25 18:01:44 +000074/*
75 * Surface stacking and ordering.
76 *
77 * This is handled using several linked lists of surfaces, organised into
78 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
79 * above all of the surfaces in the layer below. The set of layers is static and
80 * in the following order (top-most first):
81 * • Lock layer (only ever displayed on its own)
82 * • Cursor layer
83 * • Fullscreen layer
84 * • Panel layer
85 * • Input panel layer
86 * • Workspace layers
87 * • Background layer
88 *
89 * The list of layers may be manipulated to remove whole layers of surfaces from
90 * display. For example, when locking the screen, all layers except the lock
91 * layer are removed.
92 *
93 * A surface’s layer is modified on configuring the surface, in
94 * set_surface_type() (which is only called when the surface’s type change is
95 * _committed_). If a surface’s type changes (e.g. when making a window
96 * fullscreen) its layer changes too.
97 *
98 * In order to allow popup and transient surfaces to be correctly stacked above
99 * their parent surfaces, each surface tracks both its parent surface, and a
100 * linked list of its children. When a surface’s layer is updated, so are the
101 * layers of its children. Note that child surfaces are *not* the same as
102 * subsurfaces — child/parent surfaces are purely for maintaining stacking
103 * order.
104 *
105 * The children_link list of siblings of a surface (i.e. those surfaces which
106 * have the same parent) only contains weston_surfaces which have a
107 * shell_surface. Stacking is not implemented for non-shell_surface
108 * weston_surfaces. This means that the following implication does *not* hold:
109 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
110 */
111
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200112struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500113 struct wl_resource *resource;
114 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200115
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500116 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500117 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600118 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200119 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400120 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000121 struct wl_list children_list; /* child surfaces of this one */
122 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300123 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200124
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400125 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400126 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500127 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200128 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800129 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200130 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800131 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700132 int unresponsive, grabbed;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100133
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500134 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200135 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500136 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200137 } rotation;
138
139 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100140 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500141 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100142 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400143 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500144 } popup;
145
Alex Wu4539b082012-03-01 12:57:46 +0800146 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300147 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400148 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300149 } transient;
150
151 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800152 enum wl_shell_surface_fullscreen_method type;
153 struct weston_transform transform; /* matrix from x, y */
154 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500155 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800156 } fullscreen;
157
Scott Moreauff1db4a2012-04-17 19:06:18 -0600158 struct ping_timer *ping_timer;
159
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200160 struct weston_transform workspace_transform;
161
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500162 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500163 struct weston_output *output;
Rafael Antognolli65f98d82013-12-03 15:35:47 -0200164 struct weston_output *recommended_output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100165 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400166
167 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200168
169 struct {
170 bool maximized;
171 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200172 bool relative;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200173 } state, next_state; /* surface states */
174 bool state_changed;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200175};
176
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300177struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400178 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300179 struct shell_surface *shsurf;
180 struct wl_listener shsurf_destroy_listener;
181};
182
Rusty Lynch1084da52013-08-15 09:10:08 -0700183struct shell_touch_grab {
184 struct weston_touch_grab grab;
185 struct shell_surface *shsurf;
186 struct wl_listener shsurf_destroy_listener;
187 struct weston_touch *touch;
188};
189
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300190struct weston_move_grab {
191 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100192 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500193};
194
Rusty Lynch1084da52013-08-15 09:10:08 -0700195struct weston_touch_move_grab {
196 struct shell_touch_grab base;
197 wl_fixed_t dx, dy;
198};
199
Pekka Paalanen460099f2012-01-20 16:48:25 +0200200struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300201 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500202 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200203 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200204 float x;
205 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200206 } center;
207};
208
Giulio Camuffo5085a752013-03-25 21:42:45 +0100209struct shell_seat {
210 struct weston_seat *seat;
211 struct wl_listener seat_destroy_listener;
212
213 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400214 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100215 struct wl_list surfaces_list;
216 struct wl_client *client;
217 int32_t initial_up;
218 } popup_grab;
219};
220
Alex Wubd3354b2012-04-17 17:20:49 +0800221static struct desktop_shell *
222shell_surface_get_shell(struct shell_surface *shsurf);
223
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500224static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400225surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500226
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300227static void
228shell_fade_startup(struct desktop_shell *shell);
229
Philip Withnallbecb77e2013-11-25 18:01:30 +0000230static struct shell_seat *
231get_shell_seat(struct weston_seat *seat);
232
Philip Withnall648a4dd2013-11-25 18:01:44 +0000233static void
234shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
235
Alex Wubd3354b2012-04-17 17:20:49 +0800236static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200237shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
238
239static bool
240shell_surface_is_xdg_surface(struct shell_surface *shsurf);
241
242static bool
243shell_surface_is_xdg_popup(struct shell_surface *shsurf);
244
245static void
246shell_surface_set_parent(struct shell_surface *shsurf,
247 struct weston_surface *parent);
248
249static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800250shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
251{
252 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500253 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800254
255 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100256
Jason Ekstranda7af7042013-10-12 22:38:11 -0500257 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800258 return false;
259
Jason Ekstranda7af7042013-10-12 22:38:11 -0500260 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
261 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800262 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500263 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800264}
265
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500266static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400267destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300268{
269 struct shell_grab *grab;
270
271 grab = container_of(listener, struct shell_grab,
272 shsurf_destroy_listener);
273
274 grab->shsurf = NULL;
275}
276
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800277struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278get_default_view(struct weston_surface *surface)
279{
280 struct shell_surface *shsurf;
281 struct weston_view *view;
282
283 if (!surface || wl_list_empty(&surface->views))
284 return NULL;
285
286 shsurf = get_shell_surface(surface);
287 if (shsurf)
288 return shsurf->view;
289
290 wl_list_for_each(view, &surface->views, surface_link)
291 if (weston_view_is_mapped(view))
292 return view;
293
294 return container_of(surface->views.next, struct weston_view, surface_link);
295}
296
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300297static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400298popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400299
300static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300301shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400302 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300303 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400304 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300305 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300306{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300307 struct desktop_shell *shell = shsurf->shell;
308
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400309 popup_grab_end(pointer);
310
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300311 grab->grab.interface = interface;
312 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400313 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500314 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400315 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300316
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700317 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400318 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400319 if (shell->child.desktop_shell) {
320 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
321 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500322 weston_pointer_set_focus(pointer,
323 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400324 wl_fixed_from_int(0),
325 wl_fixed_from_int(0));
326 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300327}
328
329static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300330shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300331{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700332 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400333 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700334 grab->shsurf->grabbed = 0;
335 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300336
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700337 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300338}
339
340static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700341shell_touch_grab_start(struct shell_touch_grab *grab,
342 const struct weston_touch_grab_interface *interface,
343 struct shell_surface *shsurf,
344 struct weston_touch *touch)
345{
346 struct desktop_shell *shell = shsurf->shell;
347
348 grab->grab.interface = interface;
349 grab->shsurf = shsurf;
350 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
351 wl_signal_add(&shsurf->destroy_signal,
352 &grab->shsurf_destroy_listener);
353
354 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700355 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700356
357 weston_touch_start_grab(touch, &grab->grab);
358 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500359 weston_touch_set_focus(touch->seat,
360 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700361}
362
363static void
364shell_touch_grab_end(struct shell_touch_grab *grab)
365{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700366 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700367 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700368 grab->shsurf->grabbed = 0;
369 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700370
371 weston_touch_end_grab(grab->touch);
372}
373
374static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500375center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800376 struct weston_output *output);
377
Daniel Stone496ca172012-05-30 16:31:42 +0100378static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300379get_modifier(char *modifier)
380{
381 if (!modifier)
382 return MODIFIER_SUPER;
383
384 if (!strcmp("ctrl", modifier))
385 return MODIFIER_CTRL;
386 else if (!strcmp("alt", modifier))
387 return MODIFIER_ALT;
388 else if (!strcmp("super", modifier))
389 return MODIFIER_SUPER;
390 else
391 return MODIFIER_SUPER;
392}
393
Juan Zhaoe10d2792012-04-25 19:09:52 +0800394static enum animation_type
395get_animation_type(char *animation)
396{
Juan Zhaoe10d2792012-04-25 19:09:52 +0800397 if (!strcmp("zoom", animation))
398 return ANIMATION_ZOOM;
399 else if (!strcmp("fade", animation))
400 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100401 else if (!strcmp("dim-layer", animation))
402 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800403 else
404 return ANIMATION_NONE;
405}
406
Alex Wu4539b082012-03-01 12:57:46 +0800407static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400408shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200409{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400410 struct weston_config_section *section;
411 int duration;
412 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200413
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400414 section = weston_config_get_section(shell->compositor->config,
415 "screensaver", NULL, NULL);
416 weston_config_section_get_string(section,
417 "path", &shell->screensaver.path, NULL);
418 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200419 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400420
421 section = weston_config_get_section(shell->compositor->config,
422 "shell", NULL, NULL);
423 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100424 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100425 shell->client = s;
426 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400427 "binding-modifier", &s, "super");
428 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200429 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400430 weston_config_section_get_string(section, "animation", &s, "none");
431 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200432 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700433 weston_config_section_get_string(section,
434 "startup-animation", &s, "fade");
435 shell->startup_animation_type = get_animation_type(s);
436 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700437 if (shell->startup_animation_type == ANIMATION_ZOOM)
438 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100439 weston_config_section_get_string(section, "focus-animation", &s, "none");
440 shell->focus_animation_type = get_animation_type(s);
441 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400442 weston_config_section_get_uint(section, "num-workspaces",
443 &shell->workspaces.num,
444 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200445}
446
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800447struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100448get_default_output(struct weston_compositor *compositor)
449{
450 return container_of(compositor->output_list.next,
451 struct weston_output, link);
452}
453
454
455/* no-op func for checking focus surface */
456static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600457focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100458{
459}
460
461static struct focus_surface *
462get_focus_surface(struct weston_surface *surface)
463{
464 if (surface->configure == focus_surface_configure)
465 return surface->configure_private;
466 else
467 return NULL;
468}
469
470static bool
471is_focus_surface (struct weston_surface *es)
472{
473 return (es->configure == focus_surface_configure);
474}
475
476static bool
477is_focus_view (struct weston_view *view)
478{
479 return is_focus_surface (view->surface);
480}
481
482static struct focus_surface *
483create_focus_surface(struct weston_compositor *ec,
484 struct weston_output *output)
485{
486 struct focus_surface *fsurf = NULL;
487 struct weston_surface *surface = NULL;
488
489 fsurf = malloc(sizeof *fsurf);
490 if (!fsurf)
491 return NULL;
492
493 fsurf->surface = weston_surface_create(ec);
494 surface = fsurf->surface;
495 if (surface == NULL) {
496 free(fsurf);
497 return NULL;
498 }
499
500 surface->configure = focus_surface_configure;
501 surface->output = output;
502 surface->configure_private = fsurf;
503
504 fsurf->view = weston_view_create (surface);
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100505 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100506
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600507 surface->width = output->width;
508 surface->height = output->height;
509 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100510 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
511 pixman_region32_fini(&surface->opaque);
512 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
513 output->width, output->height);
514 pixman_region32_fini(&surface->input);
515 pixman_region32_init(&surface->input);
516
517 wl_list_init(&fsurf->workspace_transform.link);
518
519 return fsurf;
520}
521
522static void
523focus_surface_destroy(struct focus_surface *fsurf)
524{
525 weston_surface_destroy(fsurf->surface);
526 free(fsurf);
527}
528
529static void
530focus_animation_done(struct weston_view_animation *animation, void *data)
531{
532 struct workspace *ws = data;
533
534 ws->focus_animation = NULL;
535}
536
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200537static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200538focus_state_destroy(struct focus_state *state)
539{
540 wl_list_remove(&state->seat_destroy_listener.link);
541 wl_list_remove(&state->surface_destroy_listener.link);
542 free(state);
543}
544
545static void
546focus_state_seat_destroy(struct wl_listener *listener, void *data)
547{
548 struct focus_state *state = container_of(listener,
549 struct focus_state,
550 seat_destroy_listener);
551
552 wl_list_remove(&state->link);
553 focus_state_destroy(state);
554}
555
556static void
557focus_state_surface_destroy(struct wl_listener *listener, void *data)
558{
559 struct focus_state *state = container_of(listener,
560 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400561 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400562 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500563 struct weston_surface *main_surface, *next;
564 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200565
Pekka Paalanen01388e22013-04-25 13:57:44 +0300566 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
567
Kristian Høgsberge3778222012-07-31 17:29:30 -0400568 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500569 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
570 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400571 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100572 if (is_focus_view(view))
573 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400574
Jason Ekstranda7af7042013-10-12 22:38:11 -0500575 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400576 break;
577 }
578
Pekka Paalanen01388e22013-04-25 13:57:44 +0300579 /* if the focus was a sub-surface, activate its main surface */
580 if (main_surface != state->keyboard_focus)
581 next = main_surface;
582
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100583 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400584 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100585 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400586 activate(shell, next, state->seat);
587 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100588 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
589 if (state->ws->focus_animation)
590 weston_view_animation_destroy(state->ws->focus_animation);
591
592 state->ws->focus_animation = weston_fade_run(
593 state->ws->fsurf_front->view,
594 state->ws->fsurf_front->view->alpha, 0.0, 300,
595 focus_animation_done, state->ws);
596 }
597
Kristian Høgsberge3778222012-07-31 17:29:30 -0400598 wl_list_remove(&state->link);
599 focus_state_destroy(state);
600 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200601}
602
603static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400604focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200605{
Jonas Ådahl04769742012-06-13 00:01:24 +0200606 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200607
608 state = malloc(sizeof *state);
609 if (state == NULL)
610 return NULL;
611
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100612 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400613 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200614 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400615 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200616
617 state->seat_destroy_listener.notify = focus_state_seat_destroy;
618 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400619 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200620 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400621 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200622
623 return state;
624}
625
Jonas Ådahl8538b222012-08-29 22:13:03 +0200626static struct focus_state *
627ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
628{
629 struct workspace *ws = get_current_workspace(shell);
630 struct focus_state *state;
631
632 wl_list_for_each(state, &ws->focus_list, link)
633 if (state->seat == seat)
634 break;
635
636 if (&state->link == &ws->focus_list)
637 state = focus_state_create(seat, ws);
638
639 return state;
640}
641
Jonas Ådahl04769742012-06-13 00:01:24 +0200642static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400643restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200644{
645 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400646 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200647
648 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400649 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200650
Kristian Høgsberge3148752013-05-06 23:19:49 -0400651 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200652 }
653}
654
655static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200656replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
657 struct weston_seat *seat)
658{
659 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400660 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200661
662 wl_list_for_each(state, &ws->focus_list, link) {
663 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400664 surface = seat->keyboard->focus;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500665 state->keyboard_focus = surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200666 return;
667 }
668 }
669}
670
671static void
672drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
673 struct weston_surface *surface)
674{
675 struct focus_state *state;
676
677 wl_list_for_each(state, &ws->focus_list, link)
678 if (state->keyboard_focus == surface)
679 state->keyboard_focus = NULL;
680}
681
682static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100683animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
684 struct weston_view *from, struct weston_view *to)
685{
686 struct weston_output *output;
687 bool focus_surface_created = false;
688
689 /* FIXME: Only support dim animation using two layers */
690 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
691 return;
692
693 output = get_default_output(shell->compositor);
694 if (ws->fsurf_front == NULL && (from || to)) {
695 ws->fsurf_front = create_focus_surface(shell->compositor, output);
696 ws->fsurf_back = create_focus_surface(shell->compositor, output);
697 ws->fsurf_front->view->alpha = 0.0;
698 ws->fsurf_back->view->alpha = 0.0;
699 focus_surface_created = true;
700 } else {
701 wl_list_remove(&ws->fsurf_front->view->layer_link);
702 wl_list_remove(&ws->fsurf_back->view->layer_link);
703 }
704
705 if (ws->focus_animation) {
706 weston_view_animation_destroy(ws->focus_animation);
707 ws->focus_animation = NULL;
708 }
709
710 if (to)
711 wl_list_insert(&to->layer_link,
712 &ws->fsurf_front->view->layer_link);
713 else if (from)
714 wl_list_insert(&ws->layer.view_list,
715 &ws->fsurf_front->view->layer_link);
716
717 if (focus_surface_created) {
718 ws->focus_animation = weston_fade_run(
719 ws->fsurf_front->view,
720 ws->fsurf_front->view->alpha, 0.6, 300,
721 focus_animation_done, ws);
722 } else if (from) {
723 wl_list_insert(&from->layer_link,
724 &ws->fsurf_back->view->layer_link);
725 ws->focus_animation = weston_stable_fade_run(
726 ws->fsurf_front->view, 0.0,
727 ws->fsurf_back->view, 0.6,
728 focus_animation_done, ws);
729 } else if (to) {
730 wl_list_insert(&ws->layer.view_list,
731 &ws->fsurf_back->view->layer_link);
732 ws->focus_animation = weston_stable_fade_run(
733 ws->fsurf_front->view, 0.0,
734 ws->fsurf_back->view, 0.6,
735 focus_animation_done, ws);
736 }
737}
738
739static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200740workspace_destroy(struct workspace *ws)
741{
Jonas Ådahl04769742012-06-13 00:01:24 +0200742 struct focus_state *state, *next;
743
744 wl_list_for_each_safe(state, next, &ws->focus_list, link)
745 focus_state_destroy(state);
746
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100747 if (ws->fsurf_front)
748 focus_surface_destroy(ws->fsurf_front);
749 if (ws->fsurf_back)
750 focus_surface_destroy(ws->fsurf_back);
751
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200752 free(ws);
753}
754
Jonas Ådahl04769742012-06-13 00:01:24 +0200755static void
756seat_destroyed(struct wl_listener *listener, void *data)
757{
758 struct weston_seat *seat = data;
759 struct focus_state *state, *next;
760 struct workspace *ws = container_of(listener,
761 struct workspace,
762 seat_destroyed_listener);
763
764 wl_list_for_each_safe(state, next, &ws->focus_list, link)
765 if (state->seat == seat)
766 wl_list_remove(&state->link);
767}
768
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200769static struct workspace *
770workspace_create(void)
771{
772 struct workspace *ws = malloc(sizeof *ws);
773 if (ws == NULL)
774 return NULL;
775
776 weston_layer_init(&ws->layer, NULL);
777
Jonas Ådahl04769742012-06-13 00:01:24 +0200778 wl_list_init(&ws->focus_list);
779 wl_list_init(&ws->seat_destroyed_listener.link);
780 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100781 ws->fsurf_front = NULL;
782 ws->fsurf_back = NULL;
783 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200784
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200785 return ws;
786}
787
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200788static int
789workspace_is_empty(struct workspace *ws)
790{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500791 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200792}
793
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200794static struct workspace *
795get_workspace(struct desktop_shell *shell, unsigned int index)
796{
797 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200798 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200799 pws += index;
800 return *pws;
801}
802
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800803struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200804get_current_workspace(struct desktop_shell *shell)
805{
806 return get_workspace(shell, shell->workspaces.current);
807}
808
809static void
810activate_workspace(struct desktop_shell *shell, unsigned int index)
811{
812 struct workspace *ws;
813
814 ws = get_workspace(shell, index);
815 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
816
817 shell->workspaces.current = index;
818}
819
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200820static unsigned int
821get_output_height(struct weston_output *output)
822{
823 return abs(output->region.extents.y1 - output->region.extents.y2);
824}
825
826static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100827view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200828{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200829 struct weston_transform *transform;
830
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100831 if (is_focus_view(view)) {
832 struct focus_surface *fsurf = get_focus_surface(view->surface);
833 transform = &fsurf->workspace_transform;
834 } else {
835 struct shell_surface *shsurf = get_shell_surface(view->surface);
836 transform = &shsurf->workspace_transform;
837 }
838
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200839 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500840 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100841 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200842
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100843 weston_matrix_init(&transform->matrix);
844 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200845 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500846 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200847}
848
849static void
850workspace_translate_out(struct workspace *ws, double fraction)
851{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500852 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200853 unsigned int height;
854 double d;
855
Jason Ekstranda7af7042013-10-12 22:38:11 -0500856 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
857 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200858 d = height * fraction;
859
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100860 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200861 }
862}
863
864static void
865workspace_translate_in(struct workspace *ws, double fraction)
866{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500867 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200868 unsigned int height;
869 double d;
870
Jason Ekstranda7af7042013-10-12 22:38:11 -0500871 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
872 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200873
874 if (fraction > 0)
875 d = -(height - height * fraction);
876 else
877 d = height + height * fraction;
878
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100879 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200880 }
881}
882
883static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200884broadcast_current_workspace_state(struct desktop_shell *shell)
885{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700886 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200887
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700888 wl_resource_for_each(resource, &shell->workspaces.client_list)
889 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200890 shell->workspaces.current,
891 shell->workspaces.num);
892}
893
894static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200895reverse_workspace_change_animation(struct desktop_shell *shell,
896 unsigned int index,
897 struct workspace *from,
898 struct workspace *to)
899{
900 shell->workspaces.current = index;
901
902 shell->workspaces.anim_to = to;
903 shell->workspaces.anim_from = from;
904 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
905 shell->workspaces.anim_timestamp = 0;
906
Scott Moreau4272e632012-08-13 09:58:41 -0600907 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200908}
909
910static void
911workspace_deactivate_transforms(struct workspace *ws)
912{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500913 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100914 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200915
Jason Ekstranda7af7042013-10-12 22:38:11 -0500916 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100917 if (is_focus_view(view)) {
918 struct focus_surface *fsurf = get_focus_surface(view->surface);
919 transform = &fsurf->workspace_transform;
920 } else {
921 struct shell_surface *shsurf = get_shell_surface(view->surface);
922 transform = &shsurf->workspace_transform;
923 }
924
925 if (!wl_list_empty(&transform->link)) {
926 wl_list_remove(&transform->link);
927 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200928 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500929 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200930 }
931}
932
933static void
934finish_workspace_change_animation(struct desktop_shell *shell,
935 struct workspace *from,
936 struct workspace *to)
937{
Scott Moreau4272e632012-08-13 09:58:41 -0600938 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200939
940 wl_list_remove(&shell->workspaces.animation.link);
941 workspace_deactivate_transforms(from);
942 workspace_deactivate_transforms(to);
943 shell->workspaces.anim_to = NULL;
944
945 wl_list_remove(&shell->workspaces.anim_from->layer.link);
946}
947
948static void
949animate_workspace_change_frame(struct weston_animation *animation,
950 struct weston_output *output, uint32_t msecs)
951{
952 struct desktop_shell *shell =
953 container_of(animation, struct desktop_shell,
954 workspaces.animation);
955 struct workspace *from = shell->workspaces.anim_from;
956 struct workspace *to = shell->workspaces.anim_to;
957 uint32_t t;
958 double x, y;
959
960 if (workspace_is_empty(from) && workspace_is_empty(to)) {
961 finish_workspace_change_animation(shell, from, to);
962 return;
963 }
964
965 if (shell->workspaces.anim_timestamp == 0) {
966 if (shell->workspaces.anim_current == 0.0)
967 shell->workspaces.anim_timestamp = msecs;
968 else
969 shell->workspaces.anim_timestamp =
970 msecs -
971 /* Invers of movement function 'y' below. */
972 (asin(1.0 - shell->workspaces.anim_current) *
973 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
974 M_2_PI);
975 }
976
977 t = msecs - shell->workspaces.anim_timestamp;
978
979 /*
980 * x = [0, π/2]
981 * y(x) = sin(x)
982 */
983 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
984 y = sin(x);
985
986 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600987 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200988
989 workspace_translate_out(from, shell->workspaces.anim_dir * y);
990 workspace_translate_in(to, shell->workspaces.anim_dir * y);
991 shell->workspaces.anim_current = y;
992
Scott Moreau4272e632012-08-13 09:58:41 -0600993 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200994 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200995 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200996 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200997}
998
999static void
1000animate_workspace_change(struct desktop_shell *shell,
1001 unsigned int index,
1002 struct workspace *from,
1003 struct workspace *to)
1004{
1005 struct weston_output *output;
1006
1007 int dir;
1008
1009 if (index > shell->workspaces.current)
1010 dir = -1;
1011 else
1012 dir = 1;
1013
1014 shell->workspaces.current = index;
1015
1016 shell->workspaces.anim_dir = dir;
1017 shell->workspaces.anim_from = from;
1018 shell->workspaces.anim_to = to;
1019 shell->workspaces.anim_current = 0.0;
1020 shell->workspaces.anim_timestamp = 0;
1021
1022 output = container_of(shell->compositor->output_list.next,
1023 struct weston_output, link);
1024 wl_list_insert(&output->animation_list,
1025 &shell->workspaces.animation.link);
1026
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001027 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001028
1029 workspace_translate_in(to, 0);
1030
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001031 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001032
Scott Moreau4272e632012-08-13 09:58:41 -06001033 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001034}
1035
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001036static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001037update_workspace(struct desktop_shell *shell, unsigned int index,
1038 struct workspace *from, struct workspace *to)
1039{
1040 shell->workspaces.current = index;
1041 wl_list_insert(&from->layer.link, &to->layer.link);
1042 wl_list_remove(&from->layer.link);
1043}
1044
1045static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001046change_workspace(struct desktop_shell *shell, unsigned int index)
1047{
1048 struct workspace *from;
1049 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001050 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001051
1052 if (index == shell->workspaces.current)
1053 return;
1054
1055 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001056 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001057 return;
1058
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001059 from = get_current_workspace(shell);
1060 to = get_workspace(shell, index);
1061
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001062 if (shell->workspaces.anim_from == to &&
1063 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001064 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001065 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001066 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001067 return;
1068 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001069
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001070 if (shell->workspaces.anim_to != NULL)
1071 finish_workspace_change_animation(shell,
1072 shell->workspaces.anim_from,
1073 shell->workspaces.anim_to);
1074
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001075 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001076
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001077 if (shell->focus_animation_type != ANIMATION_NONE) {
1078 wl_list_for_each(state, &from->focus_list, link)
1079 if (state->keyboard_focus)
1080 animate_focus_change(shell, from,
1081 get_default_view(state->keyboard_focus), NULL);
1082
1083 wl_list_for_each(state, &to->focus_list, link)
1084 if (state->keyboard_focus)
1085 animate_focus_change(shell, to,
1086 NULL, get_default_view(state->keyboard_focus));
1087 }
1088
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001089 if (workspace_is_empty(to) && workspace_is_empty(from))
1090 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001091 else
1092 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001093
1094 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001095}
1096
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001097static bool
1098workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1099{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001100 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001101 struct wl_list *e;
1102
1103 if (wl_list_empty(list))
1104 return false;
1105
1106 e = list->next;
1107
1108 if (e->next != list)
1109 return false;
1110
Jason Ekstranda7af7042013-10-12 22:38:11 -05001111 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001112}
1113
1114static void
Philip Withnall659163d2013-11-25 18:01:36 +00001115move_surface_to_workspace(struct desktop_shell *shell,
1116 struct shell_surface *shsurf,
1117 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001118{
1119 struct workspace *from;
1120 struct workspace *to;
1121 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001122 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001123 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001124
1125 if (workspace == shell->workspaces.current)
1126 return;
1127
Philip Withnall659163d2013-11-25 18:01:36 +00001128 view = get_default_view(shsurf->surface);
1129 if (!view)
1130 return;
1131
1132 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1133
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001134 if (workspace >= shell->workspaces.num)
1135 workspace = shell->workspaces.num - 1;
1136
Jonas Ådahle9d22502012-08-29 22:13:01 +02001137 from = get_current_workspace(shell);
1138 to = get_workspace(shell, workspace);
1139
Jason Ekstranda7af7042013-10-12 22:38:11 -05001140 wl_list_remove(&view->layer_link);
1141 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001142
Philip Withnall648a4dd2013-11-25 18:01:44 +00001143 shell_surface_update_child_surface_layers(shsurf);
1144
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001146 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1147 if (!seat->keyboard)
1148 continue;
1149
1150 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001151 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001152 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001153 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001154
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001156}
1157
1158static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001159take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001160 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001161 unsigned int index)
1162{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001163 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001164 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001165 struct shell_surface *shsurf;
1166 struct workspace *from;
1167 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001168 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001169
Pekka Paalanen01388e22013-04-25 13:57:44 +03001170 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001171 view = get_default_view(surface);
1172 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001173 index == shell->workspaces.current ||
1174 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001175 return;
1176
1177 from = get_current_workspace(shell);
1178 to = get_workspace(shell, index);
1179
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 wl_list_remove(&view->layer_link);
1181 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001182
Philip Withnall659163d2013-11-25 18:01:36 +00001183 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001184 if (shsurf != NULL)
1185 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001186
Jonas Ådahle9d22502012-08-29 22:13:01 +02001187 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001188 drop_focus_state(shell, from, surface);
1189
1190 if (shell->workspaces.anim_from == to &&
1191 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001192 wl_list_remove(&to->layer.link);
1193 wl_list_insert(from->layer.link.prev, &to->layer.link);
1194
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001195 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001196 broadcast_current_workspace_state(shell);
1197
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001198 return;
1199 }
1200
1201 if (shell->workspaces.anim_to != NULL)
1202 finish_workspace_change_animation(shell,
1203 shell->workspaces.anim_from,
1204 shell->workspaces.anim_to);
1205
1206 if (workspace_is_empty(from) &&
1207 workspace_has_only(to, surface))
1208 update_workspace(shell, index, from, to);
1209 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001210 if (shsurf != NULL &&
1211 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001212 wl_list_insert(&shell->workspaces.anim_sticky_list,
1213 &shsurf->workspace_transform.link);
1214
1215 animate_workspace_change(shell, index, from, to);
1216 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001217
1218 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001219
1220 state = ensure_focus_state(shell, seat);
1221 if (state != NULL)
1222 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001223}
1224
1225static void
1226workspace_manager_move_surface(struct wl_client *client,
1227 struct wl_resource *resource,
1228 struct wl_resource *surface_resource,
1229 uint32_t workspace)
1230{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001231 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001232 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001233 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001234 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001235 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001236
Pekka Paalanen01388e22013-04-25 13:57:44 +03001237 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001238 shell_surface = get_shell_surface(main_surface);
1239 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001241
1242 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001243}
1244
1245static const struct workspace_manager_interface workspace_manager_implementation = {
1246 workspace_manager_move_surface,
1247};
1248
1249static void
1250unbind_resource(struct wl_resource *resource)
1251{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001252 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001253}
1254
1255static void
1256bind_workspace_manager(struct wl_client *client,
1257 void *data, uint32_t version, uint32_t id)
1258{
1259 struct desktop_shell *shell = data;
1260 struct wl_resource *resource;
1261
Jason Ekstranda85118c2013-06-27 20:17:02 -05001262 resource = wl_resource_create(client,
1263 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001264
1265 if (resource == NULL) {
1266 weston_log("couldn't add workspace manager object");
1267 return;
1268 }
1269
Jason Ekstranda85118c2013-06-27 20:17:02 -05001270 wl_resource_set_implementation(resource,
1271 &workspace_manager_implementation,
1272 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001273 wl_list_insert(&shell->workspaces.client_list,
1274 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001275
1276 workspace_manager_send_state(resource,
1277 shell->workspaces.current,
1278 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001279}
1280
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001281static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001282touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1283 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1284{
1285}
1286
1287static void
1288touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1289{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001290 struct weston_touch_move_grab *move =
1291 (struct weston_touch_move_grab *) container_of(
1292 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001293
Jonas Ådahl9484b692013-12-02 22:05:03 +01001294 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001295 shell_touch_grab_end(&move->base);
1296 free(move);
1297 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001298}
1299
1300static void
1301touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1302 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1303{
1304 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1305 struct shell_surface *shsurf = move->base.shsurf;
1306 struct weston_surface *es;
1307 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1308 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1309
1310 if (!shsurf)
1311 return;
1312
1313 es = shsurf->surface;
1314
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001315 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001316
1317 weston_compositor_schedule_repaint(es->compositor);
1318}
1319
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001320static void
1321touch_move_grab_cancel(struct weston_touch_grab *grab)
1322{
1323 struct weston_touch_move_grab *move =
1324 (struct weston_touch_move_grab *) container_of(
1325 grab, struct shell_touch_grab, grab);
1326
1327 shell_touch_grab_end(&move->base);
1328 free(move);
1329}
1330
Rusty Lynch1084da52013-08-15 09:10:08 -07001331static const struct weston_touch_grab_interface touch_move_grab_interface = {
1332 touch_move_grab_down,
1333 touch_move_grab_up,
1334 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001335 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001336};
1337
1338static int
1339surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1340{
1341 struct weston_touch_move_grab *move;
1342
1343 if (!shsurf)
1344 return -1;
1345
Rafael Antognolli03b16592013-12-03 15:35:42 -02001346 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001347 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001348 if (shsurf->grabbed)
1349 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001350
1351 move = malloc(sizeof *move);
1352 if (!move)
1353 return -1;
1354
Jason Ekstranda7af7042013-10-12 22:38:11 -05001355 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001356 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001358 seat->touch->grab_y;
1359
1360 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1361 seat->touch);
1362
1363 return 0;
1364}
1365
1366static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001367noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001368{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001369}
1370
1371static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001372move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1373 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001374{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001375 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001376 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001377 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001378 int dx, dy;
1379
1380 weston_pointer_move(pointer, x, y);
1381 dx = wl_fixed_to_int(pointer->x + move->dx);
1382 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001383
1384 if (!shsurf)
1385 return;
1386
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001387 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001388
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001390}
1391
1392static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001393move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001394 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001395{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001396 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1397 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001398 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001399 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001400
Daniel Stone4dbadb12012-05-30 16:31:51 +01001401 if (pointer->button_count == 0 &&
1402 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001403 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001404 free(grab);
1405 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001406}
1407
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001408static void
1409move_grab_cancel(struct weston_pointer_grab *grab)
1410{
1411 struct shell_grab *shell_grab =
1412 container_of(grab, struct shell_grab, grab);
1413
1414 shell_grab_end(shell_grab);
1415 free(grab);
1416}
1417
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001418static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001419 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001420 move_grab_motion,
1421 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001422 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001423};
1424
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001425static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001426surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001427{
1428 struct weston_move_grab *move;
1429
1430 if (!shsurf)
1431 return -1;
1432
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001433 if (shsurf->grabbed)
1434 return 0;
Rafael Antognolli03b16592013-12-03 15:35:42 -02001435 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001436 return 0;
1437
1438 move = malloc(sizeof *move);
1439 if (!move)
1440 return -1;
1441
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001443 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001444 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001445 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001446
1447 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001448 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001449
1450 return 0;
1451}
1452
1453static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001454common_surface_move(struct wl_resource *resource,
1455 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001456{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001457 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001458 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001459 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001460
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001461 if (seat->pointer &&
1462 seat->pointer->button_count > 0 &&
1463 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001465 if ((surface == shsurf->surface) &&
1466 (surface_move(shsurf, seat) < 0))
1467 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001468 } else if (seat->touch &&
1469 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001470 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001471 if ((surface == shsurf->surface) &&
1472 (surface_touch_move(shsurf, seat) < 0))
1473 wl_resource_post_no_memory(resource);
1474 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001475}
1476
Rafael Antognollie2a34552013-12-03 15:35:45 -02001477static void
1478shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1479 struct wl_resource *seat_resource, uint32_t serial)
1480{
1481 common_surface_move(resource, seat_resource, serial);
1482}
1483
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001484struct weston_resize_grab {
1485 struct shell_grab base;
1486 uint32_t edges;
1487 int32_t width, height;
1488};
1489
1490static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001491resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1492 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001493{
1494 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001495 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001496 struct shell_surface *shsurf = resize->base.shsurf;
1497 int32_t width, height;
1498 wl_fixed_t from_x, from_y;
1499 wl_fixed_t to_x, to_y;
1500
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001501 weston_pointer_move(pointer, x, y);
1502
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001503 if (!shsurf)
1504 return;
1505
Jason Ekstranda7af7042013-10-12 22:38:11 -05001506 weston_view_from_global_fixed(shsurf->view,
1507 pointer->grab_x, pointer->grab_y,
1508 &from_x, &from_y);
1509 weston_view_from_global_fixed(shsurf->view,
1510 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001511
1512 width = resize->width;
1513 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1514 width += wl_fixed_to_int(from_x - to_x);
1515 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1516 width += wl_fixed_to_int(to_x - from_x);
1517 }
1518
1519 height = resize->height;
1520 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1521 height += wl_fixed_to_int(from_y - to_y);
1522 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1523 height += wl_fixed_to_int(to_y - from_y);
1524 }
1525
1526 shsurf->client->send_configure(shsurf->surface,
1527 resize->edges, width, height);
1528}
1529
1530static void
1531send_configure(struct weston_surface *surface,
1532 uint32_t edges, int32_t width, int32_t height)
1533{
1534 struct shell_surface *shsurf = get_shell_surface(surface);
1535
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001536 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001537 edges, width, height);
1538}
1539
1540static const struct weston_shell_client shell_client = {
1541 send_configure
1542};
1543
1544static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001545resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001546 uint32_t time, uint32_t button, uint32_t state_w)
1547{
1548 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001549 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001550 enum wl_pointer_button_state state = state_w;
1551
1552 if (pointer->button_count == 0 &&
1553 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1554 shell_grab_end(&resize->base);
1555 free(grab);
1556 }
1557}
1558
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001559static void
1560resize_grab_cancel(struct weston_pointer_grab *grab)
1561{
1562 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1563
1564 shell_grab_end(&resize->base);
1565 free(grab);
1566}
1567
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001568static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001569 noop_grab_focus,
1570 resize_grab_motion,
1571 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001572 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001573};
1574
Giulio Camuffob8366642013-04-25 13:57:46 +03001575/*
1576 * Returns the bounding box of a surface and all its sub-surfaces,
1577 * in the surface coordinates system. */
1578static void
1579surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1580 int32_t *y, int32_t *w, int32_t *h) {
1581 pixman_region32_t region;
1582 pixman_box32_t *box;
1583 struct weston_subsurface *subsurface;
1584
1585 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586 surface->width,
1587 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001588
1589 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1590 pixman_region32_union_rect(&region, &region,
1591 subsurface->position.x,
1592 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001593 subsurface->surface->width,
1594 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001595 }
1596
1597 box = pixman_region32_extents(&region);
1598 if (x)
1599 *x = box->x1;
1600 if (y)
1601 *y = box->y1;
1602 if (w)
1603 *w = box->x2 - box->x1;
1604 if (h)
1605 *h = box->y2 - box->y1;
1606
1607 pixman_region32_fini(&region);
1608}
1609
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001610static int
1611surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001612 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001613{
1614 struct weston_resize_grab *resize;
1615
Rafael Antognolli03b16592013-12-03 15:35:42 -02001616 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001617 return 0;
1618
1619 if (edges == 0 || edges > 15 ||
1620 (edges & 3) == 3 || (edges & 12) == 12)
1621 return 0;
1622
1623 resize = malloc(sizeof *resize);
1624 if (!resize)
1625 return -1;
1626
1627 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001628 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1629 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001630
1631 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001632 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001633
1634 return 0;
1635}
1636
1637static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001638common_surface_resize(struct wl_resource *resource,
1639 struct wl_resource *seat_resource, uint32_t serial,
1640 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001641{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001642 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001643 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001644 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001645
Rafael Antognolli03b16592013-12-03 15:35:42 -02001646 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001647 return;
1648
Jason Ekstranda7af7042013-10-12 22:38:11 -05001649 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001650 if (seat->pointer->button_count == 0 ||
1651 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001652 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001653 return;
1654
Kristian Høgsberge3148752013-05-06 23:19:49 -04001655 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001656 wl_resource_post_no_memory(resource);
1657}
1658
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001659static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001660shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1661 struct wl_resource *seat_resource, uint32_t serial,
1662 uint32_t edges)
1663{
1664 common_surface_resize(resource, seat_resource, serial, edges);
1665}
1666
1667static void
Kristian Høgsberg67800732013-07-04 01:12:17 -04001668end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1669
1670static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001671busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001672{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001673 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001674 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001675 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001676 wl_fixed_t sx, sy;
1677
Jason Ekstranda7af7042013-10-12 22:38:11 -05001678 view = weston_compositor_pick_view(pointer->seat->compositor,
1679 pointer->x, pointer->y,
1680 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001681
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682 if (!grab->shsurf || grab->shsurf->surface != view->surface)
Kristian Høgsberg67800732013-07-04 01:12:17 -04001683 end_busy_cursor(grab->shsurf, pointer);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001684}
1685
1686static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001687busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1688 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001689{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001690 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001691}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001692
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001693static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001694busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001695 uint32_t time, uint32_t button, uint32_t state)
1696{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001697 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001698 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001699 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001700
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001701 if (shsurf && button == BTN_LEFT && state) {
1702 activate(shsurf->shell, shsurf->surface, seat);
1703 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001704 } else if (shsurf && button == BTN_RIGHT && state) {
1705 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001706 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001707 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001708}
1709
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001710static void
1711busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1712{
1713 struct shell_grab *grab = (struct shell_grab *) base;
1714
1715 shell_grab_end(grab);
1716 free(grab);
1717}
1718
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001719static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001720 busy_cursor_grab_focus,
1721 busy_cursor_grab_motion,
1722 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001723 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001724};
1725
1726static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001727set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001728{
1729 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001730
1731 grab = malloc(sizeof *grab);
1732 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001733 return;
1734
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001735 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1736 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001737}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001738
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001739static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001740end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001741{
1742 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1743
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001744 if (grab->grab.interface == &busy_cursor_grab_interface &&
1745 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001746 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001747 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001748 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001749}
1750
Scott Moreau9521d5e2012-04-19 13:06:17 -06001751static void
1752ping_timer_destroy(struct shell_surface *shsurf)
1753{
1754 if (!shsurf || !shsurf->ping_timer)
1755 return;
1756
1757 if (shsurf->ping_timer->source)
1758 wl_event_source_remove(shsurf->ping_timer->source);
1759
1760 free(shsurf->ping_timer);
1761 shsurf->ping_timer = NULL;
1762}
1763
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001764static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001765ping_timeout_handler(void *data)
1766{
1767 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001768 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001769
Scott Moreau9521d5e2012-04-19 13:06:17 -06001770 /* Client is not responding */
1771 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001772
1773 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Emilio Pozuelo Monfort3d0fc762013-11-22 16:21:20 +01001774 if (seat->pointer->focus &&
1775 seat->pointer->focus->surface == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001776 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001777
1778 return 1;
1779}
1780
1781static void
1782ping_handler(struct weston_surface *surface, uint32_t serial)
1783{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001784 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001785 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001786 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001787
1788 if (!shsurf)
1789 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001790 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001791 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001792
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001793 if (shsurf->surface == shsurf->shell->grab_surface)
1794 return;
1795
Scott Moreauff1db4a2012-04-17 19:06:18 -06001796 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001797 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001798 if (!shsurf->ping_timer)
1799 return;
1800
1801 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001802 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1803 shsurf->ping_timer->source =
1804 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1805 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1806
Rafael Antognollie2a34552013-12-03 15:35:45 -02001807 if (shell_surface_is_wl_shell_surface(shsurf))
1808 wl_shell_surface_send_ping(shsurf->resource, serial);
1809 else if (shell_surface_is_xdg_surface(shsurf))
1810 xdg_surface_send_ping(shsurf->resource, serial);
1811 else if (shell_surface_is_xdg_popup(shsurf))
1812 xdg_popup_send_ping(shsurf->resource, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001813 }
1814}
1815
1816static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001817handle_pointer_focus(struct wl_listener *listener, void *data)
1818{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001819 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001820 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001821 struct weston_compositor *compositor;
1822 struct shell_surface *shsurf;
1823 uint32_t serial;
1824
Jason Ekstranda7af7042013-10-12 22:38:11 -05001825 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001826 return;
1827
Jason Ekstranda7af7042013-10-12 22:38:11 -05001828 compositor = view->surface->compositor;
1829 shsurf = get_shell_surface(view->surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001830
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001831 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001832 set_busy_cursor(shsurf, pointer);
1833 } else {
1834 serial = wl_display_next_serial(compositor->wl_display);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001836 }
1837}
1838
1839static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001840create_pointer_focus_listener(struct weston_seat *seat)
1841{
1842 struct wl_listener *listener;
1843
Kristian Høgsberge3148752013-05-06 23:19:49 -04001844 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001845 return;
1846
1847 listener = malloc(sizeof *listener);
1848 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001849 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001850}
1851
1852static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001853xdg_surface_set_transient_for(struct wl_client *client,
1854 struct wl_resource *resource,
1855 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06001856{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001857 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02001858 struct weston_surface *parent =
1859 wl_resource_get_user_data(parent_resource);
1860
1861 shell_surface_set_parent(shsurf, parent);
1862}
1863
1864static void
1865surface_pong(struct shell_surface *shsurf, uint32_t serial)
1866{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001867 struct weston_compositor *ec = shsurf->surface->compositor;
Rafael Antognollie2a34552013-12-03 15:35:45 -02001868 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001869
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001870 if (shsurf->ping_timer == NULL)
1871 /* Just ignore unsolicited pong. */
1872 return;
1873
Scott Moreauff1db4a2012-04-17 19:06:18 -06001874 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001875 shsurf->unresponsive = 0;
Hardeningeb1e1302013-05-17 18:07:41 +02001876 wl_list_for_each(seat, &ec->seat_list, link) {
1877 if(seat->pointer)
1878 end_busy_cursor(shsurf, seat->pointer);
1879 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001880 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001881 }
1882}
1883
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001884static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001885shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1886 uint32_t serial)
1887{
1888 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1889
1890 surface_pong(shsurf, serial);
1891
1892}
1893
1894static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001895set_title(struct shell_surface *shsurf, const char *title)
1896{
1897 free(shsurf->title);
1898 shsurf->title = strdup(title);
1899}
1900
1901static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001902shell_surface_set_title(struct wl_client *client,
1903 struct wl_resource *resource, const char *title)
1904{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001905 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001906
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001907 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001908}
1909
1910static void
1911shell_surface_set_class(struct wl_client *client,
1912 struct wl_resource *resource, const char *class)
1913{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001914 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001915
1916 free(shsurf->class);
1917 shsurf->class = strdup(class);
1918}
1919
Alex Wu4539b082012-03-01 12:57:46 +08001920static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001921restore_output_mode(struct weston_output *output)
1922{
Hardening57388e42013-09-18 23:56:36 +02001923 if (output->current_mode != output->original_mode ||
1924 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001925 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02001926 output->original_mode,
1927 output->original_scale,
1928 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001929}
1930
1931static void
1932restore_all_output_modes(struct weston_compositor *compositor)
1933{
1934 struct weston_output *output;
1935
1936 wl_list_for_each(output, &compositor->output_list, link)
1937 restore_output_mode(output);
1938}
1939
Philip Withnallbecb77e2013-11-25 18:01:30 +00001940static int
1941get_output_panel_height(struct desktop_shell *shell,
1942 struct weston_output *output)
1943{
1944 struct weston_view *view;
1945 int panel_height = 0;
1946
1947 if (!output)
1948 return 0;
1949
1950 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
1951 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001952 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00001953 break;
1954 }
1955 }
1956
1957 return panel_height;
1958}
1959
Philip Withnall07926d92013-11-25 18:01:40 +00001960/* The surface will be inserted into the list immediately after the link
1961 * returned by this function (i.e. will be stacked immediately above the
1962 * returned link). */
1963static struct wl_list *
1964shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1965{
1966 struct workspace *ws;
1967
1968 switch (shsurf->type) {
Philip Withnallda704d92013-11-25 18:01:46 +00001969 case SHELL_SURFACE_POPUP: {
1970 /* Popups should go at the front of the workspace of their
1971 * parent surface, rather than just in front of the parent. This
1972 * fixes the situation where there are two top-level windows:
1973 * - Above
1974 * - Below
1975 * and a pop-up menu is created for 'Below'. We want:
1976 * - Popup
1977 * - Above
1978 * - Below
1979 * not:
1980 * - Above
1981 * - Popup
1982 * - Below
1983 */
1984 struct shell_surface *parent_shsurf;
1985
1986 parent_shsurf = get_shell_surface(shsurf->parent);
1987 if (parent_shsurf != NULL)
1988 return shell_surface_calculate_layer_link(parent_shsurf);
1989
1990 break;
1991 }
1992
Rafael Antognolli03b16592013-12-03 15:35:42 -02001993 case SHELL_SURFACE_TOPLEVEL: {
Rafael Antognollied207b42013-12-03 15:35:43 -02001994 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02001995 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02001996 } else if (shsurf->parent) {
1997 /* Move the surface to its parent layer so that
1998 * surfaces which are transient for fullscreen surfaces
1999 * don't get hidden by the fullscreen surfaces.
2000 * However, unlike popups, transient surfaces are
2001 * stacked in front of their parent but not in front of
2002 * other surfaces of the same type. */
2003 struct weston_view *parent;
2004
2005 /* TODO: Handle a parent with multiple views */
2006 parent = get_default_view(shsurf->parent);
2007 if (parent)
2008 return parent->layer_link.prev;
2009 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002010 break;
2011 }
Philip Withnall07926d92013-11-25 18:01:40 +00002012
2013 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002014 return &shsurf->shell->fullscreen_layer.view_list;
2015
Philip Withnall07926d92013-11-25 18:01:40 +00002016 case SHELL_SURFACE_NONE:
2017 default:
2018 /* Go to the fallback, below. */
2019 break;
2020 }
2021
2022 /* Move the surface to a normal workspace layer so that surfaces
2023 * which were previously fullscreen or transient are no longer
2024 * rendered on top. */
2025 ws = get_current_workspace(shsurf->shell);
2026 return &ws->layer.view_list;
2027}
2028
Philip Withnall648a4dd2013-11-25 18:01:44 +00002029static void
2030shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2031{
2032 struct shell_surface *child;
2033
2034 /* Move the child layers to the same workspace as shsurf. They will be
2035 * stacked above shsurf. */
2036 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2037 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2038 weston_view_geometry_dirty(child->view);
2039 wl_list_remove(&child->view->layer_link);
2040 wl_list_insert(shsurf->view->layer_link.prev,
2041 &child->view->layer_link);
2042 weston_view_geometry_dirty(child->view);
2043 weston_surface_damage(child->surface);
2044
2045 /* Recurse. We don’t expect this to recurse very far (if
2046 * at all) because that would imply we have transient
2047 * (or popup) children of transient surfaces, which
2048 * would be unusual. */
2049 shell_surface_update_child_surface_layers(child);
2050 }
2051 }
2052}
2053
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002054/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002055 * geometry to ensure the changes are redrawn.
2056 *
2057 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2058 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002059static void
2060shell_surface_update_layer(struct shell_surface *shsurf)
2061{
2062 struct wl_list *new_layer_link;
2063
2064 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2065
2066 if (new_layer_link == &shsurf->view->layer_link)
2067 return;
2068
2069 weston_view_geometry_dirty(shsurf->view);
2070 wl_list_remove(&shsurf->view->layer_link);
2071 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2072 weston_view_geometry_dirty(shsurf->view);
2073 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002074
2075 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002076}
2077
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002078static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002079shell_surface_set_parent(struct shell_surface *shsurf,
2080 struct weston_surface *parent)
2081{
2082 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002083
2084 wl_list_remove(&shsurf->children_link);
2085 wl_list_init(&shsurf->children_link);
2086
2087 /* Insert into the parent surface’s child list. */
2088 if (parent != NULL) {
2089 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2090 if (parent_shsurf != NULL)
2091 wl_list_insert(&parent_shsurf->children_list,
2092 &shsurf->children_link);
2093 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002094}
2095
2096static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002097shell_surface_set_output(struct shell_surface *shsurf,
2098 struct weston_output *output)
2099{
2100 struct weston_surface *es = shsurf->surface;
2101
2102 /* get the default output, if the client set it as NULL
2103 check whether the ouput is available */
2104 if (output)
2105 shsurf->output = output;
2106 else if (es->output)
2107 shsurf->output = es->output;
2108 else
2109 shsurf->output = get_default_output(es->compositor);
2110}
2111
2112static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002113surface_clear_next_states(struct shell_surface *shsurf)
2114{
2115 shsurf->next_state.maximized = false;
2116 shsurf->next_state.fullscreen = false;
2117
2118 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2119 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2120 shsurf->state_changed = true;
2121}
2122
2123static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002124set_toplevel(struct shell_surface *shsurf)
2125{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002126 shell_surface_set_parent(shsurf, NULL);
2127 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002128 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002129
2130 /* The layer_link is updated in set_surface_type(),
2131 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002132}
2133
2134static void
2135shell_surface_set_toplevel(struct wl_client *client,
2136 struct wl_resource *resource)
2137{
2138 struct shell_surface *surface = wl_resource_get_user_data(resource);
2139
2140 set_toplevel(surface);
2141}
2142
2143static void
2144set_transient(struct shell_surface *shsurf,
2145 struct weston_surface *parent, int x, int y, uint32_t flags)
2146{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002147 assert(parent != NULL);
2148
Philip Withnallbecb77e2013-11-25 18:01:30 +00002149 shsurf->transient.x = x;
2150 shsurf->transient.y = y;
2151 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002152
Rafael Antognollied207b42013-12-03 15:35:43 -02002153 shsurf->next_state.relative = true;
Rafael Antognollidb59f9a2013-12-05 18:53:17 -02002154 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002155
2156 /* The layer_link is updated in set_surface_type(),
2157 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002158}
2159
2160static void
2161shell_surface_set_transient(struct wl_client *client,
2162 struct wl_resource *resource,
2163 struct wl_resource *parent_resource,
2164 int x, int y, uint32_t flags)
2165{
2166 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2167 struct weston_surface *parent =
2168 wl_resource_get_user_data(parent_resource);
2169
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002170 shell_surface_set_parent(shsurf, parent);
2171
Rafael Antognolli03b16592013-12-03 15:35:42 -02002172 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002173 set_transient(shsurf, parent, x, y, flags);
2174}
2175
2176static void
2177set_fullscreen(struct shell_surface *shsurf,
2178 uint32_t method,
2179 uint32_t framerate,
2180 struct weston_output *output)
2181{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002182 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002183
2184 shsurf->fullscreen_output = shsurf->output;
2185 shsurf->fullscreen.type = method;
2186 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002187
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08002188 shsurf->next_state.fullscreen = true;
2189 shsurf->state_changed = true;
Rafael Antognollidb59f9a2013-12-05 18:53:17 -02002190 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002191
2192 shsurf->client->send_configure(shsurf->surface, 0,
2193 shsurf->output->width,
2194 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002195
2196 /* The layer_link is updated in set_surface_type(),
2197 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002198}
2199
2200static void
2201unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002202{
Philip Withnallf85fe842013-11-25 18:01:37 +00002203 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002204 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2205 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002206 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002207 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002208 shsurf->fullscreen_output = NULL;
2209
Alex Wu4539b082012-03-01 12:57:46 +08002210 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2211 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002212
Alex Wu4539b082012-03-01 12:57:46 +08002213 wl_list_remove(&shsurf->fullscreen.transform.link);
2214 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002215
Jason Ekstranda7af7042013-10-12 22:38:11 -05002216 if (shsurf->fullscreen.black_view)
2217 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2218 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002219
Jason Ekstranda7af7042013-10-12 22:38:11 -05002220 weston_view_set_position(shsurf->view,
2221 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002222 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002223 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002224 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002225 shsurf->saved_rotation_valid = false;
2226 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002227
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002228 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002229}
2230
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002231static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002232shell_surface_set_fullscreen(struct wl_client *client,
2233 struct wl_resource *resource,
2234 uint32_t method,
2235 uint32_t framerate,
2236 struct wl_resource *output_resource)
2237{
2238 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2239 struct weston_output *output;
2240
2241 if (output_resource)
2242 output = wl_resource_get_user_data(output_resource);
2243 else
2244 output = NULL;
2245
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002246 shell_surface_set_parent(shsurf, NULL);
2247
Rafael Antognolli03b16592013-12-03 15:35:42 -02002248 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002249 set_fullscreen(shsurf, method, framerate, output);
2250}
2251
2252static void
2253set_popup(struct shell_surface *shsurf,
2254 struct weston_surface *parent,
2255 struct weston_seat *seat,
2256 uint32_t serial,
2257 int32_t x,
2258 int32_t y)
2259{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002260 assert(parent != NULL);
2261
Philip Withnallbecb77e2013-11-25 18:01:30 +00002262 shsurf->popup.shseat = get_shell_seat(seat);
2263 shsurf->popup.serial = serial;
2264 shsurf->popup.x = x;
2265 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002266
Philip Withnallb995e1d2013-11-25 18:01:39 +00002267 shsurf->next_type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002268}
2269
2270static void
2271shell_surface_set_popup(struct wl_client *client,
2272 struct wl_resource *resource,
2273 struct wl_resource *seat_resource,
2274 uint32_t serial,
2275 struct wl_resource *parent_resource,
2276 int32_t x, int32_t y, uint32_t flags)
2277{
2278 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002279 struct weston_surface *parent =
2280 wl_resource_get_user_data(parent_resource);
2281
2282 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002283
Rafael Antognolli03b16592013-12-03 15:35:42 -02002284 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002285 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002286 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002287 wl_resource_get_user_data(seat_resource),
2288 serial, x, y);
2289}
2290
2291static void
2292set_maximized(struct shell_surface *shsurf,
2293 struct weston_output *output)
2294{
2295 struct desktop_shell *shell;
2296 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002297
Philip Withnall352e7ed2013-11-25 18:01:35 +00002298 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002299
2300 shell = shell_surface_get_shell(shsurf);
2301 panel_height = get_output_panel_height(shell, shsurf->output);
2302 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2303
2304 shsurf->client->send_configure(shsurf->surface, edges,
2305 shsurf->output->width,
2306 shsurf->output->height - panel_height);
2307
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08002308 shsurf->next_state.maximized = true;
2309 shsurf->state_changed = true;
Rafael Antognollidb59f9a2013-12-05 18:53:17 -02002310 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002311}
2312
2313static void
2314unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002315{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002316 /* undo all maximized things here */
2317 shsurf->output = get_default_output(shsurf->surface->compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002318 weston_view_set_position(shsurf->view,
2319 shsurf->saved_x,
2320 shsurf->saved_y);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002321
2322 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002323 wl_list_insert(&shsurf->view->geometry.transformation_list,
2324 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002325 shsurf->saved_rotation_valid = false;
2326 }
2327
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002328 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002329}
2330
Philip Withnallbecb77e2013-11-25 18:01:30 +00002331static void
2332shell_surface_set_maximized(struct wl_client *client,
2333 struct wl_resource *resource,
2334 struct wl_resource *output_resource)
2335{
2336 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2337 struct weston_output *output;
2338
2339 if (output_resource)
2340 output = wl_resource_get_user_data(output_resource);
2341 else
2342 output = NULL;
2343
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002344 shell_surface_set_parent(shsurf, NULL);
2345
Rafael Antognolli03b16592013-12-03 15:35:42 -02002346 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002347 set_maximized(shsurf, output);
2348}
2349
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002350/* This is only ever called from set_surface_type(), so there’s no need to
2351 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002352static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002353reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002354{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002355 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002356 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002357 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002358 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002359
2360 surface->type = SHELL_SURFACE_NONE;
2361 return 0;
2362}
2363
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002364static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002365set_full_output(struct shell_surface *shsurf)
2366{
2367 shsurf->saved_x = shsurf->view->geometry.x;
2368 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002369 shsurf->saved_width = shsurf->surface->width;
2370 shsurf->saved_height = shsurf->surface->height;
2371 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002372 shsurf->saved_position_valid = true;
2373
2374 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2375 wl_list_remove(&shsurf->rotation.transform.link);
2376 wl_list_init(&shsurf->rotation.transform.link);
2377 weston_view_geometry_dirty(shsurf->view);
2378 shsurf->saved_rotation_valid = true;
2379 }
2380}
2381
2382static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002383set_surface_type(struct shell_surface *shsurf)
2384{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002385 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002386 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002387
Philip Withnallbecb77e2013-11-25 18:01:30 +00002388 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002389
2390 shsurf->type = shsurf->next_type;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002391 shsurf->state = shsurf->next_state;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002392 shsurf->next_type = SHELL_SURFACE_NONE;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002393 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002394
2395 switch (shsurf->type) {
2396 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002397 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002398 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002399 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002400 weston_view_set_position(shsurf->view,
2401 pev->geometry.x + shsurf->transient.x,
2402 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002403 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002404 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002405
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002406 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002407 weston_view_set_position(shsurf->view, shsurf->transient.x,
2408 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002409 break;
2410
Philip Withnall0f640e22013-11-25 18:01:31 +00002411 case SHELL_SURFACE_POPUP:
2412 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002413 default:
2414 break;
2415 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002416
2417 /* Update the surface’s layer. */
2418 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002419}
2420
Tiago Vignattibe143262012-04-16 17:31:41 +03002421static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002422shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002423{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002424 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002425}
2426
Alex Wu21858432012-04-01 20:13:08 +08002427static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002428black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002429
Jason Ekstranda7af7042013-10-12 22:38:11 -05002430static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002431create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002432 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002433 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002434{
2435 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002436 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002437
2438 surface = weston_surface_create(ec);
2439 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002440 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002441 return NULL;
2442 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002443 view = weston_view_create(surface);
2444 if (surface == NULL) {
2445 weston_log("no memory\n");
2446 weston_surface_destroy(surface);
2447 return NULL;
2448 }
Alex Wu4539b082012-03-01 12:57:46 +08002449
Alex Wu21858432012-04-01 20:13:08 +08002450 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002451 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002452 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002453 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002454 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002455 pixman_region32_fini(&surface->input);
2456 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002457
Xiong Zhangbecf5a32013-11-29 11:18:14 +08002458 surface->width = w;
2459 surface->height = h;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002460 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002461
2462 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002463}
2464
Philip Withnalled8f1a92013-11-25 18:01:43 +00002465static void
2466shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2467{
2468 struct weston_output *output = shsurf->fullscreen_output;
2469
Rafael Antognolli03b16592013-12-03 15:35:42 -02002470 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002471
2472 if (!shsurf->fullscreen.black_view)
2473 shsurf->fullscreen.black_view =
2474 create_black_surface(shsurf->surface->compositor,
2475 shsurf->surface,
2476 output->x, output->y,
2477 output->width,
2478 output->height);
2479
2480 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2481 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2482 wl_list_insert(&shsurf->view->layer_link,
2483 &shsurf->fullscreen.black_view->layer_link);
2484 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2485 weston_surface_damage(shsurf->surface);
2486}
2487
Alex Wu4539b082012-03-01 12:57:46 +08002488/* Create black surface and append it to the associated fullscreen surface.
2489 * Handle size dismatch and positioning according to the method. */
2490static void
2491shell_configure_fullscreen(struct shell_surface *shsurf)
2492{
2493 struct weston_output *output = shsurf->fullscreen_output;
2494 struct weston_surface *surface = shsurf->surface;
2495 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002496 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002497 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002498
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002499 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2500 restore_output_mode(output);
2501
Philip Withnalled8f1a92013-11-25 18:01:43 +00002502 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002503
Jason Ekstranda7af7042013-10-12 22:38:11 -05002504 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002505 &surf_width, &surf_height);
2506
Alex Wu4539b082012-03-01 12:57:46 +08002507 switch (shsurf->fullscreen.type) {
2508 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002509 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002510 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002511 break;
2512 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002513 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002514 if (output->width == surf_width &&
2515 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002516 weston_view_set_position(shsurf->view,
2517 output->x - surf_x,
2518 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002519 break;
2520 }
2521
Alex Wu4539b082012-03-01 12:57:46 +08002522 matrix = &shsurf->fullscreen.transform.matrix;
2523 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002524
Scott Moreau1bad5db2012-08-18 01:04:05 -06002525 output_aspect = (float) output->width /
2526 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002527 /* XXX: Use surf_width and surf_height here? */
2528 surface_aspect = (float) surface->width /
2529 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002530 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002531 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002532 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002533 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002534 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002535 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002536
Alex Wu4539b082012-03-01 12:57:46 +08002537 weston_matrix_scale(matrix, scale, scale, 1);
2538 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002539 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002540 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002541 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2542 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002543 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002544
Alex Wu4539b082012-03-01 12:57:46 +08002545 break;
2546 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002547 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002548 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002549 surf_width * surface->buffer_viewport.scale,
2550 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002551 shsurf->fullscreen.framerate};
2552
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002553 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002554 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002555 weston_view_set_position(shsurf->view,
2556 output->x - surf_x,
2557 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002558 shsurf->fullscreen.black_view->surface->width = output->width;
2559 shsurf->fullscreen.black_view->surface->height = output->height;
2560 weston_view_set_position(shsurf->fullscreen.black_view,
2561 output->x - surf_x,
2562 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002563 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002564 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002565 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002566 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002567 }
Alex Wubd3354b2012-04-17 17:20:49 +08002568 }
Alex Wu4539b082012-03-01 12:57:46 +08002569 break;
2570 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002571 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002572 break;
2573 default:
2574 break;
2575 }
2576}
2577
Alex Wu4539b082012-03-01 12:57:46 +08002578static void
2579shell_map_fullscreen(struct shell_surface *shsurf)
2580{
Alex Wubd3354b2012-04-17 17:20:49 +08002581 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002582}
2583
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002584static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002585set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2586{
2587 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002588 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002589 shsurf->transient.x = x;
2590 shsurf->transient.y = y;
2591 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002592
2593 shell_surface_set_parent(shsurf, NULL);
2594
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002595 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2596}
2597
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002598static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002599
2600static void
2601destroy_shell_seat(struct wl_listener *listener, void *data)
2602{
2603 struct shell_seat *shseat =
2604 container_of(listener,
2605 struct shell_seat, seat_destroy_listener);
2606 struct shell_surface *shsurf, *prev = NULL;
2607
2608 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002609 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002610 shseat->popup_grab.client = NULL;
2611
2612 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2613 shsurf->popup.shseat = NULL;
2614 if (prev) {
2615 wl_list_init(&prev->popup.grab_link);
2616 }
2617 prev = shsurf;
2618 }
2619 wl_list_init(&prev->popup.grab_link);
2620 }
2621
2622 wl_list_remove(&shseat->seat_destroy_listener.link);
2623 free(shseat);
2624}
2625
2626static struct shell_seat *
2627create_shell_seat(struct weston_seat *seat)
2628{
2629 struct shell_seat *shseat;
2630
2631 shseat = calloc(1, sizeof *shseat);
2632 if (!shseat) {
2633 weston_log("no memory to allocate shell seat\n");
2634 return NULL;
2635 }
2636
2637 shseat->seat = seat;
2638 wl_list_init(&shseat->popup_grab.surfaces_list);
2639
2640 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2641 wl_signal_add(&seat->destroy_signal,
2642 &shseat->seat_destroy_listener);
2643
2644 return shseat;
2645}
2646
2647static struct shell_seat *
2648get_shell_seat(struct weston_seat *seat)
2649{
2650 struct wl_listener *listener;
2651
2652 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2653 if (listener == NULL)
2654 return create_shell_seat(seat);
2655
2656 return container_of(listener,
2657 struct shell_seat, seat_destroy_listener);
2658}
2659
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002660static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002661popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002662{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002663 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002664 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002665 struct shell_seat *shseat =
2666 container_of(grab, struct shell_seat, popup_grab.grab);
2667 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002668 wl_fixed_t sx, sy;
2669
Jason Ekstranda7af7042013-10-12 22:38:11 -05002670 view = weston_compositor_pick_view(pointer->seat->compositor,
2671 pointer->x, pointer->y,
2672 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002673
Jason Ekstranda7af7042013-10-12 22:38:11 -05002674 if (view && view->surface->resource &&
2675 wl_resource_get_client(view->surface->resource) == client) {
2676 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002677 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002678 weston_pointer_set_focus(pointer, NULL,
2679 wl_fixed_from_int(0),
2680 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002681 }
2682}
2683
2684static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002685popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2686 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002687{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002688 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002689 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002690 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002691
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002692 weston_pointer_move(pointer, x, y);
2693
Neil Roberts96d790e2013-09-19 17:32:00 +01002694 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002695 weston_view_from_global_fixed(pointer->focus,
2696 pointer->x, pointer->y,
2697 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002698 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002699 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002700}
2701
2702static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002703popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002704 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002705{
2706 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002707 struct shell_seat *shseat =
2708 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002709 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002710 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002711 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002712 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002713
Neil Roberts96d790e2013-09-19 17:32:00 +01002714 resource_list = &grab->pointer->focus_resource_list;
2715 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002716 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002717 wl_resource_for_each(resource, resource_list) {
2718 wl_pointer_send_button(resource, serial,
2719 time, button, state);
2720 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002721 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002722 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002723 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002724 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002725 }
2726
Daniel Stone4dbadb12012-05-30 16:31:51 +01002727 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002728 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002729}
2730
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002731static void
2732popup_grab_cancel(struct weston_pointer_grab *grab)
2733{
2734 popup_grab_end(grab->pointer);
2735}
2736
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002737static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002738 popup_grab_focus,
2739 popup_grab_motion,
2740 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002741 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002742};
2743
2744static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002745shell_surface_send_popup_done(struct shell_surface *shsurf)
2746{
2747 if (shell_surface_is_wl_shell_surface(shsurf))
2748 wl_shell_surface_send_popup_done(shsurf->resource);
2749 else if (shell_surface_is_xdg_popup(shsurf))
2750 xdg_popup_send_popup_done(shsurf->resource,
2751 shsurf->popup.serial);
2752}
2753
2754static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002755popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002756{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002757 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002758 struct shell_seat *shseat =
2759 container_of(grab, struct shell_seat, popup_grab.grab);
2760 struct shell_surface *shsurf;
2761 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002762
2763 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002764 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002765 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002766 shseat->popup_grab.grab.interface = NULL;
2767 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002768 /* Send the popup_done event to all the popups open */
2769 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002770 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002771 shsurf->popup.shseat = NULL;
2772 if (prev) {
2773 wl_list_init(&prev->popup.grab_link);
2774 }
2775 prev = shsurf;
2776 }
2777 wl_list_init(&prev->popup.grab_link);
2778 wl_list_init(&shseat->popup_grab.surfaces_list);
2779 }
2780}
2781
2782static void
2783add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2784{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002785 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002786
2787 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002788 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002789 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002790 /* We must make sure here that this popup was opened after
2791 * a mouse press, and not just by moving around with other
2792 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002793 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002794 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002795
Rob Bradforddfe31052013-06-26 19:49:11 +01002796 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002797 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002798 } else {
2799 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002800 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002801}
2802
2803static void
2804remove_popup_grab(struct shell_surface *shsurf)
2805{
2806 struct shell_seat *shseat = shsurf->popup.shseat;
2807
2808 wl_list_remove(&shsurf->popup.grab_link);
2809 wl_list_init(&shsurf->popup.grab_link);
2810 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002811 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002812 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002813 }
2814}
2815
2816static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002817shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002818{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002819 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002820 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002821
Jason Ekstranda7af7042013-10-12 22:38:11 -05002822 shsurf->surface->output = parent_view->output;
2823 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002824
Jason Ekstranda7af7042013-10-12 22:38:11 -05002825 weston_view_set_transform_parent(shsurf->view, parent_view);
2826 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2827 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002828
Kristian Høgsberge3148752013-05-06 23:19:49 -04002829 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002830 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002831 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002832 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002833 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002834 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002835}
2836
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002837static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002838 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002839 shell_surface_move,
2840 shell_surface_resize,
2841 shell_surface_set_toplevel,
2842 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002843 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002844 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002845 shell_surface_set_maximized,
2846 shell_surface_set_title,
2847 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002848};
2849
2850static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002851destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002852{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002853 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2854
Giulio Camuffo5085a752013-03-25 21:42:45 +01002855 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2856 remove_popup_grab(shsurf);
2857 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002858
Alex Wubd3354b2012-04-17 17:20:49 +08002859 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002860 shell_surface_is_top_fullscreen(shsurf))
2861 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002862
Jason Ekstranda7af7042013-10-12 22:38:11 -05002863 if (shsurf->fullscreen.black_view)
2864 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08002865
Alex Wubd3354b2012-04-17 17:20:49 +08002866 /* As destroy_resource() use wl_list_for_each_safe(),
2867 * we can always remove the listener.
2868 */
2869 wl_list_remove(&shsurf->surface_destroy_listener.link);
2870 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002871 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002872 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002873
Jason Ekstranda7af7042013-10-12 22:38:11 -05002874 weston_view_destroy(shsurf->view);
2875
Philip Withnall648a4dd2013-11-25 18:01:44 +00002876 wl_list_remove(&shsurf->children_link);
2877
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002878 wl_list_remove(&shsurf->link);
2879 free(shsurf);
2880}
2881
2882static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002883shell_destroy_shell_surface(struct wl_resource *resource)
2884{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002885 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002886
2887 destroy_shell_surface(shsurf);
2888}
2889
2890static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002891shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002892{
2893 struct shell_surface *shsurf = container_of(listener,
2894 struct shell_surface,
2895 surface_destroy_listener);
2896
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002897 if (shsurf->resource)
2898 wl_resource_destroy(shsurf->resource);
2899 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03002900 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002901}
2902
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002903static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002904shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002905
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002906struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002907get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002908{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002909 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002910 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002911 else
2912 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002913}
2914
Rafael Antognollie2a34552013-12-03 15:35:45 -02002915static struct shell_surface *
2916create_common_surface(void *shell, struct weston_surface *surface,
2917 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002918{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002919 struct shell_surface *shsurf;
2920
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002921 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002922 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002923 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002924 }
2925
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002926 shsurf = calloc(1, sizeof *shsurf);
2927 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002928 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002929 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002930 }
2931
Jason Ekstranda7af7042013-10-12 22:38:11 -05002932 shsurf->view = weston_view_create(surface);
2933 if (!shsurf->view) {
2934 weston_log("no memory to allocate shell surface\n");
2935 free(shsurf);
2936 return NULL;
2937 }
2938
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002939 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002940 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002941
Tiago Vignattibc052c92012-04-19 16:18:18 +03002942 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002943 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002944 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002945 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002946 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002947 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002948 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2949 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002950 shsurf->fullscreen.black_view = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002951 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002952 wl_list_init(&shsurf->fullscreen.transform.link);
2953
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002954 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002955 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002956 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002957 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002958
2959 /* init link so its safe to always remove it in destroy_shell_surface */
2960 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002961 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002962
Pekka Paalanen460099f2012-01-20 16:48:25 +02002963 /* empty when not in use */
2964 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002965 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002966
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002967 wl_list_init(&shsurf->workspace_transform.link);
2968
Philip Withnall648a4dd2013-11-25 18:01:44 +00002969 wl_list_init(&shsurf->children_link);
2970 wl_list_init(&shsurf->children_list);
2971 shsurf->parent = NULL;
2972
Pekka Paalanen98262232011-12-01 10:42:22 +02002973 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002974 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002975
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002976 shsurf->client = client;
2977
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002978 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002979}
2980
Rafael Antognollie2a34552013-12-03 15:35:45 -02002981static struct shell_surface *
2982create_shell_surface(void *shell, struct weston_surface *surface,
2983 const struct weston_shell_client *client)
2984{
2985 struct shell_surface *shsurf;
2986 shsurf = create_common_surface(shell, surface, client);
2987
2988 shsurf->type = SHELL_SURFACE_NONE;
2989 shsurf->next_type = SHELL_SURFACE_NONE;
2990
2991 return shsurf;
2992}
2993
Jason Ekstranda7af7042013-10-12 22:38:11 -05002994static struct weston_view *
2995get_primary_view(void *shell, struct shell_surface *shsurf)
2996{
2997 return shsurf->view;
2998}
2999
Tiago Vignattibc052c92012-04-19 16:18:18 +03003000static void
3001shell_get_shell_surface(struct wl_client *client,
3002 struct wl_resource *resource,
3003 uint32_t id,
3004 struct wl_resource *surface_resource)
3005{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003006 struct weston_surface *surface =
3007 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003008 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003009 struct shell_surface *shsurf;
3010
3011 if (get_shell_surface(surface)) {
3012 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003013 WL_DISPLAY_ERROR_INVALID_OBJECT,
3014 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003015 return;
3016 }
3017
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003018 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003019 if (!shsurf) {
3020 wl_resource_post_error(surface_resource,
3021 WL_DISPLAY_ERROR_INVALID_OBJECT,
3022 "surface->configure already set");
3023 return;
3024 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003025
Jason Ekstranda85118c2013-06-27 20:17:02 -05003026 shsurf->resource =
3027 wl_resource_create(client,
3028 &wl_shell_surface_interface, 1, id);
3029 wl_resource_set_implementation(shsurf->resource,
3030 &shell_surface_implementation,
3031 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003032}
3033
Rafael Antognollie2a34552013-12-03 15:35:45 -02003034static bool
3035shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3036{
3037 return wl_resource_instance_of(shsurf->resource,
3038 &wl_shell_surface_interface,
3039 &shell_surface_implementation);
3040}
3041
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003042static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003043 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003044};
3045
Rafael Antognollie2a34552013-12-03 15:35:45 -02003046/****************************
3047 * xdg-shell implementation */
3048
3049static void
3050xdg_surface_destroy(struct wl_client *client,
3051 struct wl_resource *resource)
3052{
3053 wl_resource_destroy(resource);
3054}
3055
3056static void
3057xdg_surface_pong(struct wl_client *client,
3058 struct wl_resource *resource,
3059 uint32_t serial)
3060{
3061 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3062
3063 surface_pong(shsurf, serial);
3064}
3065
3066static void
3067xdg_surface_set_app_id(struct wl_client *client,
3068 struct wl_resource *resource,
3069 const char *app_id)
3070{
3071 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3072
3073 free(shsurf->class);
3074 shsurf->class = strdup(app_id);
3075}
3076
3077static void
3078xdg_surface_set_title(struct wl_client *client,
3079 struct wl_resource *resource, const char *title)
3080{
3081 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3082
3083 set_title(shsurf, title);
3084}
3085
3086static void
3087xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3088 struct wl_resource *seat_resource, uint32_t serial)
3089{
3090 common_surface_move(resource, seat_resource, serial);
3091}
3092
3093static void
3094xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3095 struct wl_resource *seat_resource, uint32_t serial,
3096 uint32_t edges)
3097{
3098 common_surface_resize(resource, seat_resource, serial, edges);
3099}
3100
3101static void
3102xdg_surface_set_output(struct wl_client *client,
3103 struct wl_resource *resource,
3104 struct wl_resource *output_resource)
3105{
3106 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3107 struct weston_output *output;
3108
3109 if (output_resource)
3110 output = wl_resource_get_user_data(output_resource);
3111 else
3112 output = NULL;
3113
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003114 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003115}
3116
3117static void
3118xdg_surface_set_fullscreen(struct wl_client *client,
3119 struct wl_resource *resource)
3120{
3121 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3122
3123 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3124 return;
3125
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08003126 if (!shsurf->next_state.fullscreen)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003127 set_fullscreen(shsurf,
3128 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003129 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003130}
3131
3132static void
3133xdg_surface_unset_fullscreen(struct wl_client *client,
3134 struct wl_resource *resource)
3135{
3136 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003137 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003138
3139 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3140 return;
3141
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003142 if (!shsurf->next_state.fullscreen)
3143 return;
3144
Rafael Antognollie2a34552013-12-03 15:35:45 -02003145 shsurf->next_state.fullscreen = false;
3146 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003147
3148 if (shsurf->saved_size_valid) {
3149 width = shsurf->saved_width;
3150 height = shsurf->saved_height;
3151 shsurf->saved_size_valid = false;
3152 } else {
3153 width = shsurf->surface->width;
3154 height = shsurf->surface->height;
3155 }
3156
3157 shsurf->client->send_configure(shsurf->surface, 0, width, height);
3158 shsurf->next_type = shsurf->type;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003159}
3160
3161static void
3162xdg_surface_set_maximized(struct wl_client *client,
3163 struct wl_resource *resource)
3164{
3165 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3166
3167 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3168 return;
3169
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08003170 if (!shsurf->next_state.maximized)
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003171 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003172}
3173
3174static void
3175xdg_surface_unset_maximized(struct wl_client *client,
3176 struct wl_resource *resource)
3177{
3178 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003179 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003180
3181 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3182 return;
3183
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003184 if (!shsurf->next_state.maximized)
3185 return;
3186
Rafael Antognollie2a34552013-12-03 15:35:45 -02003187 shsurf->next_state.maximized = false;
3188 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003189
3190 if (shsurf->saved_size_valid) {
3191 width = shsurf->saved_width;
3192 height = shsurf->saved_height;
3193 shsurf->saved_size_valid = false;
3194 } else {
3195 width = shsurf->surface->width;
3196 height = shsurf->surface->height;
3197 }
3198
3199 shsurf->client->send_configure(shsurf->surface, 0, width, height);
3200 shsurf->next_type = shsurf->type;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003201}
3202
3203static const struct xdg_surface_interface xdg_surface_implementation = {
3204 xdg_surface_destroy,
3205 xdg_surface_set_transient_for,
3206 xdg_surface_set_title,
3207 xdg_surface_set_app_id,
3208 xdg_surface_pong,
3209 xdg_surface_move,
3210 xdg_surface_resize,
3211 xdg_surface_set_output,
3212 xdg_surface_set_fullscreen,
3213 xdg_surface_unset_fullscreen,
3214 xdg_surface_set_maximized,
3215 xdg_surface_unset_maximized,
3216 NULL /* set_minimized */
3217};
3218
3219static void
3220xdg_send_configure(struct weston_surface *surface,
3221 uint32_t edges, int32_t width, int32_t height)
3222{
3223 struct shell_surface *shsurf = get_shell_surface(surface);
3224
3225 xdg_surface_send_configure(shsurf->resource, edges, width, height);
3226}
3227
3228static const struct weston_shell_client xdg_client = {
3229 xdg_send_configure
3230};
3231
3232static void
3233xdg_use_unstable_version(struct wl_client *client,
3234 struct wl_resource *resource,
3235 int32_t version)
3236{
3237 if (version > 1) {
3238 wl_resource_post_error(resource,
3239 1,
3240 "xdg-shell:: version not implemented yet.");
3241 return;
3242 }
3243}
3244
3245static struct shell_surface *
3246create_xdg_surface(void *shell, struct weston_surface *surface,
3247 const struct weston_shell_client *client)
3248{
3249 struct shell_surface *shsurf;
3250 shsurf = create_common_surface(shell, surface, client);
3251
3252 shsurf->type = SHELL_SURFACE_NONE;
3253 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
3254
3255 return shsurf;
3256}
3257
3258static void
3259xdg_get_xdg_surface(struct wl_client *client,
3260 struct wl_resource *resource,
3261 uint32_t id,
3262 struct wl_resource *surface_resource)
3263{
3264 struct weston_surface *surface =
3265 wl_resource_get_user_data(surface_resource);
3266 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3267 struct shell_surface *shsurf;
3268
3269 if (get_shell_surface(surface)) {
3270 wl_resource_post_error(surface_resource,
3271 WL_DISPLAY_ERROR_INVALID_OBJECT,
3272 "desktop_shell::get_shell_surface already requested");
3273 return;
3274 }
3275
3276 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3277 if (!shsurf) {
3278 wl_resource_post_error(surface_resource,
3279 WL_DISPLAY_ERROR_INVALID_OBJECT,
3280 "surface->configure already set");
3281 return;
3282 }
3283
3284 shsurf->resource =
3285 wl_resource_create(client,
3286 &xdg_surface_interface, 1, id);
3287 wl_resource_set_implementation(shsurf->resource,
3288 &xdg_surface_implementation,
3289 shsurf, shell_destroy_shell_surface);
3290}
3291
3292static bool
3293shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3294{
3295 return wl_resource_instance_of(shsurf->resource,
3296 &xdg_surface_interface,
3297 &xdg_surface_implementation);
3298}
3299
3300/* xdg-popup implementation */
3301
3302static void
3303xdg_popup_destroy(struct wl_client *client,
3304 struct wl_resource *resource)
3305{
3306 wl_resource_destroy(resource);
3307}
3308
3309static void
3310xdg_popup_pong(struct wl_client *client,
3311 struct wl_resource *resource,
3312 uint32_t serial)
3313{
3314 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3315
3316 surface_pong(shsurf, serial);
3317}
3318
3319static const struct xdg_popup_interface xdg_popup_implementation = {
3320 xdg_popup_destroy,
3321 xdg_popup_pong
3322};
3323
3324static void
3325xdg_popup_send_configure(struct weston_surface *surface,
3326 uint32_t edges, int32_t width, int32_t height)
3327{
3328}
3329
3330static const struct weston_shell_client xdg_popup_client = {
3331 xdg_popup_send_configure
3332};
3333
3334static struct shell_surface *
3335create_xdg_popup(void *shell, struct weston_surface *surface,
3336 const struct weston_shell_client *client,
3337 struct weston_surface *parent,
3338 struct shell_seat *seat,
3339 uint32_t serial,
3340 int32_t x, int32_t y)
3341{
3342 struct shell_surface *shsurf;
3343 shsurf = create_common_surface(shell, surface, client);
3344
3345 shsurf->type = SHELL_SURFACE_NONE;
3346 shsurf->next_type = SHELL_SURFACE_POPUP;
3347 shsurf->popup.shseat = seat;
3348 shsurf->popup.serial = serial;
3349 shsurf->popup.x = x;
3350 shsurf->popup.y = y;
3351 shell_surface_set_parent(shsurf, parent);
3352
3353 return shsurf;
3354}
3355
3356static void
3357xdg_get_xdg_popup(struct wl_client *client,
3358 struct wl_resource *resource,
3359 uint32_t id,
3360 struct wl_resource *surface_resource,
3361 struct wl_resource *parent_resource,
3362 struct wl_resource *seat_resource,
3363 uint32_t serial,
3364 int32_t x, int32_t y, uint32_t flags)
3365{
3366 struct weston_surface *surface =
3367 wl_resource_get_user_data(surface_resource);
3368 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3369 struct shell_surface *shsurf;
3370 struct weston_surface *parent;
3371 struct shell_seat *seat;
3372
3373 if (get_shell_surface(surface)) {
3374 wl_resource_post_error(surface_resource,
3375 WL_DISPLAY_ERROR_INVALID_OBJECT,
3376 "desktop_shell::get_shell_surface already requested");
3377 return;
3378 }
3379
3380 if (!parent_resource) {
3381 wl_resource_post_error(surface_resource,
3382 WL_DISPLAY_ERROR_INVALID_OBJECT,
3383 "xdg_shell::get_xdg_popup requires a parent shell surface");
3384 }
3385
3386 parent = wl_resource_get_user_data(parent_resource);
3387 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3388
3389 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3390 parent, seat, serial, x, y);
3391 if (!shsurf) {
3392 wl_resource_post_error(surface_resource,
3393 WL_DISPLAY_ERROR_INVALID_OBJECT,
3394 "surface->configure already set");
3395 return;
3396 }
3397
3398 shsurf->resource =
3399 wl_resource_create(client,
3400 &xdg_popup_interface, 1, id);
3401 wl_resource_set_implementation(shsurf->resource,
3402 &xdg_popup_implementation,
3403 shsurf, shell_destroy_shell_surface);
3404}
3405
3406static bool
3407shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3408{
3409 return wl_resource_instance_of(shsurf->resource,
3410 &xdg_popup_interface,
3411 &xdg_popup_implementation);
3412}
3413
3414static const struct xdg_shell_interface xdg_implementation = {
3415 xdg_use_unstable_version,
3416 xdg_get_xdg_surface,
3417 xdg_get_xdg_popup
3418};
3419
3420static int
3421xdg_shell_unversioned_dispatch(const void *implementation,
3422 void *_target, uint32_t opcode,
3423 const struct wl_message *message,
3424 union wl_argument *args)
3425{
3426 struct wl_resource *resource = _target;
3427 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3428
3429 if (opcode != 0) {
3430 wl_resource_post_error(resource,
3431 WL_DISPLAY_ERROR_INVALID_OBJECT,
3432 "must call use_unstable_version first");
3433 return 0;
3434 }
3435
3436#define XDG_SERVER_VERSION 1
3437
3438 if (args[0].i != XDG_SERVER_VERSION) {
3439 wl_resource_post_error(resource,
3440 WL_DISPLAY_ERROR_INVALID_OBJECT,
3441 "incompatible version, server is %d "
3442 "client wants %d",
3443 XDG_SERVER_VERSION, args[0].i);
3444 return 0;
3445 }
3446
3447 wl_resource_set_implementation(resource, &xdg_implementation,
3448 shell, NULL);
3449
3450 return 1;
3451}
3452
3453/* end of xdg-shell implementation */
3454/***********************************/
3455
Kristian Høgsberg07937562011-04-12 17:25:42 -04003456static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003457shell_fade(struct desktop_shell *shell, enum fade_type type);
3458
3459static int
3460screensaver_timeout(void *data)
3461{
3462 struct desktop_shell *shell = data;
3463
3464 shell_fade(shell, FADE_OUT);
3465
3466 return 1;
3467}
3468
3469static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003470handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003471{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003472 struct desktop_shell *shell =
3473 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003474
Pekka Paalanen18027e52011-12-02 16:31:49 +02003475 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003476
3477 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003478 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003479}
3480
3481static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003482launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003483{
3484 if (shell->screensaver.binding)
3485 return;
3486
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003487 if (!shell->screensaver.path) {
3488 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003489 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003490 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003491
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003492 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003493 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003494 return;
3495 }
3496
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003497 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003498 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003499 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003500 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003501}
3502
3503static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003504terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003505{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003506 if (shell->screensaver.process.pid == 0)
3507 return;
3508
3509 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003510}
3511
3512static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003513configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003514{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003515 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003516
Jason Ekstranda7af7042013-10-12 22:38:11 -05003517 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3518 if (v->output == ev->output && v != ev) {
3519 weston_view_unmap(v);
3520 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003521 }
3522 }
3523
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003524 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003525
Jason Ekstranda7af7042013-10-12 22:38:11 -05003526 if (wl_list_empty(&ev->layer_link)) {
3527 wl_list_insert(&layer->view_list, &ev->layer_link);
3528 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003529 }
3530}
3531
3532static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003533background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003534{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003535 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003536 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003537
Jason Ekstranda7af7042013-10-12 22:38:11 -05003538 view = container_of(es->views.next, struct weston_view, surface_link);
3539
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003540 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003541}
3542
3543static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003544desktop_shell_set_background(struct wl_client *client,
3545 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003546 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003547 struct wl_resource *surface_resource)
3548{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003549 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003550 struct weston_surface *surface =
3551 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003552 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003553
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003554 if (surface->configure) {
3555 wl_resource_post_error(surface_resource,
3556 WL_DISPLAY_ERROR_INVALID_OBJECT,
3557 "surface role already assigned");
3558 return;
3559 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003560
Jason Ekstranda7af7042013-10-12 22:38:11 -05003561 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3562 weston_view_destroy(view);
3563 view = weston_view_create(surface);
3564
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003565 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003566 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003567 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003568 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003569 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003570 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003571 surface->output->width,
3572 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003573}
3574
3575static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003576panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003577{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003578 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003579 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003580
Jason Ekstranda7af7042013-10-12 22:38:11 -05003581 view = container_of(es->views.next, struct weston_view, surface_link);
3582
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003583 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003584}
3585
3586static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003587desktop_shell_set_panel(struct wl_client *client,
3588 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003589 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003590 struct wl_resource *surface_resource)
3591{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003592 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003593 struct weston_surface *surface =
3594 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003595 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003596
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003597 if (surface->configure) {
3598 wl_resource_post_error(surface_resource,
3599 WL_DISPLAY_ERROR_INVALID_OBJECT,
3600 "surface role already assigned");
3601 return;
3602 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003603
Jason Ekstranda7af7042013-10-12 22:38:11 -05003604 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3605 weston_view_destroy(view);
3606 view = weston_view_create(surface);
3607
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003608 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003609 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003610 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003611 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003612 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003613 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003614 surface->output->width,
3615 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003616}
3617
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003618static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003619lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003620{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003621 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003622 struct weston_view *view;
3623
3624 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003625
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003626 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003627 return;
3628
Jason Ekstranda7af7042013-10-12 22:38:11 -05003629 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003630
3631 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003632 wl_list_insert(&shell->lock_layer.view_list,
3633 &view->layer_link);
3634 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003635 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003636 }
3637}
3638
3639static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003640handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003641{
Tiago Vignattibe143262012-04-16 17:31:41 +03003642 struct desktop_shell *shell =
3643 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003644
Martin Minarik6d118362012-06-07 18:01:59 +02003645 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003646 shell->lock_surface = NULL;
3647}
3648
3649static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003650desktop_shell_set_lock_surface(struct wl_client *client,
3651 struct wl_resource *resource,
3652 struct wl_resource *surface_resource)
3653{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003654 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003655 struct weston_surface *surface =
3656 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003657
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003658 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003659
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003660 if (!shell->locked)
3661 return;
3662
Pekka Paalanen98262232011-12-01 10:42:22 +02003663 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003664
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003665 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003666 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003667 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003668
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003669 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003670 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003671 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003672}
3673
3674static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003675resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003676{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003677 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003678
Pekka Paalanen77346a62011-11-30 16:26:35 +02003679 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003680
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003681 wl_list_remove(&shell->lock_layer.link);
3682 wl_list_insert(&shell->compositor->cursor_layer.link,
3683 &shell->fullscreen_layer.link);
3684 wl_list_insert(&shell->fullscreen_layer.link,
3685 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003686 if (shell->showing_input_panels) {
3687 wl_list_insert(&shell->panel_layer.link,
3688 &shell->input_panel_layer.link);
3689 wl_list_insert(&shell->input_panel_layer.link,
3690 &ws->layer.link);
3691 } else {
3692 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3693 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003694
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003695 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003696
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003697 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003698 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003699 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003700}
3701
3702static void
3703desktop_shell_unlock(struct wl_client *client,
3704 struct wl_resource *resource)
3705{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003706 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003707
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003708 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003709
3710 if (shell->locked)
3711 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003712}
3713
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003714static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003715desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003716 struct wl_resource *resource,
3717 struct wl_resource *surface_resource)
3718{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003719 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003720
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003721 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003722 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003723}
3724
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003725static void
3726desktop_shell_desktop_ready(struct wl_client *client,
3727 struct wl_resource *resource)
3728{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003729 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003730
3731 shell_fade_startup(shell);
3732}
3733
Kristian Høgsberg75840622011-09-06 13:48:16 -04003734static const struct desktop_shell_interface desktop_shell_implementation = {
3735 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003736 desktop_shell_set_panel,
3737 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003738 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003739 desktop_shell_set_grab_surface,
3740 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003741};
3742
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003743static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003744get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003745{
3746 struct shell_surface *shsurf;
3747
3748 shsurf = get_shell_surface(surface);
3749 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003750 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003751 return shsurf->type;
3752}
3753
Kristian Høgsberg75840622011-09-06 13:48:16 -04003754static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003755move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003756{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003757 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003758 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003759 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003760
Pekka Paalanen01388e22013-04-25 13:57:44 +03003761 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003762 if (surface == NULL)
3763 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003764
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003765 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003766 if (shsurf == NULL || shsurf->state.fullscreen ||
3767 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003768 return;
3769
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003770 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003771}
3772
3773static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003774maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3775{
3776 struct weston_surface *focus = seat->pointer->focus->surface;
3777 struct weston_surface *surface;
3778 struct shell_surface *shsurf;
3779
3780 surface = weston_surface_get_main_surface(focus);
3781 if (surface == NULL)
3782 return;
3783
3784 shsurf = get_shell_surface(surface);
3785 if (shsurf == NULL)
3786 return;
3787
3788 if (!shell_surface_is_xdg_surface(shsurf))
3789 return;
3790
3791 if (shsurf->state.maximized)
3792 xdg_surface_send_request_unset_maximized(shsurf->resource);
3793 else
3794 xdg_surface_send_request_set_maximized(shsurf->resource);
3795}
3796
3797static void
3798fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3799{
3800 struct weston_surface *focus = seat->pointer->focus->surface;
3801 struct weston_surface *surface;
3802 struct shell_surface *shsurf;
3803
3804 surface = weston_surface_get_main_surface(focus);
3805 if (surface == NULL)
3806 return;
3807
3808 shsurf = get_shell_surface(surface);
3809 if (shsurf == NULL)
3810 return;
3811
3812 if (!shell_surface_is_xdg_surface(shsurf))
3813 return;
3814
3815 if (shsurf->state.fullscreen)
3816 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
3817 else
3818 xdg_surface_send_request_set_fullscreen(shsurf->resource);
3819}
3820
3821static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003822touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3823{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003824 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003825 struct weston_surface *surface;
3826 struct shell_surface *shsurf;
3827
3828 surface = weston_surface_get_main_surface(focus);
3829 if (surface == NULL)
3830 return;
3831
3832 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003833 if (shsurf == NULL || shsurf->state.fullscreen ||
3834 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003835 return;
3836
3837 surface_touch_move(shsurf, (struct weston_seat *) seat);
3838}
3839
3840static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003841resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003842{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003843 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003844 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003845 uint32_t edges = 0;
3846 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003847 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003848
Pekka Paalanen01388e22013-04-25 13:57:44 +03003849 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003850 if (surface == NULL)
3851 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003852
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003853 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003854 if (shsurf == NULL || shsurf->state.fullscreen ||
3855 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003856 return;
3857
Jason Ekstranda7af7042013-10-12 22:38:11 -05003858 weston_view_from_global(shsurf->view,
3859 wl_fixed_to_int(seat->pointer->grab_x),
3860 wl_fixed_to_int(seat->pointer->grab_y),
3861 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003862
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003863 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003864 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003865 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003866 edges |= 0;
3867 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003868 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003869
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003870 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003871 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003872 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003873 edges |= 0;
3874 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003875 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003876
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003877 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003878}
3879
3880static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003881surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003882 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003883{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003884 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003885 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003886 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003887 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003888
Pekka Paalanen01388e22013-04-25 13:57:44 +03003889 /* XXX: broken for windows containing sub-surfaces */
3890 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003891 if (surface == NULL)
3892 return;
3893
3894 shsurf = get_shell_surface(surface);
3895 if (!shsurf)
3896 return;
3897
Jason Ekstranda7af7042013-10-12 22:38:11 -05003898 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003899
Jason Ekstranda7af7042013-10-12 22:38:11 -05003900 if (shsurf->view->alpha > 1.0)
3901 shsurf->view->alpha = 1.0;
3902 if (shsurf->view->alpha < step)
3903 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003904
Jason Ekstranda7af7042013-10-12 22:38:11 -05003905 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003906 weston_surface_damage(surface);
3907}
3908
3909static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003910do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003911 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003912{
Daniel Stone37816df2012-05-16 18:45:18 +01003913 struct weston_seat *ws = (struct weston_seat *) seat;
3914 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003915 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003916 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003917
3918 wl_list_for_each(output, &compositor->output_list, link) {
3919 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01003920 wl_fixed_to_double(seat->pointer->x),
3921 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003922 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003923 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003924 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003925 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003926 increment = -output->zoom.increment;
3927 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003928 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003929 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003930 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003931 else
3932 increment = 0;
3933
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003934 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003935
Scott Moreaue6603982012-06-11 13:07:51 -06003936 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003937 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003938 else if (output->zoom.level > output->zoom.max_level)
3939 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02003940 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01003941 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003942 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003943
Scott Moreaue6603982012-06-11 13:07:51 -06003944 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003945
Jason Ekstranda7af7042013-10-12 22:38:11 -05003946 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003947 }
3948 }
3949}
3950
Scott Moreauccbf29d2012-02-22 14:21:41 -07003951static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003952zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003953 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003954{
3955 do_zoom(seat, time, 0, axis, value);
3956}
3957
3958static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003959zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003960 void *data)
3961{
3962 do_zoom(seat, time, key, 0, 0);
3963}
3964
3965static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003966terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003967 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003968{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003969 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003970
Daniel Stone325fc2d2012-05-30 16:31:58 +01003971 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003972}
3973
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003974struct alt_tab {
3975 struct desktop_shell *shell;
3976 struct weston_keyboard_grab grab;
3977
3978 struct wl_list *current;
3979
3980 struct wl_list preview_list;
3981};
3982
3983struct alt_tab_preview {
3984 struct alt_tab *alt_tab;
3985
3986 struct weston_view *view;
3987 struct weston_transform transform;
3988
3989 struct wl_listener listener;
3990
3991 struct wl_list link;
3992};
3993
3994static void
3995alt_tab_next(struct alt_tab *alt_tab)
3996{
3997 alt_tab->current = alt_tab->current->next;
3998
3999 /* Make sure we're not pointing to the list header e.g. after
4000 * cycling through the whole list. */
4001 if (alt_tab->current->next == alt_tab->preview_list.next)
4002 alt_tab->current = alt_tab->current->next;
4003}
4004
4005static void
4006alt_tab_destroy(struct alt_tab *alt_tab)
4007{
4008 struct alt_tab_preview *preview, *next;
4009 struct weston_keyboard *keyboard = alt_tab->grab.keyboard;
4010
4011 if (alt_tab->current && alt_tab->current != &alt_tab->preview_list) {
4012 preview = wl_container_of(alt_tab->current, preview, link);
4013
4014 activate(alt_tab->shell, preview->view->surface,
4015 (struct weston_seat *) keyboard->seat);
4016 }
4017
4018 wl_list_for_each_safe(preview, next, &alt_tab->preview_list, link) {
4019 wl_list_remove(&preview->link);
4020 wl_list_remove(&preview->listener.link);
4021 weston_view_destroy(preview->view);
4022 free(preview);
4023 }
4024
4025 weston_keyboard_end_grab(keyboard);
4026 if (keyboard->input_method_resource)
4027 keyboard->grab = &keyboard->input_method_grab;
4028
4029 free(alt_tab);
4030}
4031
4032static void
4033alt_tab_handle_surface_destroy(struct wl_listener *listener, void *data)
4034{
4035 struct alt_tab_preview *preview =
4036 container_of(listener, struct alt_tab_preview, listener);
4037 struct alt_tab *alt_tab = preview->alt_tab;
4038 int advance = 0;
4039
4040 /* If the preview that we're removing is the currently selected one,
4041 * we want to move to the next one. So we move to ->prev and then
4042 * call _next() after removing the preview. */
4043 if (alt_tab->current == &preview->link) {
4044 alt_tab->current = alt_tab->current->prev;
4045 advance = 1;
4046 }
4047
4048 wl_list_remove(&preview->listener.link);
4049 wl_list_remove(&preview->link);
4050
4051 free(preview);
4052
4053 if (advance)
4054 alt_tab_next(alt_tab);
4055
4056 /* If the last preview goes away, stop the alt-tab */
4057 if (wl_list_empty(alt_tab->current))
4058 alt_tab_destroy(alt_tab);
4059}
4060
4061static void
4062alt_tab_key(struct weston_keyboard_grab *grab,
4063 uint32_t time, uint32_t key, uint32_t state_w)
4064{
4065 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4066 enum wl_keyboard_key_state state = state_w;
4067
4068 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
4069 alt_tab_next(alt_tab);
4070}
4071
4072static void
4073alt_tab_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
4074 uint32_t mods_depressed, uint32_t mods_latched,
4075 uint32_t mods_locked, uint32_t group)
4076{
4077 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4078 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
4079
4080 if ((seat->modifier_state & MODIFIER_ALT) == 0)
4081 alt_tab_destroy(alt_tab);
4082}
4083
4084static void
4085alt_tab_cancel(struct weston_keyboard_grab *grab)
4086{
4087 struct alt_tab *alt_tab = container_of(grab, struct alt_tab, grab);
4088
4089 alt_tab_destroy(alt_tab);
4090}
4091
4092static const struct weston_keyboard_grab_interface alt_tab_grab = {
4093 alt_tab_key,
4094 alt_tab_modifier,
4095 alt_tab_cancel,
4096};
4097
4098static int
4099view_for_alt_tab(struct weston_view *view)
4100{
Rafael Antognollied207b42013-12-03 15:35:43 -02004101 struct shell_surface *shsurf = get_shell_surface(view->surface);
4102 if (!shsurf)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004103 return 0;
4104
Rafael Antognollied207b42013-12-03 15:35:43 -02004105 if (shsurf->parent)
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004106 return 0;
4107
4108 if (view != get_default_view(view->surface))
4109 return 0;
4110
4111 return 1;
4112}
4113
4114static void
4115alt_tab_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4116 void *data)
4117{
4118 struct alt_tab *alt_tab;
4119 struct desktop_shell *shell = data;
4120 struct weston_output *output = get_default_output(shell->compositor);
4121 struct workspace *ws;
4122 struct weston_view *view;
4123 int num_surfaces = 0;
4124 int x, y, side, margin;
4125
4126 wl_list_for_each(view, &shell->compositor->view_list, link) {
4127
4128 if (view_for_alt_tab(view))
4129 num_surfaces++;
4130 }
4131
4132 if (!num_surfaces)
4133 return;
4134
4135 alt_tab = malloc(sizeof *alt_tab);
4136 if (!alt_tab)
4137 return;
4138
4139 alt_tab->shell = shell;
4140 wl_list_init(&alt_tab->preview_list);
4141 alt_tab->current = &alt_tab->preview_list;
4142
4143 restore_all_output_modes(shell->compositor);
4144 lower_fullscreen_layer(alt_tab->shell);
4145
4146 alt_tab->grab.interface = &alt_tab_grab;
4147 weston_keyboard_start_grab(seat->keyboard, &alt_tab->grab);
4148 weston_keyboard_set_focus(seat->keyboard, NULL);
4149
4150 ws = get_current_workspace(shell);
4151
4152 /* FIXME: add some visual candy e.g. prelight the selected view
4153 * and/or add a black surrounding background à la gnome-shell */
4154
4155 /* Determine the size for each preview */
4156 side = output->width / num_surfaces;
4157 if (side > 200)
4158 side = 200;
4159 margin = side / 4;
4160 side -= margin;
4161
4162 x = margin;
4163 y = (output->height - side) / 2;
4164
4165 /* Create a view for each surface */
4166 wl_list_for_each(view, &shell->compositor->view_list, link) {
4167 struct alt_tab_preview *preview;
4168 struct weston_view *v;
4169 float scale;
4170
4171 if (!view_for_alt_tab(view))
4172 continue;
4173
4174 preview = malloc(sizeof *preview);
4175 if (!preview) {
4176 alt_tab_destroy(alt_tab);
4177 return;
4178 }
4179
4180 preview->alt_tab = alt_tab;
4181
4182 preview->view = v = weston_view_create(view->surface);
4183 v->output = view->output;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004184
4185 wl_list_remove(&v->layer_link);
4186 wl_list_insert(&ws->layer.view_list, &v->layer_link);
4187 weston_view_damage_below(v);
4188 weston_surface_damage(v->surface);
4189
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004190 weston_view_set_position(v, x, y);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004191
4192 preview->listener.notify = alt_tab_handle_surface_destroy;
4193 wl_signal_add(&v->destroy_signal, &preview->listener);
4194
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004195 if (view->surface->width > view->surface->height)
4196 scale = side / (float) view->surface->width;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004197 else
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004198 scale = side / (float) view->surface->height;
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004199
4200 wl_list_insert(&v->geometry.transformation_list,
4201 &preview->transform.link);
4202 weston_matrix_init(&preview->transform.matrix);
4203 weston_matrix_scale(&preview->transform.matrix,
4204 scale, scale, 1.0f);
4205
4206 weston_view_geometry_dirty(v);
4207 weston_compositor_schedule_repaint(v->surface->compositor);
4208
4209 /* Insert at the end of the list */
4210 wl_list_insert(alt_tab->preview_list.prev, &preview->link);
4211
4212 x += side + margin;
4213 }
4214
4215 /* Start at the second preview so a simple <alt>tab changes window.
4216 * We set `current' to the first preview and not the second because
4217 * we're going to receive a key press callback for the initial
4218 * <alt>tab which will make `current' point to the second element. */
4219 alt_tab_next(alt_tab);
4220}
4221
4222static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004223rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4224 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004225{
4226 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004227 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004228 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004229 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004230 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004231
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004232 weston_pointer_move(pointer, x, y);
4233
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004234 if (!shsurf)
4235 return;
4236
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004237 cx = 0.5f * shsurf->surface->width;
4238 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004239
Daniel Stone37816df2012-05-16 18:45:18 +01004240 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4241 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004242 r = sqrtf(dx * dx + dy * dy);
4243
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004244 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004245 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004246
4247 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004248 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004249 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004250
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004251 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004252 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004253
4254 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004255 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004256 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004257 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004258 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004259
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004260 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004261 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004262 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004263 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004264 wl_list_init(&shsurf->rotation.transform.link);
4265 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004266 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004267 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004268
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004269 /* We need to adjust the position of the surface
4270 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004271 cposx = shsurf->view->geometry.x + cx;
4272 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004273 dposx = rotate->center.x - cposx;
4274 dposy = rotate->center.y - cposy;
4275 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004276 weston_view_set_position(shsurf->view,
4277 shsurf->view->geometry.x + dposx,
4278 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004279 }
4280
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004281 /* Repaint implies weston_surface_update_transform(), which
4282 * lazily applies the damage due to rotation update.
4283 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004284 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004285}
4286
4287static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004288rotate_grab_button(struct weston_pointer_grab *grab,
4289 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004290{
4291 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004292 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004293 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004294 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004295 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004296
Daniel Stone4dbadb12012-05-30 16:31:51 +01004297 if (pointer->button_count == 0 &&
4298 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004299 if (shsurf)
4300 weston_matrix_multiply(&shsurf->rotation.rotation,
4301 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004302 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004303 free(rotate);
4304 }
4305}
4306
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004307static void
4308rotate_grab_cancel(struct weston_pointer_grab *grab)
4309{
4310 struct rotate_grab *rotate =
4311 container_of(grab, struct rotate_grab, base.grab);
4312
4313 shell_grab_end(&rotate->base);
4314 free(rotate);
4315}
4316
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004317static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004318 noop_grab_focus,
4319 rotate_grab_motion,
4320 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004321 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004322};
4323
4324static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004325surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004326{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004327 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004328 float dx, dy;
4329 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004330
Pekka Paalanen460099f2012-01-20 16:48:25 +02004331 rotate = malloc(sizeof *rotate);
4332 if (!rotate)
4333 return;
4334
Jason Ekstranda7af7042013-10-12 22:38:11 -05004335 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004336 surface->surface->width * 0.5f,
4337 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004338 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004339
Daniel Stone37816df2012-05-16 18:45:18 +01004340 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4341 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004342 r = sqrtf(dx * dx + dy * dy);
4343 if (r > 20.0f) {
4344 struct weston_matrix inverse;
4345
4346 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004347 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004348 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004349
4350 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004351 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004352 } else {
4353 weston_matrix_init(&surface->rotation.rotation);
4354 weston_matrix_init(&rotate->rotation);
4355 }
4356
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004357 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4358 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004359}
4360
4361static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004362rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004363 void *data)
4364{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004365 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004366 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004367 struct shell_surface *surface;
4368
Pekka Paalanen01388e22013-04-25 13:57:44 +03004369 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004370 if (base_surface == NULL)
4371 return;
4372
4373 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004374 if (surface == NULL || surface->state.fullscreen ||
4375 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004376 return;
4377
4378 surface_rotate(surface, seat);
4379}
4380
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004381/* Move all fullscreen layers down to the current workspace in a non-reversible
4382 * manner. This should be used when implementing shell-wide overlays, such as
4383 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004384void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004385lower_fullscreen_layer(struct desktop_shell *shell)
4386{
4387 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004388 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004389
4390 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004391 wl_list_for_each_reverse_safe(view, prev,
4392 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004393 layer_link) {
4394 wl_list_remove(&view->layer_link);
4395 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4396 weston_view_damage_below(view);
4397 weston_surface_damage(view->surface);
4398 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004399}
4400
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004401void
Tiago Vignattibe143262012-04-16 17:31:41 +03004402activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004403 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004404{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004405 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004406 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004407 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004408 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004409 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004410
Pekka Paalanen01388e22013-04-25 13:57:44 +03004411 main_surface = weston_surface_get_main_surface(es);
4412
Daniel Stone37816df2012-05-16 18:45:18 +01004413 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004414
Jonas Ådahl8538b222012-08-29 22:13:03 +02004415 state = ensure_focus_state(shell, seat);
4416 if (state == NULL)
4417 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004418
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004419 old_es = state->keyboard_focus;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004420 state->keyboard_focus = es;
4421 wl_list_remove(&state->surface_destroy_listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004422 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004423
Rafael Antognolli03b16592013-12-03 15:35:42 -02004424 shsurf = get_shell_surface(main_surface);
4425 if (shsurf->state.fullscreen)
4426 shell_configure_fullscreen(shsurf);
4427 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004428 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004429
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004430 if (shell->focus_animation_type != ANIMATION_NONE) {
4431 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004432 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004433 }
4434
4435 /* Update the surface’s layer. This brings it to the top of the stacking
4436 * order as appropriate. */
4437 shell_surface_update_layer(get_shell_surface(main_surface));
Kristian Høgsberg75840622011-09-06 13:48:16 -04004438}
4439
Alex Wu21858432012-04-01 20:13:08 +08004440/* no-op func for checking black surface */
4441static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004442black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004443{
4444}
4445
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004446static bool
Alex Wu21858432012-04-01 20:13:08 +08004447is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4448{
4449 if (es->configure == black_surface_configure) {
4450 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004451 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004452 return true;
4453 }
4454 return false;
4455}
4456
Kristian Høgsberg75840622011-09-06 13:48:16 -04004457static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004458activate_binding(struct weston_seat *seat,
4459 struct desktop_shell *shell,
4460 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004461{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004462 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004463
Alex Wu9c35e6b2012-03-05 14:13:13 +08004464 if (!focus)
4465 return;
4466
Pekka Paalanen01388e22013-04-25 13:57:44 +03004467 if (is_black_surface(focus, &main_surface))
4468 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004469
Pekka Paalanen01388e22013-04-25 13:57:44 +03004470 main_surface = weston_surface_get_main_surface(focus);
4471 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004472 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004473
Neil Robertsa28c6932013-10-03 16:43:04 +01004474 activate(shell, focus, seat);
4475}
4476
4477static void
4478click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4479 void *data)
4480{
4481 if (seat->pointer->grab != &seat->pointer->default_grab)
4482 return;
4483
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004484 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004485}
4486
4487static void
4488touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4489{
4490 if (seat->touch->grab != &seat->touch->default_grab)
4491 return;
4492
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004493 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004494}
4495
4496static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004497lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004498{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004499 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004500
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004501 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004502 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004503 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004504 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004505
4506 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004507
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004508 /* Hide all surfaces by removing the fullscreen, panel and
4509 * toplevel layers. This way nothing else can show or receive
4510 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004511
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004512 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004513 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004514 if (shell->showing_input_panels)
4515 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004516 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004517 wl_list_insert(&shell->compositor->cursor_layer.link,
4518 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004519
Pekka Paalanen77346a62011-11-30 16:26:35 +02004520 launch_screensaver(shell);
4521
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004522 /* TODO: disable bindings that should not work while locked. */
4523
4524 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004525}
4526
4527static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004528unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004529{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004530 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004531 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004532 return;
4533 }
4534
4535 /* If desktop-shell client has gone away, unlock immediately. */
4536 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004537 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004538 return;
4539 }
4540
4541 if (shell->prepare_event_sent)
4542 return;
4543
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004544 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004545 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004546}
4547
4548static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004549shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004550{
4551 struct desktop_shell *shell = data;
4552
4553 shell->fade.animation = NULL;
4554
4555 switch (shell->fade.type) {
4556 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004557 weston_surface_destroy(shell->fade.view->surface);
4558 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004559 break;
4560 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004561 lock(shell);
4562 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004563 default:
4564 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004565 }
4566}
4567
Jason Ekstranda7af7042013-10-12 22:38:11 -05004568static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004569shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004570{
4571 struct weston_compositor *compositor = shell->compositor;
4572 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004573 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004574
4575 surface = weston_surface_create(compositor);
4576 if (!surface)
4577 return NULL;
4578
Jason Ekstranda7af7042013-10-12 22:38:11 -05004579 view = weston_view_create(surface);
4580 if (!view) {
4581 weston_surface_destroy(surface);
4582 return NULL;
4583 }
4584
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004585 surface->width = 8192;
4586 surface->height = 8192;
4587 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004588 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004589 wl_list_insert(&compositor->fade_layer.view_list,
4590 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004591 pixman_region32_init(&surface->input);
4592
Jason Ekstranda7af7042013-10-12 22:38:11 -05004593 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004594}
4595
4596static void
4597shell_fade(struct desktop_shell *shell, enum fade_type type)
4598{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004599 float tint;
4600
4601 switch (type) {
4602 case FADE_IN:
4603 tint = 0.0;
4604 break;
4605 case FADE_OUT:
4606 tint = 1.0;
4607 break;
4608 default:
4609 weston_log("shell: invalid fade type\n");
4610 return;
4611 }
4612
4613 shell->fade.type = type;
4614
Jason Ekstranda7af7042013-10-12 22:38:11 -05004615 if (shell->fade.view == NULL) {
4616 shell->fade.view = shell_fade_create_surface(shell);
4617 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004618 return;
4619
Jason Ekstranda7af7042013-10-12 22:38:11 -05004620 shell->fade.view->alpha = 1.0 - tint;
4621 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004622 }
4623
4624 if (shell->fade.animation)
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004625 weston_fade_update(shell->fade.animation, tint);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004626 else
4627 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004628 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004629 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004630 shell_fade_done, shell);
4631}
4632
4633static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004634do_shell_fade_startup(void *data)
4635{
4636 struct desktop_shell *shell = data;
4637
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004638 if (shell->startup_animation_type == ANIMATION_FADE)
4639 shell_fade(shell, FADE_IN);
4640 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004641 weston_surface_destroy(shell->fade.view->surface);
4642 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004643 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004644}
4645
4646static void
4647shell_fade_startup(struct desktop_shell *shell)
4648{
4649 struct wl_event_loop *loop;
4650
4651 if (!shell->fade.startup_timer)
4652 return;
4653
4654 wl_event_source_remove(shell->fade.startup_timer);
4655 shell->fade.startup_timer = NULL;
4656
4657 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4658 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4659}
4660
4661static int
4662fade_startup_timeout(void *data)
4663{
4664 struct desktop_shell *shell = data;
4665
4666 shell_fade_startup(shell);
4667 return 0;
4668}
4669
4670static void
4671shell_fade_init(struct desktop_shell *shell)
4672{
4673 /* Make compositor output all black, and wait for the desktop-shell
4674 * client to signal it is ready, then fade in. The timer triggers a
4675 * fade-in, in case the desktop-shell client takes too long.
4676 */
4677
4678 struct wl_event_loop *loop;
4679
Jason Ekstranda7af7042013-10-12 22:38:11 -05004680 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004681 weston_log("%s: warning: fade surface already exists\n",
4682 __func__);
4683 return;
4684 }
4685
Jason Ekstranda7af7042013-10-12 22:38:11 -05004686 shell->fade.view = shell_fade_create_surface(shell);
4687 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004688 return;
4689
Jason Ekstranda7af7042013-10-12 22:38:11 -05004690 weston_view_update_transform(shell->fade.view);
4691 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004692
4693 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4694 shell->fade.startup_timer =
4695 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4696 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4697}
4698
4699static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004700idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004701{
4702 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004703 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004704
4705 shell_fade(shell, FADE_OUT);
4706 /* lock() is called from shell_fade_done() */
4707}
4708
4709static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004710wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004711{
4712 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004713 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004714
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004715 unlock(shell);
4716}
4717
4718static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004719center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004720{
Giulio Camuffob8366642013-04-25 13:57:46 +03004721 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004722 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004723
Jason Ekstranda7af7042013-10-12 22:38:11 -05004724 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004725
4726 x = output->x + (output->width - width) / 2 - surf_x / 2;
4727 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004728
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004729 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004730}
4731
4732static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004733weston_view_set_initial_position(struct weston_view *view,
4734 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004735{
4736 struct weston_compositor *compositor = shell->compositor;
4737 int ix = 0, iy = 0;
4738 int range_x, range_y;
4739 int dx, dy, x, y, panel_height;
4740 struct weston_output *output, *target_output = NULL;
4741 struct weston_seat *seat;
4742
4743 /* As a heuristic place the new window on the same output as the
4744 * pointer. Falling back to the output containing 0, 0.
4745 *
4746 * TODO: Do something clever for touch too?
4747 */
4748 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004749 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004750 ix = wl_fixed_to_int(seat->pointer->x);
4751 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004752 break;
4753 }
4754 }
4755
4756 wl_list_for_each(output, &compositor->output_list, link) {
4757 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4758 target_output = output;
4759 break;
4760 }
4761 }
4762
4763 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004764 weston_view_set_position(view, 10 + random() % 400,
4765 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004766 return;
4767 }
4768
4769 /* Valid range within output where the surface will still be onscreen.
4770 * If this is negative it means that the surface is bigger than
4771 * output.
4772 */
4773 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004774 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004775 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004776 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004777
Rob Bradford4cb88c72012-08-13 15:18:44 +01004778 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004779 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004780 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004781 dx = 0;
4782
4783 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004784 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004785 else
4786 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004787
4788 x = target_output->x + dx;
4789 y = target_output->y + dy;
4790
Jason Ekstranda7af7042013-10-12 22:38:11 -05004791 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004792}
4793
4794static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004795map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004796 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004797{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004798 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004799 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004800 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004801 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004802
Pekka Paalanen77346a62011-11-30 16:26:35 +02004803 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004804 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004805 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004806 if (shsurf->state.fullscreen) {
4807 center_on_output(shsurf->view, shsurf->fullscreen_output);
4808 shell_map_fullscreen(shsurf);
4809 } else if (shsurf->state.maximized) {
4810 /* use surface configure to set the geometry */
4811 panel_height = get_output_panel_height(shell, shsurf->output);
4812 surface_subsurfaces_boundingbox(shsurf->surface,
4813 &surf_x, &surf_y, NULL, NULL);
4814 weston_view_set_position(shsurf->view,
4815 shsurf->output->x - surf_x,
4816 shsurf->output->y +
4817 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004818 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004819 weston_view_set_initial_position(shsurf->view, shell);
4820 }
Juan Zhao96879df2012-02-07 08:45:41 +08004821 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004822 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004823 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004824 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004825 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004826 weston_view_set_position(shsurf->view,
4827 shsurf->view->geometry.x + sx,
4828 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004829 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004830 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004831 default:
4832 ;
4833 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004834
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004835 /* Surface stacking order, see also activate(). */
4836 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004837
Jason Ekstranda7af7042013-10-12 22:38:11 -05004838 if (shsurf->type != SHELL_SURFACE_NONE) {
4839 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004840 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004841 shsurf->surface->output = shsurf->output;
4842 shsurf->view->output = shsurf->output;
4843 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004844 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004845
Rafael Antognollied207b42013-12-03 15:35:43 -02004846 if ((shsurf->type == SHELL_SURFACE_XWAYLAND || shsurf->state.relative) &&
4847 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
4848 }
4849
Jason Ekstranda7af7042013-10-12 22:38:11 -05004850 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004851 /* XXX: xwayland's using the same fields for transient type */
4852 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004853 if (shsurf->transient.flags ==
4854 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4855 break;
4856 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004857 if (shsurf->state.relative &&
4858 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4859 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004860 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004861 break;
4862 wl_list_for_each(seat, &compositor->seat_list, link)
4863 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004864 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004865 case SHELL_SURFACE_POPUP:
4866 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004867 default:
4868 break;
4869 }
4870
Rafael Antognolli03b16592013-12-03 15:35:42 -02004871 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4872 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004873 {
4874 switch (shell->win_animation_type) {
4875 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004876 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004877 break;
4878 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004879 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004880 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004881 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004882 default:
4883 break;
4884 }
4885 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004886}
4887
4888static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004889configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004890 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004891{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004892 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004893 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004894 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004895
Pekka Paalanen77346a62011-11-30 16:26:35 +02004896 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004897
Rafael Antognolli03b16592013-12-03 15:35:42 -02004898 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004899 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004900 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004901 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004902 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4903 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004904 mx = shsurf->output->x - surf_x;
4905 my = shsurf->output->y +
4906 get_output_panel_height(shell,shsurf->output) - surf_y;
4907 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004908 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004909 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004910 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004911
Alex Wu4539b082012-03-01 12:57:46 +08004912 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004913 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004914 wl_list_for_each(view, &surface->views, surface_link)
4915 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004916
Rafael Antognolli03b16592013-12-03 15:35:42 -02004917 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004918 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004919 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004920}
4921
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004922static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004923shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004924{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004925 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03004926 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01004927
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004928 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004929
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004930 if (!weston_surface_is_mapped(es) &&
4931 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004932 remove_popup_grab(shsurf);
4933 }
4934
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004935 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004936 return;
4937
Rafael Antognolli03b16592013-12-03 15:35:42 -02004938 if ((shsurf->next_type != SHELL_SURFACE_NONE &&
4939 shsurf->type != shsurf->next_type) ||
4940 shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004941 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004942 type_changed = 1;
4943 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004944
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004945 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004946 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004947 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004948 shsurf->last_width != es->width ||
4949 shsurf->last_height != es->height) {
4950 shsurf->last_width = es->width;
4951 shsurf->last_height = es->height;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004952 float from_x, from_y;
4953 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004954
Jason Ekstranda7af7042013-10-12 22:38:11 -05004955 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4956 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004957 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004958 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004959 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004960 }
4961}
4962
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004963static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004964
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004965static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004966desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004967{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004968 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004969 struct desktop_shell *shell =
4970 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004971
4972 shell->child.process.pid = 0;
4973 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004974
4975 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4976 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004977 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004978 shell->child.deathstamp = time;
4979 shell->child.deathcount = 0;
4980 }
4981
4982 shell->child.deathcount++;
4983 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004984 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004985 return;
4986 }
4987
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004988 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004989 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004990 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004991}
4992
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004993static void
4994launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004995{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004996 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004997
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004998 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004999 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005000 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005001 desktop_shell_sigchld);
5002
5003 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005004 weston_log("not able to start %s\n", shell->client);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005005}
5006
5007static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005008bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5009{
Tiago Vignattibe143262012-04-16 17:31:41 +03005010 struct desktop_shell *shell = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005011 struct wl_resource *resource;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005012
Jason Ekstranda85118c2013-06-27 20:17:02 -05005013 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
5014 if (resource)
5015 wl_resource_set_implementation(resource, &shell_implementation,
5016 shell, NULL);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005017}
5018
Kristian Høgsberg75840622011-09-06 13:48:16 -04005019static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02005020bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5021{
5022 struct desktop_shell *shell = data;
5023 struct wl_resource *resource;
5024
5025 resource = wl_resource_create(client, &xdg_shell_interface, 1, id);
5026 if (resource)
5027 wl_resource_set_dispatcher(resource,
5028 xdg_shell_unversioned_dispatch,
5029 NULL, shell, NULL);
5030}
5031
5032static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005033unbind_desktop_shell(struct wl_resource *resource)
5034{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005035 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005036
5037 if (shell->locked)
5038 resume_desktop(shell);
5039
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005040 shell->child.desktop_shell = NULL;
5041 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005042}
5043
5044static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005045bind_desktop_shell(struct wl_client *client,
5046 void *data, uint32_t version, uint32_t id)
5047{
Tiago Vignattibe143262012-04-16 17:31:41 +03005048 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005049 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005050
Jason Ekstranda85118c2013-06-27 20:17:02 -05005051 resource = wl_resource_create(client, &desktop_shell_interface,
5052 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005053
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005054 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005055 wl_resource_set_implementation(resource,
5056 &desktop_shell_implementation,
5057 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005058 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005059
5060 if (version < 2)
5061 shell_fade_startup(shell);
5062
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005063 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005064 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005065
5066 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5067 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005068 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005069}
5070
Pekka Paalanen6e168112011-11-24 11:34:05 +02005071static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005072screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005073{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005074 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005075 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005076
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005077 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005078 return;
5079
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005080 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005081 if (!shell->locked)
5082 return;
5083
Jason Ekstranda7af7042013-10-12 22:38:11 -05005084 view = container_of(surface->views.next, struct weston_view, surface_link);
5085 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005086
Jason Ekstranda7af7042013-10-12 22:38:11 -05005087 if (wl_list_empty(&view->layer_link)) {
5088 wl_list_insert(shell->lock_layer.view_list.prev,
5089 &view->layer_link);
5090 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005091 wl_event_source_timer_update(shell->screensaver.timer,
5092 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005093 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005094 }
5095}
5096
5097static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005098screensaver_set_surface(struct wl_client *client,
5099 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005100 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005101 struct wl_resource *output_resource)
5102{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005103 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005104 struct weston_surface *surface =
5105 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005106 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005107 struct weston_view *view, *next;
5108
5109 /* Make sure we only have one view */
5110 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5111 weston_view_destroy(view);
5112 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005113
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005114 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005115 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005116 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005117}
5118
5119static const struct screensaver_interface screensaver_implementation = {
5120 screensaver_set_surface
5121};
5122
5123static void
5124unbind_screensaver(struct wl_resource *resource)
5125{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005126 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005127
Pekka Paalanen77346a62011-11-30 16:26:35 +02005128 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005129}
5130
5131static void
5132bind_screensaver(struct wl_client *client,
5133 void *data, uint32_t version, uint32_t id)
5134{
Tiago Vignattibe143262012-04-16 17:31:41 +03005135 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005136 struct wl_resource *resource;
5137
Jason Ekstranda85118c2013-06-27 20:17:02 -05005138 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005139
Pekka Paalanen77346a62011-11-30 16:26:35 +02005140 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005141 wl_resource_set_implementation(resource,
5142 &screensaver_implementation,
5143 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005144 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005145 return;
5146 }
5147
5148 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5149 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005150 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005151}
5152
Kristian Høgsberg07045392012-02-19 18:52:44 -05005153struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005154 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005155 struct weston_surface *current;
5156 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005157 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005158};
5159
5160static void
5161switcher_next(struct switcher *switcher)
5162{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005163 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005164 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005165 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005166 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005167
Jason Ekstranda7af7042013-10-12 22:38:11 -05005168 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005169 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005170 if (shsurf &&
5171 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5172 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005173 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005174 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005175 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005176 next = view->surface;
5177 prev = view->surface;
5178 view->alpha = 0.25;
5179 weston_view_geometry_dirty(view);
5180 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005181 }
Alex Wu1659daa2012-04-01 20:13:09 +08005182
Jason Ekstranda7af7042013-10-12 22:38:11 -05005183 if (is_black_surface(view->surface, NULL)) {
5184 view->alpha = 0.25;
5185 weston_view_geometry_dirty(view);
5186 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005187 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005188 }
5189
5190 if (next == NULL)
5191 next = first;
5192
Alex Wu07b26062012-03-12 16:06:01 +08005193 if (next == NULL)
5194 return;
5195
Kristian Høgsberg07045392012-02-19 18:52:44 -05005196 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005197 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005198
5199 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005200 wl_list_for_each(view, &next->views, surface_link)
5201 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005202
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005203 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005204 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005205 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005206}
5207
5208static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005209switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005210{
5211 struct switcher *switcher =
5212 container_of(listener, struct switcher, listener);
5213
5214 switcher_next(switcher);
5215}
5216
5217static void
Daniel Stone351eb612012-05-31 15:27:47 -04005218switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005219{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005220 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005221 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005222 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005223
Jason Ekstranda7af7042013-10-12 22:38:11 -05005224 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005225 if (is_focus_view(view))
5226 continue;
5227
Jason Ekstranda7af7042013-10-12 22:38:11 -05005228 view->alpha = 1.0;
5229 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005230 }
5231
Alex Wu07b26062012-03-12 16:06:01 +08005232 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005233 activate(switcher->shell, switcher->current,
5234 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005235 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005236 weston_keyboard_end_grab(keyboard);
5237 if (keyboard->input_method_resource)
5238 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005239 free(switcher);
5240}
5241
5242static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005243switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005244 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005245{
5246 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005247 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005248
Daniel Stonec9785ea2012-05-30 16:31:52 +01005249 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005250 switcher_next(switcher);
5251}
5252
5253static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005254switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005255 uint32_t mods_depressed, uint32_t mods_latched,
5256 uint32_t mods_locked, uint32_t group)
5257{
5258 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005259 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005260
Daniel Stone351eb612012-05-31 15:27:47 -04005261 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5262 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005263}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005264
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005265static void
5266switcher_cancel(struct weston_keyboard_grab *grab)
5267{
5268 struct switcher *switcher = container_of(grab, struct switcher, grab);
5269
5270 switcher_destroy(switcher);
5271}
5272
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005273static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005274 switcher_key,
5275 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005276 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005277};
5278
5279static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005280switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005281 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005282{
Tiago Vignattibe143262012-04-16 17:31:41 +03005283 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005284 struct switcher *switcher;
5285
5286 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005287 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005288 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005289 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005290 wl_list_init(&switcher->listener.link);
5291
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005292 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005293 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005294 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005295 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5296 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005297 switcher_next(switcher);
5298}
5299
Pekka Paalanen3c647232011-12-22 13:43:43 +02005300static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005301backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005302 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005303{
5304 struct weston_compositor *compositor = data;
5305 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005306 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005307
5308 /* TODO: we're limiting to simple use cases, where we assume just
5309 * control on the primary display. We'd have to extend later if we
5310 * ever get support for setting backlights on random desktop LCD
5311 * panels though */
5312 output = get_default_output(compositor);
5313 if (!output)
5314 return;
5315
5316 if (!output->set_backlight)
5317 return;
5318
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005319 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5320 backlight_new = output->backlight_current - 25;
5321 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5322 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005323
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005324 if (backlight_new < 5)
5325 backlight_new = 5;
5326 if (backlight_new > 255)
5327 backlight_new = 255;
5328
5329 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005330 output->set_backlight(output, output->backlight_current);
5331}
5332
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005333struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005334 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005335 struct weston_seat *seat;
5336 uint32_t key[2];
5337 int key_released[2];
5338};
5339
5340static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005341debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005342 uint32_t key, uint32_t state)
5343{
5344 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005345 struct weston_compositor *ec = db->seat->compositor;
5346 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005347 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005348 uint32_t serial;
5349 int send = 0, terminate = 0;
5350 int check_binding = 1;
5351 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005352 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005353
5354 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5355 /* Do not run bindings on key releases */
5356 check_binding = 0;
5357
5358 for (i = 0; i < 2; i++)
5359 if (key == db->key[i])
5360 db->key_released[i] = 1;
5361
5362 if (db->key_released[0] && db->key_released[1]) {
5363 /* All key releases been swalled so end the grab */
5364 terminate = 1;
5365 } else if (key != db->key[0] && key != db->key[1]) {
5366 /* Should not swallow release of other keys */
5367 send = 1;
5368 }
5369 } else if (key == db->key[0] && !db->key_released[0]) {
5370 /* Do not check bindings for the first press of the binding
5371 * key. This allows it to be used as a debug shortcut.
5372 * We still need to swallow this event. */
5373 check_binding = 0;
5374 } else if (db->key[1]) {
5375 /* If we already ran a binding don't process another one since
5376 * we can't keep track of all the binding keys that were
5377 * pressed in order to swallow the release events. */
5378 send = 1;
5379 check_binding = 0;
5380 }
5381
5382 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005383 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5384 key, state)) {
5385 /* We ran a binding so swallow the press and keep the
5386 * grab to swallow the released too. */
5387 send = 0;
5388 terminate = 0;
5389 db->key[1] = key;
5390 } else {
5391 /* Terminate the grab since the key pressed is not a
5392 * debug binding key. */
5393 send = 1;
5394 terminate = 1;
5395 }
5396 }
5397
5398 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005399 serial = wl_display_next_serial(display);
5400 resource_list = &grab->keyboard->focus_resource_list;
5401 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005402 wl_keyboard_send_key(resource, serial, time, key, state);
5403 }
5404 }
5405
5406 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005407 weston_keyboard_end_grab(grab->keyboard);
5408 if (grab->keyboard->input_method_resource)
5409 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005410 free(db);
5411 }
5412}
5413
5414static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005415debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005416 uint32_t mods_depressed, uint32_t mods_latched,
5417 uint32_t mods_locked, uint32_t group)
5418{
5419 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005420 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005421
Neil Roberts96d790e2013-09-19 17:32:00 +01005422 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005423
Neil Roberts96d790e2013-09-19 17:32:00 +01005424 wl_resource_for_each(resource, resource_list) {
5425 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5426 mods_latched, mods_locked, group);
5427 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005428}
5429
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005430static void
5431debug_binding_cancel(struct weston_keyboard_grab *grab)
5432{
5433 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5434
5435 weston_keyboard_end_grab(grab->keyboard);
5436 free(db);
5437}
5438
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005439struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005440 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005441 debug_binding_modifiers,
5442 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005443};
5444
5445static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005446debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005447{
5448 struct debug_binding_grab *grab;
5449
5450 grab = calloc(1, sizeof *grab);
5451 if (!grab)
5452 return;
5453
5454 grab->seat = (struct weston_seat *) seat;
5455 grab->key[0] = key;
5456 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005457 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005458}
5459
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005460static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005461force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005462 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005463{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005464 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005465 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005466 struct desktop_shell *shell = data;
5467 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005468 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005469
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005470 focus_surface = seat->keyboard->focus;
5471 if (!focus_surface)
5472 return;
5473
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005474 wl_signal_emit(&compositor->kill_signal, focus_surface);
5475
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005476 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005477 wl_client_get_credentials(client, &pid, NULL, NULL);
5478
5479 /* Skip clients that we launched ourselves (the credentials of
5480 * the socketpair is ours) */
5481 if (pid == getpid())
5482 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005483
Daniel Stone325fc2d2012-05-30 16:31:58 +01005484 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005485}
5486
5487static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005488workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005489 uint32_t key, void *data)
5490{
5491 struct desktop_shell *shell = data;
5492 unsigned int new_index = shell->workspaces.current;
5493
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005494 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005495 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005496 if (new_index != 0)
5497 new_index--;
5498
5499 change_workspace(shell, new_index);
5500}
5501
5502static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005503workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005504 uint32_t key, void *data)
5505{
5506 struct desktop_shell *shell = data;
5507 unsigned int new_index = shell->workspaces.current;
5508
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005509 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005510 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005511 if (new_index < shell->workspaces.num - 1)
5512 new_index++;
5513
5514 change_workspace(shell, new_index);
5515}
5516
5517static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005518workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005519 uint32_t key, void *data)
5520{
5521 struct desktop_shell *shell = data;
5522 unsigned int new_index;
5523
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005524 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005525 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005526 new_index = key - KEY_F1;
5527 if (new_index >= shell->workspaces.num)
5528 new_index = shell->workspaces.num - 1;
5529
5530 change_workspace(shell, new_index);
5531}
5532
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005533static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005534workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005535 uint32_t key, void *data)
5536{
5537 struct desktop_shell *shell = data;
5538 unsigned int new_index = shell->workspaces.current;
5539
5540 if (shell->locked)
5541 return;
5542
5543 if (new_index != 0)
5544 new_index--;
5545
5546 take_surface_to_workspace_by_seat(shell, seat, new_index);
5547}
5548
5549static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005550workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005551 uint32_t key, void *data)
5552{
5553 struct desktop_shell *shell = data;
5554 unsigned int new_index = shell->workspaces.current;
5555
5556 if (shell->locked)
5557 return;
5558
5559 if (new_index < shell->workspaces.num - 1)
5560 new_index++;
5561
5562 take_surface_to_workspace_by_seat(shell, seat, new_index);
5563}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005564
5565static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005566handle_output_destroy(struct wl_listener *listener, void *data)
5567{
5568 struct shell_output *output_listener =
5569 container_of(listener, struct shell_output, destroy_listener);
5570
5571 wl_list_remove(&output_listener->destroy_listener.link);
5572 wl_list_remove(&output_listener->link);
5573 free(output_listener);
5574}
5575
5576static void
5577create_shell_output(struct desktop_shell *shell,
5578 struct weston_output *output)
5579{
5580 struct shell_output *shell_output;
5581
5582 shell_output = zalloc(sizeof *shell_output);
5583 if (shell_output == NULL)
5584 return;
5585
5586 shell_output->output = output;
5587 shell_output->shell = shell;
5588 shell_output->destroy_listener.notify = handle_output_destroy;
5589 wl_signal_add(&output->destroy_signal,
5590 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005591 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005592}
5593
5594static void
5595handle_output_create(struct wl_listener *listener, void *data)
5596{
5597 struct desktop_shell *shell =
5598 container_of(listener, struct desktop_shell, output_create_listener);
5599 struct weston_output *output = (struct weston_output *)data;
5600
5601 create_shell_output(shell, output);
5602}
5603
5604static void
5605setup_output_destroy_handler(struct weston_compositor *ec,
5606 struct desktop_shell *shell)
5607{
5608 struct weston_output *output;
5609
5610 wl_list_init(&shell->output_list);
5611 wl_list_for_each(output, &ec->output_list, link)
5612 create_shell_output(shell, output);
5613
5614 shell->output_create_listener.notify = handle_output_create;
5615 wl_signal_add(&ec->output_created_signal,
5616 &shell->output_create_listener);
5617}
5618
5619static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005620shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005621{
Tiago Vignattibe143262012-04-16 17:31:41 +03005622 struct desktop_shell *shell =
5623 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005624 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005625 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005626
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005627 if (shell->child.client)
5628 wl_client_destroy(shell->child.client);
5629
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005630 wl_list_remove(&shell->idle_listener.link);
5631 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005632
5633 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005634
Xiong Zhang6b481422013-10-23 13:58:32 +08005635 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5636 wl_list_remove(&shell_output->destroy_listener.link);
5637 wl_list_remove(&shell_output->link);
5638 free(shell_output);
5639 }
5640
5641 wl_list_remove(&shell->output_create_listener.link);
5642
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005643 wl_array_for_each(ws, &shell->workspaces.array)
5644 workspace_destroy(*ws);
5645 wl_array_release(&shell->workspaces.array);
5646
Pekka Paalanen3c647232011-12-22 13:43:43 +02005647 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005648 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005649 free(shell);
5650}
5651
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005652static void
5653shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5654{
5655 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005656 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005657
5658 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005659 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5660 MODIFIER_CTRL | MODIFIER_ALT,
5661 terminate_binding, ec);
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01005662 weston_compositor_add_key_binding(ec, KEY_TAB,
5663 MODIFIER_ALT,
5664 alt_tab_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005665 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5666 click_to_activate_binding,
5667 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005668 weston_compositor_add_touch_binding(ec, 0,
5669 touch_to_activate_binding,
5670 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005671 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5672 MODIFIER_SUPER | MODIFIER_ALT,
5673 surface_opacity_binding, NULL);
5674 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5675 MODIFIER_SUPER, zoom_axis_binding,
5676 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005677
5678 /* configurable bindings */
5679 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005680 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5681 zoom_key_binding, NULL);
5682 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5683 zoom_key_binding, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02005684 weston_compositor_add_key_binding(ec, KEY_M, mod, maximize_binding,
5685 NULL);
5686 weston_compositor_add_key_binding(ec, KEY_F, mod, fullscreen_binding,
5687 NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005688 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5689 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005690 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005691 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5692 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005693
5694 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5695 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5696 rotate_binding, NULL);
5697
Daniel Stone325fc2d2012-05-30 16:31:58 +01005698 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5699 shell);
5700 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5701 ec);
5702 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5703 backlight_binding, ec);
5704 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5705 ec);
5706 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5707 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005708 weston_compositor_add_key_binding(ec, KEY_K, mod,
5709 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005710 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5711 workspace_up_binding, shell);
5712 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5713 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005714 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5715 workspace_move_surface_up_binding,
5716 shell);
5717 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5718 workspace_move_surface_down_binding,
5719 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005720
Daniel Stonedf8133b2013-11-19 11:37:14 +01005721 weston_compositor_add_modifier_binding(ec, mod, exposay_binding, shell);
5722
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005723 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5724 if (shell->workspaces.num > 1) {
5725 num_workspace_bindings = shell->workspaces.num;
5726 if (num_workspace_bindings > 6)
5727 num_workspace_bindings = 6;
5728 for (i = 0; i < num_workspace_bindings; i++)
5729 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5730 workspace_f_binding,
5731 shell);
5732 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005733
5734 /* Debug bindings */
5735 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5736 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005737}
5738
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005739WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005740module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005741 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005742{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005743 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005744 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005745 struct workspace **pws;
5746 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005747 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005748
Peter Huttererf3d62272013-08-08 11:57:05 +10005749 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005750 if (shell == NULL)
5751 return -1;
5752
Kristian Høgsberg75840622011-09-06 13:48:16 -04005753 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005754
5755 shell->destroy_listener.notify = shell_destroy;
5756 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005757 shell->idle_listener.notify = idle_handler;
5758 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5759 shell->wake_listener.notify = wake_handler;
5760 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005761
Scott Moreauff1db4a2012-04-17 19:06:18 -06005762 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005763 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005764 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005765 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005766 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005767 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005768 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005769 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005770 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005771 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005772 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005773
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005774 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5775 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005776 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5777 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005778 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005779
5780 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005781 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005782
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005783 if (input_panel_setup(shell) < 0)
5784 return -1;
5785
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005786 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005787
Daniel Stonedf8133b2013-11-19 11:37:14 +01005788 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5789 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5790
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005791 for (i = 0; i < shell->workspaces.num; i++) {
5792 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5793 if (pws == NULL)
5794 return -1;
5795
5796 *pws = workspace_create();
5797 if (*pws == NULL)
5798 return -1;
5799 }
5800 activate_workspace(shell, 0);
5801
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005802 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005803 wl_list_init(&shell->workspaces.animation.link);
5804 shell->workspaces.animation.frame = animate_workspace_change_frame;
5805
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005806 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005807 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005808 return -1;
5809
Rafael Antognollie2a34552013-12-03 15:35:45 -02005810 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5811 shell, bind_xdg_shell) == NULL)
5812 return -1;
5813
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005814 if (wl_global_create(ec->wl_display,
5815 &desktop_shell_interface, 2,
5816 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005817 return -1;
5818
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005819 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5820 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005821 return -1;
5822
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005823 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5824 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005825 return -1;
5826
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005827 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005828
Xiong Zhang6b481422013-10-23 13:58:32 +08005829 setup_output_destroy_handler(ec, shell);
5830
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005831 loop = wl_display_get_event_loop(ec->wl_display);
5832 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005833
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005834 shell->screensaver.timer =
5835 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5836
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005837 wl_list_for_each(seat, &ec->seat_list, link)
5838 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005839
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005840 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005841
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005842 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005843
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005844 return 0;
5845}