blob: 1b57bfc50577e468d6e7d6cbb1108e7643dabbdd [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
26
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040028#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080037#include "shell.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020039#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020040#include "../shared/config-parser.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020041#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010046#ifndef static_assert
47#define static_assert(cond, msg)
48#endif
49
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
51 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040052 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Xiong Zhang6b481422013-10-23 13:58:32 +080059struct shell_output {
60 struct desktop_shell *shell;
61 struct weston_output *output;
62 struct wl_listener destroy_listener;
63 struct wl_list link;
64};
65
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050066enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020067 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050068 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030069 SHELL_SURFACE_POPUP,
70 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020071};
72
Philip Withnall648a4dd2013-11-25 18:01:44 +000073/*
74 * Surface stacking and ordering.
75 *
76 * This is handled using several linked lists of surfaces, organised into
77 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
78 * above all of the surfaces in the layer below. The set of layers is static and
79 * in the following order (top-most first):
80 * • Lock layer (only ever displayed on its own)
81 * • Cursor layer
82 * • Fullscreen layer
83 * • Panel layer
84 * • Input panel layer
85 * • Workspace layers
86 * • Background layer
87 *
88 * The list of layers may be manipulated to remove whole layers of surfaces from
89 * display. For example, when locking the screen, all layers except the lock
90 * layer are removed.
91 *
92 * A surface’s layer is modified on configuring the surface, in
93 * set_surface_type() (which is only called when the surface’s type change is
94 * _committed_). If a surface’s type changes (e.g. when making a window
95 * fullscreen) its layer changes too.
96 *
97 * In order to allow popup and transient surfaces to be correctly stacked above
98 * their parent surfaces, each surface tracks both its parent surface, and a
99 * linked list of its children. When a surface’s layer is updated, so are the
100 * layers of its children. Note that child surfaces are *not* the same as
101 * subsurfaces — child/parent surfaces are purely for maintaining stacking
102 * order.
103 *
104 * The children_link list of siblings of a surface (i.e. those surfaces which
105 * have the same parent) only contains weston_surfaces which have a
106 * shell_surface. Stacking is not implemented for non-shell_surface
107 * weston_surfaces. This means that the following implication does *not* hold:
108 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
109 */
110
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200111struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500112 struct wl_resource *resource;
113 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200114
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500115 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500116 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600117 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200118 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400119 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000120 struct wl_list children_list; /* child surfaces of this one */
121 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300122 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200123
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800124 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400125 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500126 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200127 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800128 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200129 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800130 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700131 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800132 uint32_t resize_edges;
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
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200158 struct weston_transform workspace_transform;
159
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500160 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500161 struct weston_output *output;
Rafael Antognolli65f98d82013-12-03 15:35:47 -0200162 struct weston_output *recommended_output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100163 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400164
165 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200166
167 struct {
168 bool maximized;
169 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200170 bool relative;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200171 } state, next_state; /* surface states */
172 bool state_changed;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500173
174 int focus_count;
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;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800197 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700198 wl_fixed_t dx, dy;
199};
200
Pekka Paalanen460099f2012-01-20 16:48:25 +0200201struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300202 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500203 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200204 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200205 float x;
206 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200207 } center;
208};
209
Giulio Camuffo5085a752013-03-25 21:42:45 +0100210struct shell_seat {
211 struct weston_seat *seat;
212 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500213 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100214
215 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400216 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100217 struct wl_list surfaces_list;
218 struct wl_client *client;
219 int32_t initial_up;
220 } popup_grab;
221};
222
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800223struct shell_client {
224 struct wl_resource *resource;
225 struct wl_client *client;
226 struct desktop_shell *shell;
227 struct wl_listener destroy_listener;
228 struct wl_event_source *ping_timer;
229 uint32_t ping_serial;
230 int unresponsive;
231};
232
Emilio Pozuelo Monfort1a26f1b2014-01-07 16:41:40 +0100233void
234set_alpha_if_fullscreen(struct shell_surface *shsurf)
235{
236 if (shsurf && shsurf->state.fullscreen)
237 shsurf->fullscreen.black_view->alpha = 0.25;
238}
239
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -0800240static struct shell_client *
241get_shell_client(struct wl_client *client);
242
Alex Wubd3354b2012-04-17 17:20:49 +0800243static struct desktop_shell *
244shell_surface_get_shell(struct shell_surface *shsurf);
245
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500246static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400247surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500248
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300249static void
250shell_fade_startup(struct desktop_shell *shell);
251
Philip Withnallbecb77e2013-11-25 18:01:30 +0000252static struct shell_seat *
253get_shell_seat(struct weston_seat *seat);
254
Philip Withnall648a4dd2013-11-25 18:01:44 +0000255static void
256shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
257
Alex Wubd3354b2012-04-17 17:20:49 +0800258static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200259shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
260
261static bool
262shell_surface_is_xdg_surface(struct shell_surface *shsurf);
263
264static bool
265shell_surface_is_xdg_popup(struct shell_surface *shsurf);
266
267static void
268shell_surface_set_parent(struct shell_surface *shsurf,
269 struct weston_surface *parent);
270
271static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800272shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
273{
274 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500275 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800276
277 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100278
Jason Ekstranda7af7042013-10-12 22:38:11 -0500279 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800280 return false;
281
Jason Ekstranda7af7042013-10-12 22:38:11 -0500282 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
283 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800284 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500285 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800286}
287
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500288static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400289destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300290{
291 struct shell_grab *grab;
292
293 grab = container_of(listener, struct shell_grab,
294 shsurf_destroy_listener);
295
296 grab->shsurf = NULL;
297}
298
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800299struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500300get_default_view(struct weston_surface *surface)
301{
302 struct shell_surface *shsurf;
303 struct weston_view *view;
304
305 if (!surface || wl_list_empty(&surface->views))
306 return NULL;
307
308 shsurf = get_shell_surface(surface);
309 if (shsurf)
310 return shsurf->view;
311
312 wl_list_for_each(view, &surface->views, surface_link)
313 if (weston_view_is_mapped(view))
314 return view;
315
316 return container_of(surface->views.next, struct weston_view, surface_link);
317}
318
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300319static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400320popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400321
322static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300323shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400324 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300325 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400326 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300327 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300328{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300329 struct desktop_shell *shell = shsurf->shell;
330
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400331 popup_grab_end(pointer);
332
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300333 grab->grab.interface = interface;
334 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400335 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500336 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400337 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300338
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700339 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400340 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400341 if (shell->child.desktop_shell) {
342 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
343 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500344 weston_pointer_set_focus(pointer,
345 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400346 wl_fixed_from_int(0),
347 wl_fixed_from_int(0));
348 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300349}
350
351static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300352shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300353{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700354 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400355 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700356 grab->shsurf->grabbed = 0;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800357 grab->shsurf->resize_edges = 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700358 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300359
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700360 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300361}
362
363static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700364shell_touch_grab_start(struct shell_touch_grab *grab,
365 const struct weston_touch_grab_interface *interface,
366 struct shell_surface *shsurf,
367 struct weston_touch *touch)
368{
369 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800370
Rusty Lynch1084da52013-08-15 09:10:08 -0700371 grab->grab.interface = interface;
372 grab->shsurf = shsurf;
373 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
374 wl_signal_add(&shsurf->destroy_signal,
375 &grab->shsurf_destroy_listener);
376
377 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700378 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700379
380 weston_touch_start_grab(touch, &grab->grab);
381 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500382 weston_touch_set_focus(touch->seat,
383 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700384}
385
386static void
387shell_touch_grab_end(struct shell_touch_grab *grab)
388{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700389 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700390 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700391 grab->shsurf->grabbed = 0;
392 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700393
394 weston_touch_end_grab(grab->touch);
395}
396
397static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500398center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800399 struct weston_output *output);
400
Daniel Stone496ca172012-05-30 16:31:42 +0100401static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300402get_modifier(char *modifier)
403{
404 if (!modifier)
405 return MODIFIER_SUPER;
406
407 if (!strcmp("ctrl", modifier))
408 return MODIFIER_CTRL;
409 else if (!strcmp("alt", modifier))
410 return MODIFIER_ALT;
411 else if (!strcmp("super", modifier))
412 return MODIFIER_SUPER;
413 else
414 return MODIFIER_SUPER;
415}
416
Juan Zhaoe10d2792012-04-25 19:09:52 +0800417static enum animation_type
418get_animation_type(char *animation)
419{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800420 if (!animation)
421 return ANIMATION_NONE;
422
Juan Zhaoe10d2792012-04-25 19:09:52 +0800423 if (!strcmp("zoom", animation))
424 return ANIMATION_ZOOM;
425 else if (!strcmp("fade", animation))
426 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100427 else if (!strcmp("dim-layer", animation))
428 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800429 else
430 return ANIMATION_NONE;
431}
432
Alex Wu4539b082012-03-01 12:57:46 +0800433static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400434shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200435{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400436 struct weston_config_section *section;
437 int duration;
438 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200439
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400440 section = weston_config_get_section(shell->compositor->config,
441 "screensaver", NULL, NULL);
442 weston_config_section_get_string(section,
443 "path", &shell->screensaver.path, NULL);
444 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200445 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400446
447 section = weston_config_get_section(shell->compositor->config,
448 "shell", NULL, NULL);
449 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100450 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100451 shell->client = s;
452 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400453 "binding-modifier", &s, "super");
454 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200455 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800456
457 weston_config_section_get_string(section,
458 "exposay-modifier", &s, "none");
459 if (strcmp(s, "none") == 0)
460 shell->exposay_modifier = 0;
461 else
462 shell->exposay_modifier = get_modifier(s);
463 free(s);
464
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400465 weston_config_section_get_string(section, "animation", &s, "none");
466 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200467 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700468 weston_config_section_get_string(section,
469 "startup-animation", &s, "fade");
470 shell->startup_animation_type = get_animation_type(s);
471 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700472 if (shell->startup_animation_type == ANIMATION_ZOOM)
473 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100474 weston_config_section_get_string(section, "focus-animation", &s, "none");
475 shell->focus_animation_type = get_animation_type(s);
476 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400477 weston_config_section_get_uint(section, "num-workspaces",
478 &shell->workspaces.num,
479 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200480}
481
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800482struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100483get_default_output(struct weston_compositor *compositor)
484{
485 return container_of(compositor->output_list.next,
486 struct weston_output, link);
487}
488
489
490/* no-op func for checking focus surface */
491static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600492focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100493{
494}
495
496static struct focus_surface *
497get_focus_surface(struct weston_surface *surface)
498{
499 if (surface->configure == focus_surface_configure)
500 return surface->configure_private;
501 else
502 return NULL;
503}
504
505static bool
506is_focus_surface (struct weston_surface *es)
507{
508 return (es->configure == focus_surface_configure);
509}
510
511static bool
512is_focus_view (struct weston_view *view)
513{
514 return is_focus_surface (view->surface);
515}
516
517static struct focus_surface *
518create_focus_surface(struct weston_compositor *ec,
519 struct weston_output *output)
520{
521 struct focus_surface *fsurf = NULL;
522 struct weston_surface *surface = NULL;
523
524 fsurf = malloc(sizeof *fsurf);
525 if (!fsurf)
526 return NULL;
527
528 fsurf->surface = weston_surface_create(ec);
529 surface = fsurf->surface;
530 if (surface == NULL) {
531 free(fsurf);
532 return NULL;
533 }
534
535 surface->configure = focus_surface_configure;
536 surface->output = output;
537 surface->configure_private = fsurf;
538
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800539 fsurf->view = weston_view_create(surface);
540 if (fsurf->view == NULL) {
541 weston_surface_destroy(surface);
542 free(fsurf);
543 return NULL;
544 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100545 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100546
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600547 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600548 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100549 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
550 pixman_region32_fini(&surface->opaque);
551 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
552 output->width, output->height);
553 pixman_region32_fini(&surface->input);
554 pixman_region32_init(&surface->input);
555
556 wl_list_init(&fsurf->workspace_transform.link);
557
558 return fsurf;
559}
560
561static void
562focus_surface_destroy(struct focus_surface *fsurf)
563{
564 weston_surface_destroy(fsurf->surface);
565 free(fsurf);
566}
567
568static void
569focus_animation_done(struct weston_view_animation *animation, void *data)
570{
571 struct workspace *ws = data;
572
573 ws->focus_animation = NULL;
574}
575
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200576static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200577focus_state_destroy(struct focus_state *state)
578{
579 wl_list_remove(&state->seat_destroy_listener.link);
580 wl_list_remove(&state->surface_destroy_listener.link);
581 free(state);
582}
583
584static void
585focus_state_seat_destroy(struct wl_listener *listener, void *data)
586{
587 struct focus_state *state = container_of(listener,
588 struct focus_state,
589 seat_destroy_listener);
590
591 wl_list_remove(&state->link);
592 focus_state_destroy(state);
593}
594
595static void
596focus_state_surface_destroy(struct wl_listener *listener, void *data)
597{
598 struct focus_state *state = container_of(listener,
599 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400600 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400601 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500602 struct weston_surface *main_surface, *next;
603 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200604
Pekka Paalanen01388e22013-04-25 13:57:44 +0300605 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
606
Kristian Høgsberge3778222012-07-31 17:29:30 -0400607 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500608 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
609 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400610 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100611 if (is_focus_view(view))
612 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400613
Jason Ekstranda7af7042013-10-12 22:38:11 -0500614 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400615 break;
616 }
617
Pekka Paalanen01388e22013-04-25 13:57:44 +0300618 /* if the focus was a sub-surface, activate its main surface */
619 if (main_surface != state->keyboard_focus)
620 next = main_surface;
621
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100622 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400623 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100624 state->keyboard_focus = NULL;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400625 activate(shell, next, state->seat);
626 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100627 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
628 if (state->ws->focus_animation)
629 weston_view_animation_destroy(state->ws->focus_animation);
630
631 state->ws->focus_animation = weston_fade_run(
632 state->ws->fsurf_front->view,
633 state->ws->fsurf_front->view->alpha, 0.0, 300,
634 focus_animation_done, state->ws);
635 }
636
Kristian Høgsberge3778222012-07-31 17:29:30 -0400637 wl_list_remove(&state->link);
638 focus_state_destroy(state);
639 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200640}
641
642static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400643focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200644{
Jonas Ådahl04769742012-06-13 00:01:24 +0200645 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200646
647 state = malloc(sizeof *state);
648 if (state == NULL)
649 return NULL;
650
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100651 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400652 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200653 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400654 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200655
656 state->seat_destroy_listener.notify = focus_state_seat_destroy;
657 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400658 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200659 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400660 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200661
662 return state;
663}
664
Jonas Ådahl8538b222012-08-29 22:13:03 +0200665static struct focus_state *
666ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
667{
668 struct workspace *ws = get_current_workspace(shell);
669 struct focus_state *state;
670
671 wl_list_for_each(state, &ws->focus_list, link)
672 if (state->seat == seat)
673 break;
674
675 if (&state->link == &ws->focus_list)
676 state = focus_state_create(seat, ws);
677
678 return state;
679}
680
Jonas Ådahl04769742012-06-13 00:01:24 +0200681static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800682focus_state_set_focus(struct focus_state *state,
683 struct weston_surface *surface)
684{
685 if (state->keyboard_focus) {
686 wl_list_remove(&state->surface_destroy_listener.link);
687 wl_list_init(&state->surface_destroy_listener.link);
688 }
689
690 state->keyboard_focus = surface;
691 if (surface)
692 wl_signal_add(&surface->destroy_signal,
693 &state->surface_destroy_listener);
694}
695
696static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400697restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200698{
699 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400700 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200701
702 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800703 if (state->seat->keyboard == NULL)
704 continue;
705
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400706 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200707
Kristian Høgsberge3148752013-05-06 23:19:49 -0400708 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200709 }
710}
711
712static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200713replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
714 struct weston_seat *seat)
715{
716 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200717
718 wl_list_for_each(state, &ws->focus_list, link) {
719 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800720 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200721 return;
722 }
723 }
724}
725
726static void
727drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
728 struct weston_surface *surface)
729{
730 struct focus_state *state;
731
732 wl_list_for_each(state, &ws->focus_list, link)
733 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800734 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200735}
736
737static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100738animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
739 struct weston_view *from, struct weston_view *to)
740{
741 struct weston_output *output;
742 bool focus_surface_created = false;
743
744 /* FIXME: Only support dim animation using two layers */
745 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
746 return;
747
748 output = get_default_output(shell->compositor);
749 if (ws->fsurf_front == NULL && (from || to)) {
750 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800751 if (ws->fsurf_front == NULL)
752 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100753 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800754
755 ws->fsurf_back = create_focus_surface(shell->compositor, output);
756 if (ws->fsurf_back == NULL) {
757 focus_surface_destroy(ws->fsurf_front);
758 return;
759 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100760 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800761
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100762 focus_surface_created = true;
763 } else {
764 wl_list_remove(&ws->fsurf_front->view->layer_link);
765 wl_list_remove(&ws->fsurf_back->view->layer_link);
766 }
767
768 if (ws->focus_animation) {
769 weston_view_animation_destroy(ws->focus_animation);
770 ws->focus_animation = NULL;
771 }
772
773 if (to)
774 wl_list_insert(&to->layer_link,
775 &ws->fsurf_front->view->layer_link);
776 else if (from)
777 wl_list_insert(&ws->layer.view_list,
778 &ws->fsurf_front->view->layer_link);
779
780 if (focus_surface_created) {
781 ws->focus_animation = weston_fade_run(
782 ws->fsurf_front->view,
783 ws->fsurf_front->view->alpha, 0.6, 300,
784 focus_animation_done, ws);
785 } else if (from) {
786 wl_list_insert(&from->layer_link,
787 &ws->fsurf_back->view->layer_link);
788 ws->focus_animation = weston_stable_fade_run(
789 ws->fsurf_front->view, 0.0,
790 ws->fsurf_back->view, 0.6,
791 focus_animation_done, ws);
792 } else if (to) {
793 wl_list_insert(&ws->layer.view_list,
794 &ws->fsurf_back->view->layer_link);
795 ws->focus_animation = weston_stable_fade_run(
796 ws->fsurf_front->view, 0.0,
797 ws->fsurf_back->view, 0.6,
798 focus_animation_done, ws);
799 }
800}
801
802static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200803workspace_destroy(struct workspace *ws)
804{
Jonas Ådahl04769742012-06-13 00:01:24 +0200805 struct focus_state *state, *next;
806
807 wl_list_for_each_safe(state, next, &ws->focus_list, link)
808 focus_state_destroy(state);
809
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100810 if (ws->fsurf_front)
811 focus_surface_destroy(ws->fsurf_front);
812 if (ws->fsurf_back)
813 focus_surface_destroy(ws->fsurf_back);
814
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200815 free(ws);
816}
817
Jonas Ådahl04769742012-06-13 00:01:24 +0200818static void
819seat_destroyed(struct wl_listener *listener, void *data)
820{
821 struct weston_seat *seat = data;
822 struct focus_state *state, *next;
823 struct workspace *ws = container_of(listener,
824 struct workspace,
825 seat_destroyed_listener);
826
827 wl_list_for_each_safe(state, next, &ws->focus_list, link)
828 if (state->seat == seat)
829 wl_list_remove(&state->link);
830}
831
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200832static struct workspace *
833workspace_create(void)
834{
835 struct workspace *ws = malloc(sizeof *ws);
836 if (ws == NULL)
837 return NULL;
838
839 weston_layer_init(&ws->layer, NULL);
840
Jonas Ådahl04769742012-06-13 00:01:24 +0200841 wl_list_init(&ws->focus_list);
842 wl_list_init(&ws->seat_destroyed_listener.link);
843 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100844 ws->fsurf_front = NULL;
845 ws->fsurf_back = NULL;
846 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200847
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200848 return ws;
849}
850
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200851static int
852workspace_is_empty(struct workspace *ws)
853{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500854 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200855}
856
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200857static struct workspace *
858get_workspace(struct desktop_shell *shell, unsigned int index)
859{
860 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200861 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200862 pws += index;
863 return *pws;
864}
865
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800866struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200867get_current_workspace(struct desktop_shell *shell)
868{
869 return get_workspace(shell, shell->workspaces.current);
870}
871
872static void
873activate_workspace(struct desktop_shell *shell, unsigned int index)
874{
875 struct workspace *ws;
876
877 ws = get_workspace(shell, index);
878 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
879
880 shell->workspaces.current = index;
881}
882
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200883static unsigned int
884get_output_height(struct weston_output *output)
885{
886 return abs(output->region.extents.y1 - output->region.extents.y2);
887}
888
889static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100890view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200891{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200892 struct weston_transform *transform;
893
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100894 if (is_focus_view(view)) {
895 struct focus_surface *fsurf = get_focus_surface(view->surface);
896 transform = &fsurf->workspace_transform;
897 } else {
898 struct shell_surface *shsurf = get_shell_surface(view->surface);
899 transform = &shsurf->workspace_transform;
900 }
901
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200902 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500903 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100904 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200905
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100906 weston_matrix_init(&transform->matrix);
907 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200908 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500909 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200910}
911
912static void
913workspace_translate_out(struct workspace *ws, double fraction)
914{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500915 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200916 unsigned int height;
917 double d;
918
Jason Ekstranda7af7042013-10-12 22:38:11 -0500919 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
920 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200921 d = height * fraction;
922
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100923 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200924 }
925}
926
927static void
928workspace_translate_in(struct workspace *ws, double fraction)
929{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500930 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200931 unsigned int height;
932 double d;
933
Jason Ekstranda7af7042013-10-12 22:38:11 -0500934 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
935 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200936
937 if (fraction > 0)
938 d = -(height - height * fraction);
939 else
940 d = height + height * fraction;
941
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100942 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200943 }
944}
945
946static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200947broadcast_current_workspace_state(struct desktop_shell *shell)
948{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700949 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200950
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -0700951 wl_resource_for_each(resource, &shell->workspaces.client_list)
952 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +0200953 shell->workspaces.current,
954 shell->workspaces.num);
955}
956
957static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200958reverse_workspace_change_animation(struct desktop_shell *shell,
959 unsigned int index,
960 struct workspace *from,
961 struct workspace *to)
962{
963 shell->workspaces.current = index;
964
965 shell->workspaces.anim_to = to;
966 shell->workspaces.anim_from = from;
967 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
968 shell->workspaces.anim_timestamp = 0;
969
Scott Moreau4272e632012-08-13 09:58:41 -0600970 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200971}
972
973static void
974workspace_deactivate_transforms(struct workspace *ws)
975{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100977 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200978
Jason Ekstranda7af7042013-10-12 22:38:11 -0500979 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100980 if (is_focus_view(view)) {
981 struct focus_surface *fsurf = get_focus_surface(view->surface);
982 transform = &fsurf->workspace_transform;
983 } else {
984 struct shell_surface *shsurf = get_shell_surface(view->surface);
985 transform = &shsurf->workspace_transform;
986 }
987
988 if (!wl_list_empty(&transform->link)) {
989 wl_list_remove(&transform->link);
990 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200991 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500992 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200993 }
994}
995
996static void
997finish_workspace_change_animation(struct desktop_shell *shell,
998 struct workspace *from,
999 struct workspace *to)
1000{
Scott Moreau4272e632012-08-13 09:58:41 -06001001 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001002
1003 wl_list_remove(&shell->workspaces.animation.link);
1004 workspace_deactivate_transforms(from);
1005 workspace_deactivate_transforms(to);
1006 shell->workspaces.anim_to = NULL;
1007
1008 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1009}
1010
1011static void
1012animate_workspace_change_frame(struct weston_animation *animation,
1013 struct weston_output *output, uint32_t msecs)
1014{
1015 struct desktop_shell *shell =
1016 container_of(animation, struct desktop_shell,
1017 workspaces.animation);
1018 struct workspace *from = shell->workspaces.anim_from;
1019 struct workspace *to = shell->workspaces.anim_to;
1020 uint32_t t;
1021 double x, y;
1022
1023 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1024 finish_workspace_change_animation(shell, from, to);
1025 return;
1026 }
1027
1028 if (shell->workspaces.anim_timestamp == 0) {
1029 if (shell->workspaces.anim_current == 0.0)
1030 shell->workspaces.anim_timestamp = msecs;
1031 else
1032 shell->workspaces.anim_timestamp =
1033 msecs -
1034 /* Invers of movement function 'y' below. */
1035 (asin(1.0 - shell->workspaces.anim_current) *
1036 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1037 M_2_PI);
1038 }
1039
1040 t = msecs - shell->workspaces.anim_timestamp;
1041
1042 /*
1043 * x = [0, π/2]
1044 * y(x) = sin(x)
1045 */
1046 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1047 y = sin(x);
1048
1049 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001050 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001051
1052 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1053 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1054 shell->workspaces.anim_current = y;
1055
Scott Moreau4272e632012-08-13 09:58:41 -06001056 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001057 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001058 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001059 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001060}
1061
1062static void
1063animate_workspace_change(struct desktop_shell *shell,
1064 unsigned int index,
1065 struct workspace *from,
1066 struct workspace *to)
1067{
1068 struct weston_output *output;
1069
1070 int dir;
1071
1072 if (index > shell->workspaces.current)
1073 dir = -1;
1074 else
1075 dir = 1;
1076
1077 shell->workspaces.current = index;
1078
1079 shell->workspaces.anim_dir = dir;
1080 shell->workspaces.anim_from = from;
1081 shell->workspaces.anim_to = to;
1082 shell->workspaces.anim_current = 0.0;
1083 shell->workspaces.anim_timestamp = 0;
1084
1085 output = container_of(shell->compositor->output_list.next,
1086 struct weston_output, link);
1087 wl_list_insert(&output->animation_list,
1088 &shell->workspaces.animation.link);
1089
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001090 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001091
1092 workspace_translate_in(to, 0);
1093
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001094 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001095
Scott Moreau4272e632012-08-13 09:58:41 -06001096 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001097}
1098
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001099static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001100update_workspace(struct desktop_shell *shell, unsigned int index,
1101 struct workspace *from, struct workspace *to)
1102{
1103 shell->workspaces.current = index;
1104 wl_list_insert(&from->layer.link, &to->layer.link);
1105 wl_list_remove(&from->layer.link);
1106}
1107
1108static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001109change_workspace(struct desktop_shell *shell, unsigned int index)
1110{
1111 struct workspace *from;
1112 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001113 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001114
1115 if (index == shell->workspaces.current)
1116 return;
1117
1118 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001119 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001120 return;
1121
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001122 from = get_current_workspace(shell);
1123 to = get_workspace(shell, index);
1124
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001125 if (shell->workspaces.anim_from == to &&
1126 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001127 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001128 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001129 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001130 return;
1131 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001132
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001133 if (shell->workspaces.anim_to != NULL)
1134 finish_workspace_change_animation(shell,
1135 shell->workspaces.anim_from,
1136 shell->workspaces.anim_to);
1137
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001138 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001139
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001140 if (shell->focus_animation_type != ANIMATION_NONE) {
1141 wl_list_for_each(state, &from->focus_list, link)
1142 if (state->keyboard_focus)
1143 animate_focus_change(shell, from,
1144 get_default_view(state->keyboard_focus), NULL);
1145
1146 wl_list_for_each(state, &to->focus_list, link)
1147 if (state->keyboard_focus)
1148 animate_focus_change(shell, to,
1149 NULL, get_default_view(state->keyboard_focus));
1150 }
1151
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001152 if (workspace_is_empty(to) && workspace_is_empty(from))
1153 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001154 else
1155 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001156
1157 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001158}
1159
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001160static bool
1161workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1162{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001163 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001164 struct wl_list *e;
1165
1166 if (wl_list_empty(list))
1167 return false;
1168
1169 e = list->next;
1170
1171 if (e->next != list)
1172 return false;
1173
Jason Ekstranda7af7042013-10-12 22:38:11 -05001174 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001175}
1176
1177static void
Philip Withnall659163d2013-11-25 18:01:36 +00001178move_surface_to_workspace(struct desktop_shell *shell,
1179 struct shell_surface *shsurf,
1180 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001181{
1182 struct workspace *from;
1183 struct workspace *to;
1184 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001185 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001186 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001187
1188 if (workspace == shell->workspaces.current)
1189 return;
1190
Philip Withnall659163d2013-11-25 18:01:36 +00001191 view = get_default_view(shsurf->surface);
1192 if (!view)
1193 return;
1194
1195 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1196
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001197 if (workspace >= shell->workspaces.num)
1198 workspace = shell->workspaces.num - 1;
1199
Jonas Ådahle9d22502012-08-29 22:13:01 +02001200 from = get_current_workspace(shell);
1201 to = get_workspace(shell, workspace);
1202
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 wl_list_remove(&view->layer_link);
1204 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001205
Philip Withnall648a4dd2013-11-25 18:01:44 +00001206 shell_surface_update_child_surface_layers(shsurf);
1207
Jason Ekstranda7af7042013-10-12 22:38:11 -05001208 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001209 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1210 if (!seat->keyboard)
1211 continue;
1212
1213 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001215 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001216 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001217
Jason Ekstranda7af7042013-10-12 22:38:11 -05001218 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001219}
1220
1221static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001222take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001223 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001224 unsigned int index)
1225{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001226 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001228 struct shell_surface *shsurf;
1229 struct workspace *from;
1230 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001231 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001232
Pekka Paalanen01388e22013-04-25 13:57:44 +03001233 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001234 view = get_default_view(surface);
1235 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001236 index == shell->workspaces.current ||
1237 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001238 return;
1239
1240 from = get_current_workspace(shell);
1241 to = get_workspace(shell, index);
1242
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 wl_list_remove(&view->layer_link);
1244 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001245
Philip Withnall659163d2013-11-25 18:01:36 +00001246 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001247 if (shsurf != NULL)
1248 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001249
Jonas Ådahle9d22502012-08-29 22:13:01 +02001250 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001251 drop_focus_state(shell, from, surface);
1252
1253 if (shell->workspaces.anim_from == to &&
1254 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001255 wl_list_remove(&to->layer.link);
1256 wl_list_insert(from->layer.link.prev, &to->layer.link);
1257
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001258 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001259 broadcast_current_workspace_state(shell);
1260
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001261 return;
1262 }
1263
1264 if (shell->workspaces.anim_to != NULL)
1265 finish_workspace_change_animation(shell,
1266 shell->workspaces.anim_from,
1267 shell->workspaces.anim_to);
1268
1269 if (workspace_is_empty(from) &&
1270 workspace_has_only(to, surface))
1271 update_workspace(shell, index, from, to);
1272 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001273 if (shsurf != NULL &&
1274 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001275 wl_list_insert(&shell->workspaces.anim_sticky_list,
1276 &shsurf->workspace_transform.link);
1277
1278 animate_workspace_change(shell, index, from, to);
1279 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001280
1281 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001282
1283 state = ensure_focus_state(shell, seat);
1284 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001285 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001286}
1287
1288static void
1289workspace_manager_move_surface(struct wl_client *client,
1290 struct wl_resource *resource,
1291 struct wl_resource *surface_resource,
1292 uint32_t workspace)
1293{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001294 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001295 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001296 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001297 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001298 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001299
Pekka Paalanen01388e22013-04-25 13:57:44 +03001300 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001301 shell_surface = get_shell_surface(main_surface);
1302 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001304
1305 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001306}
1307
1308static const struct workspace_manager_interface workspace_manager_implementation = {
1309 workspace_manager_move_surface,
1310};
1311
1312static void
1313unbind_resource(struct wl_resource *resource)
1314{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001315 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001316}
1317
1318static void
1319bind_workspace_manager(struct wl_client *client,
1320 void *data, uint32_t version, uint32_t id)
1321{
1322 struct desktop_shell *shell = data;
1323 struct wl_resource *resource;
1324
Jason Ekstranda85118c2013-06-27 20:17:02 -05001325 resource = wl_resource_create(client,
1326 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001327
1328 if (resource == NULL) {
1329 weston_log("couldn't add workspace manager object");
1330 return;
1331 }
1332
Jason Ekstranda85118c2013-06-27 20:17:02 -05001333 wl_resource_set_implementation(resource,
1334 &workspace_manager_implementation,
1335 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001336 wl_list_insert(&shell->workspaces.client_list,
1337 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001338
1339 workspace_manager_send_state(resource,
1340 shell->workspaces.current,
1341 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001342}
1343
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001344static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001345touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1346 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1347{
1348}
1349
1350static void
1351touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1352{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001353 struct weston_touch_move_grab *move =
1354 (struct weston_touch_move_grab *) container_of(
1355 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001356
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001357 if (touch_id == 0)
1358 move->active = 0;
1359
Jonas Ådahl9484b692013-12-02 22:05:03 +01001360 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001361 shell_touch_grab_end(&move->base);
1362 free(move);
1363 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001364}
1365
1366static void
1367touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1368 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1369{
1370 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1371 struct shell_surface *shsurf = move->base.shsurf;
1372 struct weston_surface *es;
1373 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1374 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1375
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001376 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001377 return;
1378
1379 es = shsurf->surface;
1380
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001381 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001382
1383 weston_compositor_schedule_repaint(es->compositor);
1384}
1385
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001386static void
1387touch_move_grab_cancel(struct weston_touch_grab *grab)
1388{
1389 struct weston_touch_move_grab *move =
1390 (struct weston_touch_move_grab *) container_of(
1391 grab, struct shell_touch_grab, grab);
1392
1393 shell_touch_grab_end(&move->base);
1394 free(move);
1395}
1396
Rusty Lynch1084da52013-08-15 09:10:08 -07001397static const struct weston_touch_grab_interface touch_move_grab_interface = {
1398 touch_move_grab_down,
1399 touch_move_grab_up,
1400 touch_move_grab_motion,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001401 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001402};
1403
1404static int
1405surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1406{
1407 struct weston_touch_move_grab *move;
1408
1409 if (!shsurf)
1410 return -1;
1411
Rafael Antognolli03b16592013-12-03 15:35:42 -02001412 if (shsurf->state.fullscreen)
Rusty Lynch1084da52013-08-15 09:10:08 -07001413 return 0;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001414 if (shsurf->grabbed)
1415 return 0;
Rusty Lynch1084da52013-08-15 09:10:08 -07001416
1417 move = malloc(sizeof *move);
1418 if (!move)
1419 return -1;
1420
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001421 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001422 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001423 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001425 seat->touch->grab_y;
1426
1427 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1428 seat->touch);
1429
1430 return 0;
1431}
1432
1433static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001434noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001435{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001436}
1437
1438static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001439move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1440 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001441{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001442 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001443 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001444 struct shell_surface *shsurf = move->base.shsurf;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001445 int dx, dy;
1446
1447 weston_pointer_move(pointer, x, y);
1448 dx = wl_fixed_to_int(pointer->x + move->dx);
1449 dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001450
1451 if (!shsurf)
1452 return;
1453
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001454 weston_view_set_position(shsurf->view, dx, dy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001455
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001457}
1458
1459static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001460move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001461 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001462{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001463 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1464 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001465 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001466 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001467
Daniel Stone4dbadb12012-05-30 16:31:51 +01001468 if (pointer->button_count == 0 &&
1469 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001470 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001471 free(grab);
1472 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001473}
1474
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001475static void
1476move_grab_cancel(struct weston_pointer_grab *grab)
1477{
1478 struct shell_grab *shell_grab =
1479 container_of(grab, struct shell_grab, grab);
1480
1481 shell_grab_end(shell_grab);
1482 free(grab);
1483}
1484
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001485static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001486 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001487 move_grab_motion,
1488 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001489 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001490};
1491
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001492static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001493surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001494{
1495 struct weston_move_grab *move;
1496
1497 if (!shsurf)
1498 return -1;
1499
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -07001500 if (shsurf->grabbed)
1501 return 0;
Ricardo Vieiraf1c3bd82014-01-18 16:30:50 +00001502 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001503 return 0;
1504
1505 move = malloc(sizeof *move);
1506 if (!move)
1507 return -1;
1508
Jason Ekstranda7af7042013-10-12 22:38:11 -05001509 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001510 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001512 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001513
1514 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001515 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001516
1517 return 0;
1518}
1519
1520static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001521common_surface_move(struct wl_resource *resource,
1522 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001523{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001524 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001525 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001526 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001527
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001528 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001529 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001530 seat->pointer->button_count > 0 &&
1531 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001532 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001533 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001534 (surface_move(shsurf, seat) < 0))
1535 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001536 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001537 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001538 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001539 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001540 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001541 (surface_touch_move(shsurf, seat) < 0))
1542 wl_resource_post_no_memory(resource);
1543 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001544}
1545
Rafael Antognollie2a34552013-12-03 15:35:45 -02001546static void
1547shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1548 struct wl_resource *seat_resource, uint32_t serial)
1549{
1550 common_surface_move(resource, seat_resource, serial);
1551}
1552
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001553struct weston_resize_grab {
1554 struct shell_grab base;
1555 uint32_t edges;
1556 int32_t width, height;
1557};
1558
1559static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001560resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1561 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001562{
1563 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001564 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001565 struct shell_surface *shsurf = resize->base.shsurf;
1566 int32_t width, height;
1567 wl_fixed_t from_x, from_y;
1568 wl_fixed_t to_x, to_y;
1569
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001570 weston_pointer_move(pointer, x, y);
1571
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001572 if (!shsurf)
1573 return;
1574
Jason Ekstranda7af7042013-10-12 22:38:11 -05001575 weston_view_from_global_fixed(shsurf->view,
1576 pointer->grab_x, pointer->grab_y,
1577 &from_x, &from_y);
1578 weston_view_from_global_fixed(shsurf->view,
1579 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001580
1581 width = resize->width;
1582 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1583 width += wl_fixed_to_int(from_x - to_x);
1584 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1585 width += wl_fixed_to_int(to_x - from_x);
1586 }
1587
1588 height = resize->height;
1589 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1590 height += wl_fixed_to_int(from_y - to_y);
1591 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1592 height += wl_fixed_to_int(to_y - from_y);
1593 }
1594
1595 shsurf->client->send_configure(shsurf->surface,
1596 resize->edges, width, height);
1597}
1598
1599static void
1600send_configure(struct weston_surface *surface,
1601 uint32_t edges, int32_t width, int32_t height)
1602{
1603 struct shell_surface *shsurf = get_shell_surface(surface);
1604
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001605 assert(shsurf);
1606
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001607 wl_shell_surface_send_configure(shsurf->resource,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001608 edges, width, height);
1609}
1610
1611static const struct weston_shell_client shell_client = {
1612 send_configure
1613};
1614
1615static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001616resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001617 uint32_t time, uint32_t button, uint32_t state_w)
1618{
1619 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001620 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001621 enum wl_pointer_button_state state = state_w;
1622
1623 if (pointer->button_count == 0 &&
1624 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1625 shell_grab_end(&resize->base);
1626 free(grab);
1627 }
1628}
1629
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001630static void
1631resize_grab_cancel(struct weston_pointer_grab *grab)
1632{
1633 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1634
1635 shell_grab_end(&resize->base);
1636 free(grab);
1637}
1638
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001639static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001640 noop_grab_focus,
1641 resize_grab_motion,
1642 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001643 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001644};
1645
Giulio Camuffob8366642013-04-25 13:57:46 +03001646/*
1647 * Returns the bounding box of a surface and all its sub-surfaces,
1648 * in the surface coordinates system. */
1649static void
1650surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1651 int32_t *y, int32_t *w, int32_t *h) {
1652 pixman_region32_t region;
1653 pixman_box32_t *box;
1654 struct weston_subsurface *subsurface;
1655
1656 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001657 surface->width,
1658 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001659
1660 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1661 pixman_region32_union_rect(&region, &region,
1662 subsurface->position.x,
1663 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664 subsurface->surface->width,
1665 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001666 }
1667
1668 box = pixman_region32_extents(&region);
1669 if (x)
1670 *x = box->x1;
1671 if (y)
1672 *y = box->y1;
1673 if (w)
1674 *w = box->x2 - box->x1;
1675 if (h)
1676 *h = box->y2 - box->y1;
1677
1678 pixman_region32_fini(&region);
1679}
1680
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001681static int
1682surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001683 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001684{
1685 struct weston_resize_grab *resize;
1686
Rafael Antognolli03b16592013-12-03 15:35:42 -02001687 if (shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001688 return 0;
1689
1690 if (edges == 0 || edges > 15 ||
1691 (edges & 3) == 3 || (edges & 12) == 12)
1692 return 0;
1693
1694 resize = malloc(sizeof *resize);
1695 if (!resize)
1696 return -1;
1697
1698 resize->edges = edges;
Giulio Camuffob8366642013-04-25 13:57:46 +03001699 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1700 &resize->width, &resize->height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001701
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001702 shsurf->resize_edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001703 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001704 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001705
1706 return 0;
1707}
1708
1709static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001710common_surface_resize(struct wl_resource *resource,
1711 struct wl_resource *seat_resource, uint32_t serial,
1712 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001713{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001714 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001715 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001716 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001717
Rafael Antognolli03b16592013-12-03 15:35:42 -02001718 if (shsurf->state.fullscreen)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001719 return;
1720
Kristian Høgsberge3148752013-05-06 23:19:49 -04001721 if (seat->pointer->button_count == 0 ||
1722 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001723 seat->pointer->focus == NULL)
1724 return;
1725
1726 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1727 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001728 return;
1729
Kristian Høgsberge3148752013-05-06 23:19:49 -04001730 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001731 wl_resource_post_no_memory(resource);
1732}
1733
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001734static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001735shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1736 struct wl_resource *seat_resource, uint32_t serial,
1737 uint32_t edges)
1738{
1739 common_surface_resize(resource, seat_resource, serial, edges);
1740}
1741
1742static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001743busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001744{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001745 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001746 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001747 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001748 wl_fixed_t sx, sy;
1749
Jason Ekstranda7af7042013-10-12 22:38:11 -05001750 view = weston_compositor_pick_view(pointer->seat->compositor,
1751 pointer->x, pointer->y,
1752 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001753
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001754 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1755 shell_grab_end(grab);
1756 free(grab);
1757 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001758}
1759
1760static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001761busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1762 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001763{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001764 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001765}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001766
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001767static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001768busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001769 uint32_t time, uint32_t button, uint32_t state)
1770{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001771 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001772 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001773 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001774
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001775 if (shsurf && button == BTN_LEFT && state) {
1776 activate(shsurf->shell, shsurf->surface, seat);
1777 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001778 } else if (shsurf && button == BTN_RIGHT && state) {
1779 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001780 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001781 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001782}
1783
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001784static void
1785busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1786{
1787 struct shell_grab *grab = (struct shell_grab *) base;
1788
1789 shell_grab_end(grab);
1790 free(grab);
1791}
1792
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001793static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001794 busy_cursor_grab_focus,
1795 busy_cursor_grab_motion,
1796 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001797 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001798};
1799
1800static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001801set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001802{
1803 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001804
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001805 if (pointer->grab->interface == &busy_cursor_grab_interface)
1806 return;
1807
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001808 grab = malloc(sizeof *grab);
1809 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001810 return;
1811
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001812 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1813 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001814}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001815
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001816static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001817end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001818{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001819 struct shell_grab *grab;
1820 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001821
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001822 wl_list_for_each(seat, &compositor->seat_list, link) {
1823 if (seat->pointer == NULL)
1824 continue;
1825
1826 grab = (struct shell_grab *) seat->pointer->grab;
1827 if (grab->grab.interface == &busy_cursor_grab_interface &&
1828 wl_resource_get_client(grab->shsurf->resource) == client) {
1829 shell_grab_end(grab);
1830 free(grab);
1831 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001832 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001833}
1834
Scott Moreau9521d5e2012-04-19 13:06:17 -06001835static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001836handle_shell_client_destroy(struct wl_listener *listener, void *data);
1837
1838static int
1839xdg_ping_timeout_handler(void *data)
1840{
1841 struct shell_client *sc = data;
1842 struct weston_seat *seat;
1843 struct shell_surface *shsurf;
1844
1845 /* Client is not responding */
1846 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001847 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1848 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1849 continue;
1850 if (seat->pointer->focus->surface->resource == NULL)
1851 continue;
1852
1853 shsurf = get_shell_surface(seat->pointer->focus->surface);
1854 if (shsurf &&
1855 wl_resource_get_client(shsurf->resource) == sc->client)
1856 set_busy_cursor(shsurf, seat->pointer);
1857 }
1858
1859 return 1;
1860}
1861
1862static void
1863handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1864{
1865 struct weston_compositor *compositor = shsurf->shell->compositor;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001866 struct wl_client *client = wl_resource_get_client(shsurf->resource);
1867 struct shell_client *sc;
1868 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001869 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001870
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001871 sc = get_shell_client(client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001872 if (sc->unresponsive) {
1873 xdg_ping_timeout_handler(sc);
1874 return;
1875 }
1876
1877 sc->ping_serial = serial;
1878 loop = wl_display_get_event_loop(compositor->wl_display);
1879 if (sc->ping_timer == NULL)
1880 sc->ping_timer =
1881 wl_event_loop_add_timer(loop,
1882 xdg_ping_timeout_handler, sc);
1883 if (sc->ping_timer == NULL)
1884 return;
1885
1886 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1887
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001888 if (shell_surface_is_xdg_surface(shsurf) ||
1889 shell_surface_is_xdg_popup(shsurf))
1890 xdg_shell_send_ping(sc->resource, serial);
1891 else if (shell_surface_is_wl_shell_surface(shsurf))
1892 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001893}
1894
Scott Moreauff1db4a2012-04-17 19:06:18 -06001895static void
1896ping_handler(struct weston_surface *surface, uint32_t serial)
1897{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001898 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001899
1900 if (!shsurf)
1901 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001902 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001903 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001904 if (shsurf->surface == shsurf->shell->grab_surface)
1905 return;
1906
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08001907 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001908}
1909
1910static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001911handle_pointer_focus(struct wl_listener *listener, void *data)
1912{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001913 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001914 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001915 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001916 uint32_t serial;
1917
Jason Ekstranda7af7042013-10-12 22:38:11 -05001918 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001919 return;
1920
Jason Ekstranda7af7042013-10-12 22:38:11 -05001921 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08001922 serial = wl_display_next_serial(compositor->wl_display);
1923 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001924}
1925
1926static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001927create_pointer_focus_listener(struct weston_seat *seat)
1928{
1929 struct wl_listener *listener;
1930
Kristian Høgsberge3148752013-05-06 23:19:49 -04001931 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001932 return;
1933
1934 listener = malloc(sizeof *listener);
1935 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001936 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001937}
1938
1939static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001940shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1941{
1942 if (--shsurf->focus_count == 0)
1943 if (shell_surface_is_xdg_surface(shsurf))
1944 xdg_surface_send_focused_unset(shsurf->resource);
1945}
1946
1947static void
1948shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1949{
1950 if (shsurf->focus_count++ == 0)
1951 if (shell_surface_is_xdg_surface(shsurf))
1952 xdg_surface_send_focused_set(shsurf->resource);
1953}
1954
1955static void
1956handle_keyboard_focus(struct wl_listener *listener, void *data)
1957{
1958 struct weston_keyboard *keyboard = data;
1959 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1960
1961 if (seat->focused_surface) {
1962 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1963 if (shsurf)
1964 shell_surface_lose_keyboard_focus(shsurf);
1965 }
1966
1967 seat->focused_surface = keyboard->focus;
1968
1969 if (seat->focused_surface) {
1970 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1971 if (shsurf)
1972 shell_surface_gain_keyboard_focus(shsurf);
1973 }
1974}
1975
1976static void
1977create_keyboard_focus_listener(struct weston_seat *seat)
1978{
1979 struct wl_listener *listener;
1980
1981 if (!seat->keyboard)
1982 return;
1983
1984 listener = malloc(sizeof *listener);
1985 listener->notify = handle_keyboard_focus;
1986 wl_signal_add(&seat->keyboard->focus_signal, listener);
1987}
1988
1989static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001990xdg_surface_set_transient_for(struct wl_client *client,
1991 struct wl_resource *resource,
1992 struct wl_resource *parent_resource)
Scott Moreauff1db4a2012-04-17 19:06:18 -06001993{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001994 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Jasper St. Pierre8f180d42013-12-07 13:49:28 -05001995 struct weston_surface *parent;
1996
1997 if (parent_resource)
1998 parent = wl_resource_get_user_data(parent_resource);
1999 else
2000 parent = NULL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002001
2002 shell_surface_set_parent(shsurf, parent);
2003}
2004
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002005static struct shell_client *
2006get_shell_client(struct wl_client *client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002007{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002008 struct wl_listener *listener;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002009
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002010 listener = wl_client_get_destroy_listener(client,
2011 handle_shell_client_destroy);
2012 if (listener == NULL)
2013 return NULL;
Kristian Høgsberg92374e12012-08-11 22:39:12 -04002014
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002015 return container_of(listener, struct shell_client, destroy_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002016}
2017
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002018static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002019shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002020{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002021 if (sc->ping_serial != serial)
2022 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002023
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002024 sc->unresponsive = 0;
2025 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002026
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002027 if (sc->ping_timer) {
2028 wl_event_source_remove(sc->ping_timer);
2029 sc->ping_timer = NULL;
2030 }
2031
2032}
2033
2034static void
2035shell_surface_pong(struct wl_client *client,
2036 struct wl_resource *resource, uint32_t serial)
2037{
2038 struct shell_client *sc;
2039
2040 sc = get_shell_client(client);
2041 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002042}
2043
2044static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002045set_title(struct shell_surface *shsurf, const char *title)
2046{
2047 free(shsurf->title);
2048 shsurf->title = strdup(title);
2049}
2050
2051static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002052shell_surface_set_title(struct wl_client *client,
2053 struct wl_resource *resource, const char *title)
2054{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002055 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002056
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002057 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002058}
2059
2060static void
2061shell_surface_set_class(struct wl_client *client,
2062 struct wl_resource *resource, const char *class)
2063{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002064 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002065
2066 free(shsurf->class);
2067 shsurf->class = strdup(class);
2068}
2069
Alex Wu4539b082012-03-01 12:57:46 +08002070static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002071restore_output_mode(struct weston_output *output)
2072{
Hardening57388e42013-09-18 23:56:36 +02002073 if (output->current_mode != output->original_mode ||
2074 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002075 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002076 output->original_mode,
2077 output->original_scale,
2078 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002079}
2080
2081static void
2082restore_all_output_modes(struct weston_compositor *compositor)
2083{
2084 struct weston_output *output;
2085
2086 wl_list_for_each(output, &compositor->output_list, link)
2087 restore_output_mode(output);
2088}
2089
Philip Withnallbecb77e2013-11-25 18:01:30 +00002090static int
2091get_output_panel_height(struct desktop_shell *shell,
2092 struct weston_output *output)
2093{
2094 struct weston_view *view;
2095 int panel_height = 0;
2096
2097 if (!output)
2098 return 0;
2099
2100 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2101 if (view->surface->output == output) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002102 panel_height = view->surface->height;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002103 break;
2104 }
2105 }
2106
2107 return panel_height;
2108}
2109
Philip Withnall07926d92013-11-25 18:01:40 +00002110/* The surface will be inserted into the list immediately after the link
2111 * returned by this function (i.e. will be stacked immediately above the
2112 * returned link). */
2113static struct wl_list *
2114shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2115{
2116 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002117 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002118
2119 switch (shsurf->type) {
Kristian Høgsbergead52422014-01-01 13:51:52 -08002120 case SHELL_SURFACE_POPUP:
2121 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002122 if (shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002123 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002124 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002125 /* Move the surface to its parent layer so
2126 * that surfaces which are transient for
2127 * fullscreen surfaces don't get hidden by the
2128 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002129
2130 /* TODO: Handle a parent with multiple views */
2131 parent = get_default_view(shsurf->parent);
2132 if (parent)
2133 return parent->layer_link.prev;
2134 }
Rafael Antognolli03b16592013-12-03 15:35:42 -02002135 break;
Philip Withnall07926d92013-11-25 18:01:40 +00002136
2137 case SHELL_SURFACE_XWAYLAND:
Kristian Høgsberg4804a302013-12-05 22:43:03 -08002138 return &shsurf->shell->fullscreen_layer.view_list;
2139
Philip Withnall07926d92013-11-25 18:01:40 +00002140 case SHELL_SURFACE_NONE:
2141 default:
2142 /* Go to the fallback, below. */
2143 break;
2144 }
2145
2146 /* Move the surface to a normal workspace layer so that surfaces
2147 * which were previously fullscreen or transient are no longer
2148 * rendered on top. */
2149 ws = get_current_workspace(shsurf->shell);
2150 return &ws->layer.view_list;
2151}
2152
Philip Withnall648a4dd2013-11-25 18:01:44 +00002153static void
2154shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2155{
2156 struct shell_surface *child;
2157
2158 /* Move the child layers to the same workspace as shsurf. They will be
2159 * stacked above shsurf. */
2160 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2161 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2162 weston_view_geometry_dirty(child->view);
2163 wl_list_remove(&child->view->layer_link);
2164 wl_list_insert(shsurf->view->layer_link.prev,
2165 &child->view->layer_link);
2166 weston_view_geometry_dirty(child->view);
2167 weston_surface_damage(child->surface);
2168
2169 /* Recurse. We don’t expect this to recurse very far (if
2170 * at all) because that would imply we have transient
2171 * (or popup) children of transient surfaces, which
2172 * would be unusual. */
2173 shell_surface_update_child_surface_layers(child);
2174 }
2175 }
2176}
2177
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002178/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002179 * geometry to ensure the changes are redrawn.
2180 *
2181 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2182 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002183static void
2184shell_surface_update_layer(struct shell_surface *shsurf)
2185{
2186 struct wl_list *new_layer_link;
2187
2188 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2189
2190 if (new_layer_link == &shsurf->view->layer_link)
2191 return;
2192
2193 weston_view_geometry_dirty(shsurf->view);
2194 wl_list_remove(&shsurf->view->layer_link);
2195 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2196 weston_view_geometry_dirty(shsurf->view);
2197 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002198
2199 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002200}
2201
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002202static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002203shell_surface_set_parent(struct shell_surface *shsurf,
2204 struct weston_surface *parent)
2205{
2206 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002207
2208 wl_list_remove(&shsurf->children_link);
2209 wl_list_init(&shsurf->children_link);
2210
2211 /* Insert into the parent surface’s child list. */
2212 if (parent != NULL) {
2213 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2214 if (parent_shsurf != NULL)
2215 wl_list_insert(&parent_shsurf->children_list,
2216 &shsurf->children_link);
2217 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002218}
2219
2220static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002221shell_surface_set_output(struct shell_surface *shsurf,
2222 struct weston_output *output)
2223{
2224 struct weston_surface *es = shsurf->surface;
2225
2226 /* get the default output, if the client set it as NULL
2227 check whether the ouput is available */
2228 if (output)
2229 shsurf->output = output;
2230 else if (es->output)
2231 shsurf->output = es->output;
2232 else
2233 shsurf->output = get_default_output(es->compositor);
2234}
2235
2236static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002237surface_clear_next_states(struct shell_surface *shsurf)
2238{
2239 shsurf->next_state.maximized = false;
2240 shsurf->next_state.fullscreen = false;
2241
2242 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2243 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2244 shsurf->state_changed = true;
2245}
2246
2247static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002248set_toplevel(struct shell_surface *shsurf)
2249{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002250 shell_surface_set_parent(shsurf, NULL);
2251 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002252 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002253
2254 /* The layer_link is updated in set_surface_type(),
2255 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002256}
2257
2258static void
2259shell_surface_set_toplevel(struct wl_client *client,
2260 struct wl_resource *resource)
2261{
2262 struct shell_surface *surface = wl_resource_get_user_data(resource);
2263
2264 set_toplevel(surface);
2265}
2266
2267static void
2268set_transient(struct shell_surface *shsurf,
2269 struct weston_surface *parent, int x, int y, uint32_t flags)
2270{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002271 assert(parent != NULL);
2272
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002273 shell_surface_set_parent(shsurf, parent);
2274
2275 surface_clear_next_states(shsurf);
2276
Philip Withnallbecb77e2013-11-25 18:01:30 +00002277 shsurf->transient.x = x;
2278 shsurf->transient.y = y;
2279 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002280
Rafael Antognollied207b42013-12-03 15:35:43 -02002281 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002282 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002283 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002284
2285 /* The layer_link is updated in set_surface_type(),
2286 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002287}
2288
2289static void
2290shell_surface_set_transient(struct wl_client *client,
2291 struct wl_resource *resource,
2292 struct wl_resource *parent_resource,
2293 int x, int y, uint32_t flags)
2294{
2295 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2296 struct weston_surface *parent =
2297 wl_resource_get_user_data(parent_resource);
2298
2299 set_transient(shsurf, parent, x, y, flags);
2300}
2301
2302static void
2303set_fullscreen(struct shell_surface *shsurf,
2304 uint32_t method,
2305 uint32_t framerate,
2306 struct weston_output *output)
2307{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002308 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002309
2310 shsurf->fullscreen_output = shsurf->output;
2311 shsurf->fullscreen.type = method;
2312 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002313
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08002314 shsurf->next_state.fullscreen = true;
2315 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002316 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002317
2318 shsurf->client->send_configure(shsurf->surface, 0,
2319 shsurf->output->width,
2320 shsurf->output->height);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002321
2322 /* The layer_link is updated in set_surface_type(),
2323 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002324}
2325
2326static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002327weston_view_set_initial_position(struct weston_view *view,
2328 struct desktop_shell *shell);
2329
2330static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002331unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002332{
Philip Withnallf85fe842013-11-25 18:01:37 +00002333 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002334 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2335 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002336 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002337 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002338 shsurf->fullscreen_output = NULL;
2339
Alex Wu4539b082012-03-01 12:57:46 +08002340 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2341 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002342
Alex Wu4539b082012-03-01 12:57:46 +08002343 wl_list_remove(&shsurf->fullscreen.transform.link);
2344 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002345
Jason Ekstranda7af7042013-10-12 22:38:11 -05002346 if (shsurf->fullscreen.black_view)
2347 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2348 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002349
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002350 if (shsurf->saved_position_valid)
2351 weston_view_set_position(shsurf->view,
2352 shsurf->saved_x, shsurf->saved_y);
2353 else
2354 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2355
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002356 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002357 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002358 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002359 shsurf->saved_rotation_valid = false;
2360 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002361
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002362 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002363}
2364
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002365static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002366shell_surface_set_fullscreen(struct wl_client *client,
2367 struct wl_resource *resource,
2368 uint32_t method,
2369 uint32_t framerate,
2370 struct wl_resource *output_resource)
2371{
2372 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2373 struct weston_output *output;
2374
2375 if (output_resource)
2376 output = wl_resource_get_user_data(output_resource);
2377 else
2378 output = NULL;
2379
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002380 shell_surface_set_parent(shsurf, NULL);
2381
Rafael Antognolli03b16592013-12-03 15:35:42 -02002382 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002383 set_fullscreen(shsurf, method, framerate, output);
2384}
2385
2386static void
2387set_popup(struct shell_surface *shsurf,
2388 struct weston_surface *parent,
2389 struct weston_seat *seat,
2390 uint32_t serial,
2391 int32_t x,
2392 int32_t y)
2393{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002394 assert(parent != NULL);
2395
Philip Withnallbecb77e2013-11-25 18:01:30 +00002396 shsurf->popup.shseat = get_shell_seat(seat);
2397 shsurf->popup.serial = serial;
2398 shsurf->popup.x = x;
2399 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002400
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002401 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002402}
2403
2404static void
2405shell_surface_set_popup(struct wl_client *client,
2406 struct wl_resource *resource,
2407 struct wl_resource *seat_resource,
2408 uint32_t serial,
2409 struct wl_resource *parent_resource,
2410 int32_t x, int32_t y, uint32_t flags)
2411{
2412 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002413 struct weston_surface *parent =
2414 wl_resource_get_user_data(parent_resource);
2415
2416 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002417
Rafael Antognolli03b16592013-12-03 15:35:42 -02002418 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002419 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002420 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002421 wl_resource_get_user_data(seat_resource),
2422 serial, x, y);
2423}
2424
2425static void
2426set_maximized(struct shell_surface *shsurf,
2427 struct weston_output *output)
2428{
2429 struct desktop_shell *shell;
2430 uint32_t edges = 0, panel_height = 0;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002431
Philip Withnall352e7ed2013-11-25 18:01:35 +00002432 shell_surface_set_output(shsurf, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002433
2434 shell = shell_surface_get_shell(shsurf);
2435 panel_height = get_output_panel_height(shell, shsurf->output);
2436 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2437
2438 shsurf->client->send_configure(shsurf->surface, edges,
2439 shsurf->output->width,
2440 shsurf->output->height - panel_height);
2441
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08002442 shsurf->next_state.maximized = true;
2443 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002444 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002445}
2446
2447static void
2448unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002449{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002450 /* undo all maximized things here */
2451 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002452
2453 if (shsurf->saved_position_valid)
2454 weston_view_set_position(shsurf->view,
2455 shsurf->saved_x, shsurf->saved_y);
2456 else
2457 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002458
2459 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002460 wl_list_insert(&shsurf->view->geometry.transformation_list,
2461 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002462 shsurf->saved_rotation_valid = false;
2463 }
2464
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002465 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002466}
2467
Philip Withnallbecb77e2013-11-25 18:01:30 +00002468static void
2469shell_surface_set_maximized(struct wl_client *client,
2470 struct wl_resource *resource,
2471 struct wl_resource *output_resource)
2472{
2473 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2474 struct weston_output *output;
2475
2476 if (output_resource)
2477 output = wl_resource_get_user_data(output_resource);
2478 else
2479 output = NULL;
2480
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002481 shell_surface_set_parent(shsurf, NULL);
2482
Rafael Antognolli03b16592013-12-03 15:35:42 -02002483 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002484 set_maximized(shsurf, output);
2485}
2486
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002487/* This is only ever called from set_surface_type(), so there’s no need to
2488 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002489static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002490reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002491{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002492 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002493 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002494 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002495 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002496
Pekka Paalanen98262232011-12-01 10:42:22 +02002497 return 0;
2498}
2499
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002500static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002501set_full_output(struct shell_surface *shsurf)
2502{
2503 shsurf->saved_x = shsurf->view->geometry.x;
2504 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002505 shsurf->saved_width = shsurf->surface->width;
2506 shsurf->saved_height = shsurf->surface->height;
2507 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002508 shsurf->saved_position_valid = true;
2509
2510 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2511 wl_list_remove(&shsurf->rotation.transform.link);
2512 wl_list_init(&shsurf->rotation.transform.link);
2513 weston_view_geometry_dirty(shsurf->view);
2514 shsurf->saved_rotation_valid = true;
2515 }
2516}
2517
2518static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002519set_surface_type(struct shell_surface *shsurf)
2520{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002521 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002522 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002523
Philip Withnallbecb77e2013-11-25 18:01:30 +00002524 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002525
Rafael Antognolli03b16592013-12-03 15:35:42 -02002526 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002527 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002528
2529 switch (shsurf->type) {
2530 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002531 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002532 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002533 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002534 weston_view_set_position(shsurf->view,
2535 pev->geometry.x + shsurf->transient.x,
2536 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002537 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002538 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002539
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002540 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002541 weston_view_set_position(shsurf->view, shsurf->transient.x,
2542 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002543 break;
2544
Philip Withnall0f640e22013-11-25 18:01:31 +00002545 case SHELL_SURFACE_POPUP:
2546 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002547 default:
2548 break;
2549 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002550
2551 /* Update the surface’s layer. */
2552 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002553}
2554
Tiago Vignattibe143262012-04-16 17:31:41 +03002555static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002556shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002557{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002558 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002559}
2560
Alex Wu21858432012-04-01 20:13:08 +08002561static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002562black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002563
Jason Ekstranda7af7042013-10-12 22:38:11 -05002564static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002565create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002566 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002567 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002568{
2569 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002570 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002571
2572 surface = weston_surface_create(ec);
2573 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002574 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002575 return NULL;
2576 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002577 view = weston_view_create(surface);
2578 if (surface == NULL) {
2579 weston_log("no memory\n");
2580 weston_surface_destroy(surface);
2581 return NULL;
2582 }
Alex Wu4539b082012-03-01 12:57:46 +08002583
Alex Wu21858432012-04-01 20:13:08 +08002584 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002585 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002586 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002587 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002588 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002589 pixman_region32_fini(&surface->input);
2590 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002591
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002592 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002593 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002594
2595 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002596}
2597
Philip Withnalled8f1a92013-11-25 18:01:43 +00002598static void
2599shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2600{
2601 struct weston_output *output = shsurf->fullscreen_output;
2602
Rafael Antognolli03b16592013-12-03 15:35:42 -02002603 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002604
2605 if (!shsurf->fullscreen.black_view)
2606 shsurf->fullscreen.black_view =
2607 create_black_surface(shsurf->surface->compositor,
2608 shsurf->surface,
2609 output->x, output->y,
2610 output->width,
2611 output->height);
2612
2613 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2614 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2615 wl_list_insert(&shsurf->view->layer_link,
2616 &shsurf->fullscreen.black_view->layer_link);
2617 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2618 weston_surface_damage(shsurf->surface);
2619}
2620
Alex Wu4539b082012-03-01 12:57:46 +08002621/* Create black surface and append it to the associated fullscreen surface.
2622 * Handle size dismatch and positioning according to the method. */
2623static void
2624shell_configure_fullscreen(struct shell_surface *shsurf)
2625{
2626 struct weston_output *output = shsurf->fullscreen_output;
2627 struct weston_surface *surface = shsurf->surface;
2628 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002629 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002630 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002631
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002632 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2633 restore_output_mode(output);
2634
Philip Withnalled8f1a92013-11-25 18:01:43 +00002635 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002636
Jason Ekstranda7af7042013-10-12 22:38:11 -05002637 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002638 &surf_width, &surf_height);
2639
Alex Wu4539b082012-03-01 12:57:46 +08002640 switch (shsurf->fullscreen.type) {
2641 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002642 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002643 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002644 break;
2645 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002646 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002647 if (output->width == surf_width &&
2648 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002649 weston_view_set_position(shsurf->view,
2650 output->x - surf_x,
2651 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002652 break;
2653 }
2654
Alex Wu4539b082012-03-01 12:57:46 +08002655 matrix = &shsurf->fullscreen.transform.matrix;
2656 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002657
Scott Moreau1bad5db2012-08-18 01:04:05 -06002658 output_aspect = (float) output->width /
2659 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002660 /* XXX: Use surf_width and surf_height here? */
2661 surface_aspect = (float) surface->width /
2662 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002663 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002664 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002665 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002666 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002667 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002668 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002669
Alex Wu4539b082012-03-01 12:57:46 +08002670 weston_matrix_scale(matrix, scale, scale, 1);
2671 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002672 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002673 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002674 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2675 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002676 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002677
Alex Wu4539b082012-03-01 12:57:46 +08002678 break;
2679 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002680 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002681 struct weston_mode mode = {0,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002682 surf_width * surface->buffer_viewport.scale,
2683 surf_height * surface->buffer_viewport.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002684 shsurf->fullscreen.framerate};
2685
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002686 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.scale,
Hardening57388e42013-09-18 23:56:36 +02002687 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002688 weston_view_set_position(shsurf->view,
2689 output->x - surf_x,
2690 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002691 shsurf->fullscreen.black_view->surface->width = output->width;
2692 shsurf->fullscreen.black_view->surface->height = output->height;
2693 weston_view_set_position(shsurf->fullscreen.black_view,
2694 output->x - surf_x,
2695 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002696 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002697 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002698 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002699 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002700 }
Alex Wubd3354b2012-04-17 17:20:49 +08002701 }
Alex Wu4539b082012-03-01 12:57:46 +08002702 break;
2703 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002704 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002705 break;
2706 default:
2707 break;
2708 }
2709}
2710
Alex Wu4539b082012-03-01 12:57:46 +08002711static void
2712shell_map_fullscreen(struct shell_surface *shsurf)
2713{
Alex Wubd3354b2012-04-17 17:20:49 +08002714 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002715}
2716
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002717static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002718set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2719{
2720 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002721 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002722 shsurf->transient.x = x;
2723 shsurf->transient.y = y;
2724 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002725
2726 shell_surface_set_parent(shsurf, NULL);
2727
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002728 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002729 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002730}
2731
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002732static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002733
2734static void
2735destroy_shell_seat(struct wl_listener *listener, void *data)
2736{
2737 struct shell_seat *shseat =
2738 container_of(listener,
2739 struct shell_seat, seat_destroy_listener);
2740 struct shell_surface *shsurf, *prev = NULL;
2741
2742 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002743 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002744 shseat->popup_grab.client = NULL;
2745
2746 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2747 shsurf->popup.shseat = NULL;
2748 if (prev) {
2749 wl_list_init(&prev->popup.grab_link);
2750 }
2751 prev = shsurf;
2752 }
2753 wl_list_init(&prev->popup.grab_link);
2754 }
2755
2756 wl_list_remove(&shseat->seat_destroy_listener.link);
2757 free(shseat);
2758}
2759
2760static struct shell_seat *
2761create_shell_seat(struct weston_seat *seat)
2762{
2763 struct shell_seat *shseat;
2764
2765 shseat = calloc(1, sizeof *shseat);
2766 if (!shseat) {
2767 weston_log("no memory to allocate shell seat\n");
2768 return NULL;
2769 }
2770
2771 shseat->seat = seat;
2772 wl_list_init(&shseat->popup_grab.surfaces_list);
2773
2774 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2775 wl_signal_add(&seat->destroy_signal,
2776 &shseat->seat_destroy_listener);
2777
2778 return shseat;
2779}
2780
2781static struct shell_seat *
2782get_shell_seat(struct weston_seat *seat)
2783{
2784 struct wl_listener *listener;
2785
2786 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2787 if (listener == NULL)
2788 return create_shell_seat(seat);
2789
2790 return container_of(listener,
2791 struct shell_seat, seat_destroy_listener);
2792}
2793
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002794static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002795popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002796{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002797 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002798 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002799 struct shell_seat *shseat =
2800 container_of(grab, struct shell_seat, popup_grab.grab);
2801 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002802 wl_fixed_t sx, sy;
2803
Jason Ekstranda7af7042013-10-12 22:38:11 -05002804 view = weston_compositor_pick_view(pointer->seat->compositor,
2805 pointer->x, pointer->y,
2806 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002807
Jason Ekstranda7af7042013-10-12 22:38:11 -05002808 if (view && view->surface->resource &&
2809 wl_resource_get_client(view->surface->resource) == client) {
2810 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002811 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002812 weston_pointer_set_focus(pointer, NULL,
2813 wl_fixed_from_int(0),
2814 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002815 }
2816}
2817
2818static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002819popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2820 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002821{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002822 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002823 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002824 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002825
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002826 weston_pointer_move(pointer, x, y);
2827
Neil Roberts96d790e2013-09-19 17:32:00 +01002828 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002829 weston_view_from_global_fixed(pointer->focus,
2830 pointer->x, pointer->y,
2831 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002832 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002833 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002834}
2835
2836static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002837popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002838 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002839{
2840 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002841 struct shell_seat *shseat =
2842 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002843 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002844 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002845 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002846 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002847
Neil Roberts96d790e2013-09-19 17:32:00 +01002848 resource_list = &grab->pointer->focus_resource_list;
2849 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002850 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01002851 wl_resource_for_each(resource, resource_list) {
2852 wl_pointer_send_button(resource, serial,
2853 time, button, state);
2854 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002855 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01002856 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04002857 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002858 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002859 }
2860
Daniel Stone4dbadb12012-05-30 16:31:51 +01002861 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002862 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002863}
2864
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002865static void
2866popup_grab_cancel(struct weston_pointer_grab *grab)
2867{
2868 popup_grab_end(grab->pointer);
2869}
2870
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002871static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002872 popup_grab_focus,
2873 popup_grab_motion,
2874 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02002875 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002876};
2877
2878static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02002879shell_surface_send_popup_done(struct shell_surface *shsurf)
2880{
2881 if (shell_surface_is_wl_shell_surface(shsurf))
2882 wl_shell_surface_send_popup_done(shsurf->resource);
2883 else if (shell_surface_is_xdg_popup(shsurf))
2884 xdg_popup_send_popup_done(shsurf->resource,
2885 shsurf->popup.serial);
2886}
2887
2888static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002889popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002890{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002891 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002892 struct shell_seat *shseat =
2893 container_of(grab, struct shell_seat, popup_grab.grab);
2894 struct shell_surface *shsurf;
2895 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002896
2897 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002898 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002899 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002900 shseat->popup_grab.grab.interface = NULL;
2901 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002902 /* Send the popup_done event to all the popups open */
2903 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002904 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002905 shsurf->popup.shseat = NULL;
2906 if (prev) {
2907 wl_list_init(&prev->popup.grab_link);
2908 }
2909 prev = shsurf;
2910 }
2911 wl_list_init(&prev->popup.grab_link);
2912 wl_list_init(&shseat->popup_grab.surfaces_list);
2913 }
2914}
2915
2916static void
2917add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2918{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002919 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002920
2921 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002922 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002923 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002924 /* We must make sure here that this popup was opened after
2925 * a mouse press, and not just by moving around with other
2926 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002927 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002928 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002929
Rob Bradforddfe31052013-06-26 19:49:11 +01002930 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002931 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01002932 } else {
2933 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002934 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01002935}
2936
2937static void
2938remove_popup_grab(struct shell_surface *shsurf)
2939{
2940 struct shell_seat *shseat = shsurf->popup.shseat;
2941
2942 wl_list_remove(&shsurf->popup.grab_link);
2943 wl_list_init(&shsurf->popup.grab_link);
2944 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002945 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002946 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002947 }
2948}
2949
2950static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002951shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002952{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002953 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002954 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002955
Jason Ekstranda7af7042013-10-12 22:38:11 -05002956 shsurf->surface->output = parent_view->output;
2957 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002958
Jason Ekstranda7af7042013-10-12 22:38:11 -05002959 weston_view_set_transform_parent(shsurf->view, parent_view);
2960 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2961 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002962
Kristian Høgsberge3148752013-05-06 23:19:49 -04002963 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002964 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002965 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02002966 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002967 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002968 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002969}
2970
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002971static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002972 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002973 shell_surface_move,
2974 shell_surface_resize,
2975 shell_surface_set_toplevel,
2976 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002977 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002978 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002979 shell_surface_set_maximized,
2980 shell_surface_set_title,
2981 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002982};
2983
2984static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002985destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002986{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08002987 struct shell_surface *child, *next;
2988
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002989 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2990
Giulio Camuffo5085a752013-03-25 21:42:45 +01002991 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2992 remove_popup_grab(shsurf);
2993 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002994
Alex Wubd3354b2012-04-17 17:20:49 +08002995 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002996 shell_surface_is_top_fullscreen(shsurf))
2997 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002998
Jason Ekstranda7af7042013-10-12 22:38:11 -05002999 if (shsurf->fullscreen.black_view)
3000 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003001
Alex Wubd3354b2012-04-17 17:20:49 +08003002 /* As destroy_resource() use wl_list_for_each_safe(),
3003 * we can always remove the listener.
3004 */
3005 wl_list_remove(&shsurf->surface_destroy_listener.link);
3006 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003007 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003008
Jason Ekstranda7af7042013-10-12 22:38:11 -05003009 weston_view_destroy(shsurf->view);
3010
Philip Withnall648a4dd2013-11-25 18:01:44 +00003011 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003012 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3013 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003014
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003015 wl_list_remove(&shsurf->link);
3016 free(shsurf);
3017}
3018
3019static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003020shell_destroy_shell_surface(struct wl_resource *resource)
3021{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003022 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003023
3024 destroy_shell_surface(shsurf);
3025}
3026
3027static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003028shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003029{
3030 struct shell_surface *shsurf = container_of(listener,
3031 struct shell_surface,
3032 surface_destroy_listener);
3033
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003034 if (shsurf->resource)
3035 wl_resource_destroy(shsurf->resource);
3036 else
Tiago Vignattibc052c92012-04-19 16:18:18 +03003037 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003038}
3039
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003040static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003041shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003042
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003043struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003044get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003045{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003046 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003047 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003048 else
3049 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003050}
3051
Rafael Antognollie2a34552013-12-03 15:35:45 -02003052static struct shell_surface *
3053create_common_surface(void *shell, struct weston_surface *surface,
3054 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003055{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003056 struct shell_surface *shsurf;
3057
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003058 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003059 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003060 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003061 }
3062
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003063 shsurf = calloc(1, sizeof *shsurf);
3064 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003065 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003066 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003067 }
3068
Jason Ekstranda7af7042013-10-12 22:38:11 -05003069 shsurf->view = weston_view_create(surface);
3070 if (!shsurf->view) {
3071 weston_log("no memory to allocate shell surface\n");
3072 free(shsurf);
3073 return NULL;
3074 }
3075
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003076 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003077 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003078
Tiago Vignattibc052c92012-04-19 16:18:18 +03003079 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003080 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003081 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003082 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003083 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003084 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003085 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3086 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003087 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003088 wl_list_init(&shsurf->fullscreen.transform.link);
3089
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003090 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003091 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003092 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003093 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003094
3095 /* init link so its safe to always remove it in destroy_shell_surface */
3096 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003097 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003098
Pekka Paalanen460099f2012-01-20 16:48:25 +02003099 /* empty when not in use */
3100 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003101 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003102
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003103 wl_list_init(&shsurf->workspace_transform.link);
3104
Philip Withnall648a4dd2013-11-25 18:01:44 +00003105 wl_list_init(&shsurf->children_link);
3106 wl_list_init(&shsurf->children_list);
3107 shsurf->parent = NULL;
3108
Pekka Paalanen98262232011-12-01 10:42:22 +02003109 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003110
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003111 shsurf->client = client;
3112
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003113 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003114}
3115
Rafael Antognollie2a34552013-12-03 15:35:45 -02003116static struct shell_surface *
3117create_shell_surface(void *shell, struct weston_surface *surface,
3118 const struct weston_shell_client *client)
3119{
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003120 return create_common_surface(shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003121}
3122
Jason Ekstranda7af7042013-10-12 22:38:11 -05003123static struct weston_view *
3124get_primary_view(void *shell, struct shell_surface *shsurf)
3125{
3126 return shsurf->view;
3127}
3128
Tiago Vignattibc052c92012-04-19 16:18:18 +03003129static void
3130shell_get_shell_surface(struct wl_client *client,
3131 struct wl_resource *resource,
3132 uint32_t id,
3133 struct wl_resource *surface_resource)
3134{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003135 struct weston_surface *surface =
3136 wl_resource_get_user_data(surface_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003137 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003138 struct shell_surface *shsurf;
3139
3140 if (get_shell_surface(surface)) {
3141 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003142 WL_DISPLAY_ERROR_INVALID_OBJECT,
3143 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003144 return;
3145 }
3146
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003147 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003148 if (!shsurf) {
3149 wl_resource_post_error(surface_resource,
3150 WL_DISPLAY_ERROR_INVALID_OBJECT,
3151 "surface->configure already set");
3152 return;
3153 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003154
Jason Ekstranda85118c2013-06-27 20:17:02 -05003155 shsurf->resource =
3156 wl_resource_create(client,
3157 &wl_shell_surface_interface, 1, id);
3158 wl_resource_set_implementation(shsurf->resource,
3159 &shell_surface_implementation,
3160 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003161}
3162
Rafael Antognollie2a34552013-12-03 15:35:45 -02003163static bool
3164shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3165{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003166 /* A shell surface without a resource is created from xwayland
3167 * and is considered a wl_shell surface for now. */
3168
3169 return shsurf->resource == NULL ||
3170 wl_resource_instance_of(shsurf->resource,
3171 &wl_shell_surface_interface,
3172 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003173}
3174
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003175static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003176 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003177};
3178
Rafael Antognollie2a34552013-12-03 15:35:45 -02003179/****************************
3180 * xdg-shell implementation */
3181
3182static void
3183xdg_surface_destroy(struct wl_client *client,
3184 struct wl_resource *resource)
3185{
3186 wl_resource_destroy(resource);
3187}
3188
3189static void
Jasper St. Pierre74073452014-02-01 18:36:41 -05003190xdg_surface_set_margin(struct wl_client *client,
3191 struct wl_resource *resource,
3192 int32_t left,
3193 int32_t right,
3194 int32_t top,
3195 int32_t bottom)
3196{
3197 /* Do nothing, Weston doesn't try to constrain or place
3198 * surfaces in any special manner... */
3199}
3200
3201static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003202xdg_surface_set_app_id(struct wl_client *client,
3203 struct wl_resource *resource,
3204 const char *app_id)
3205{
3206 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3207
3208 free(shsurf->class);
3209 shsurf->class = strdup(app_id);
3210}
3211
3212static void
3213xdg_surface_set_title(struct wl_client *client,
3214 struct wl_resource *resource, const char *title)
3215{
3216 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3217
3218 set_title(shsurf, title);
3219}
3220
3221static void
3222xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3223 struct wl_resource *seat_resource, uint32_t serial)
3224{
3225 common_surface_move(resource, seat_resource, serial);
3226}
3227
3228static void
3229xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3230 struct wl_resource *seat_resource, uint32_t serial,
3231 uint32_t edges)
3232{
3233 common_surface_resize(resource, seat_resource, serial, edges);
3234}
3235
3236static void
3237xdg_surface_set_output(struct wl_client *client,
3238 struct wl_resource *resource,
3239 struct wl_resource *output_resource)
3240{
3241 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3242 struct weston_output *output;
3243
3244 if (output_resource)
3245 output = wl_resource_get_user_data(output_resource);
3246 else
3247 output = NULL;
3248
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003249 shsurf->recommended_output = output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003250}
3251
3252static void
3253xdg_surface_set_fullscreen(struct wl_client *client,
3254 struct wl_resource *resource)
3255{
3256 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3257
3258 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3259 return;
3260
Kristian Høgsberge960b3f2013-12-05 22:04:42 -08003261 if (!shsurf->next_state.fullscreen)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003262 set_fullscreen(shsurf,
3263 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003264 0, shsurf->recommended_output);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003265}
3266
3267static void
3268xdg_surface_unset_fullscreen(struct wl_client *client,
3269 struct wl_resource *resource)
3270{
3271 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003272 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003273
3274 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3275 return;
3276
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003277 if (!shsurf->next_state.fullscreen)
3278 return;
3279
Rafael Antognollie2a34552013-12-03 15:35:45 -02003280 shsurf->next_state.fullscreen = false;
3281 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003282
3283 if (shsurf->saved_size_valid) {
3284 width = shsurf->saved_width;
3285 height = shsurf->saved_height;
3286 shsurf->saved_size_valid = false;
3287 } else {
3288 width = shsurf->surface->width;
3289 height = shsurf->surface->height;
3290 }
3291
3292 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003293}
3294
3295static void
3296xdg_surface_set_maximized(struct wl_client *client,
3297 struct wl_resource *resource)
3298{
3299 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3300
3301 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3302 return;
3303
Kristian Høgsbergc2745b12013-12-05 22:00:40 -08003304 if (!shsurf->next_state.maximized)
Rafael Antognolli65f98d82013-12-03 15:35:47 -02003305 set_maximized(shsurf, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003306}
3307
3308static void
3309xdg_surface_unset_maximized(struct wl_client *client,
3310 struct wl_resource *resource)
3311{
3312 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003313 int32_t width, height;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003314
3315 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3316 return;
3317
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003318 if (!shsurf->next_state.maximized)
3319 return;
3320
Rafael Antognollie2a34552013-12-03 15:35:45 -02003321 shsurf->next_state.maximized = false;
3322 shsurf->state_changed = true;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003323
3324 if (shsurf->saved_size_valid) {
3325 width = shsurf->saved_width;
3326 height = shsurf->saved_height;
3327 shsurf->saved_size_valid = false;
3328 } else {
3329 width = shsurf->surface->width;
3330 height = shsurf->surface->height;
3331 }
3332
3333 shsurf->client->send_configure(shsurf->surface, 0, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003334}
3335
3336static const struct xdg_surface_interface xdg_surface_implementation = {
3337 xdg_surface_destroy,
3338 xdg_surface_set_transient_for,
Jasper St. Pierre74073452014-02-01 18:36:41 -05003339 xdg_surface_set_margin,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003340 xdg_surface_set_title,
3341 xdg_surface_set_app_id,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003342 xdg_surface_move,
3343 xdg_surface_resize,
3344 xdg_surface_set_output,
3345 xdg_surface_set_fullscreen,
3346 xdg_surface_unset_fullscreen,
3347 xdg_surface_set_maximized,
3348 xdg_surface_unset_maximized,
3349 NULL /* set_minimized */
3350};
3351
3352static void
3353xdg_send_configure(struct weston_surface *surface,
3354 uint32_t edges, int32_t width, int32_t height)
3355{
3356 struct shell_surface *shsurf = get_shell_surface(surface);
3357
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003358 assert(shsurf);
3359
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08003360 xdg_surface_send_configure(shsurf->resource, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003361}
3362
3363static const struct weston_shell_client xdg_client = {
3364 xdg_send_configure
3365};
3366
3367static void
3368xdg_use_unstable_version(struct wl_client *client,
3369 struct wl_resource *resource,
3370 int32_t version)
3371{
3372 if (version > 1) {
3373 wl_resource_post_error(resource,
3374 1,
3375 "xdg-shell:: version not implemented yet.");
3376 return;
3377 }
3378}
3379
3380static struct shell_surface *
3381create_xdg_surface(void *shell, struct weston_surface *surface,
3382 const struct weston_shell_client *client)
3383{
3384 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003385
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003386 shsurf = create_common_surface(shell, surface, client);
3387 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003388
3389 return shsurf;
3390}
3391
3392static void
3393xdg_get_xdg_surface(struct wl_client *client,
3394 struct wl_resource *resource,
3395 uint32_t id,
3396 struct wl_resource *surface_resource)
3397{
3398 struct weston_surface *surface =
3399 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003400 struct shell_client *sc = wl_resource_get_user_data(resource);
3401 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003402 struct shell_surface *shsurf;
3403
3404 if (get_shell_surface(surface)) {
3405 wl_resource_post_error(surface_resource,
3406 WL_DISPLAY_ERROR_INVALID_OBJECT,
3407 "desktop_shell::get_shell_surface already requested");
3408 return;
3409 }
3410
3411 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3412 if (!shsurf) {
3413 wl_resource_post_error(surface_resource,
3414 WL_DISPLAY_ERROR_INVALID_OBJECT,
3415 "surface->configure already set");
3416 return;
3417 }
3418
3419 shsurf->resource =
3420 wl_resource_create(client,
3421 &xdg_surface_interface, 1, id);
3422 wl_resource_set_implementation(shsurf->resource,
3423 &xdg_surface_implementation,
3424 shsurf, shell_destroy_shell_surface);
3425}
3426
3427static bool
3428shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3429{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003430 return shsurf->resource &&
3431 wl_resource_instance_of(shsurf->resource,
3432 &xdg_surface_interface,
3433 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003434}
3435
3436/* xdg-popup implementation */
3437
3438static void
3439xdg_popup_destroy(struct wl_client *client,
3440 struct wl_resource *resource)
3441{
3442 wl_resource_destroy(resource);
3443}
3444
Rafael Antognollie2a34552013-12-03 15:35:45 -02003445static const struct xdg_popup_interface xdg_popup_implementation = {
3446 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003447};
3448
3449static void
3450xdg_popup_send_configure(struct weston_surface *surface,
3451 uint32_t edges, int32_t width, int32_t height)
3452{
3453}
3454
3455static const struct weston_shell_client xdg_popup_client = {
3456 xdg_popup_send_configure
3457};
3458
3459static struct shell_surface *
3460create_xdg_popup(void *shell, struct weston_surface *surface,
3461 const struct weston_shell_client *client,
3462 struct weston_surface *parent,
3463 struct shell_seat *seat,
3464 uint32_t serial,
3465 int32_t x, int32_t y)
3466{
3467 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003468
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003469 shsurf = create_common_surface(shell, surface, client);
3470 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003471 shsurf->popup.shseat = seat;
3472 shsurf->popup.serial = serial;
3473 shsurf->popup.x = x;
3474 shsurf->popup.y = y;
3475 shell_surface_set_parent(shsurf, parent);
3476
3477 return shsurf;
3478}
3479
3480static void
3481xdg_get_xdg_popup(struct wl_client *client,
3482 struct wl_resource *resource,
3483 uint32_t id,
3484 struct wl_resource *surface_resource,
3485 struct wl_resource *parent_resource,
3486 struct wl_resource *seat_resource,
3487 uint32_t serial,
3488 int32_t x, int32_t y, uint32_t flags)
3489{
3490 struct weston_surface *surface =
3491 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003492 struct shell_client *sc = wl_resource_get_user_data(resource);
3493 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003494 struct shell_surface *shsurf;
3495 struct weston_surface *parent;
3496 struct shell_seat *seat;
3497
3498 if (get_shell_surface(surface)) {
3499 wl_resource_post_error(surface_resource,
3500 WL_DISPLAY_ERROR_INVALID_OBJECT,
3501 "desktop_shell::get_shell_surface already requested");
3502 return;
3503 }
3504
3505 if (!parent_resource) {
3506 wl_resource_post_error(surface_resource,
3507 WL_DISPLAY_ERROR_INVALID_OBJECT,
3508 "xdg_shell::get_xdg_popup requires a parent shell surface");
3509 }
3510
3511 parent = wl_resource_get_user_data(parent_resource);
3512 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3513
3514 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3515 parent, seat, serial, x, y);
3516 if (!shsurf) {
3517 wl_resource_post_error(surface_resource,
3518 WL_DISPLAY_ERROR_INVALID_OBJECT,
3519 "surface->configure already set");
3520 return;
3521 }
3522
3523 shsurf->resource =
3524 wl_resource_create(client,
3525 &xdg_popup_interface, 1, id);
3526 wl_resource_set_implementation(shsurf->resource,
3527 &xdg_popup_implementation,
3528 shsurf, shell_destroy_shell_surface);
3529}
3530
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003531static void
3532xdg_pong(struct wl_client *client,
3533 struct wl_resource *resource, uint32_t serial)
3534{
3535 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003536
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003537 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003538}
3539
Rafael Antognollie2a34552013-12-03 15:35:45 -02003540static bool
3541shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3542{
3543 return wl_resource_instance_of(shsurf->resource,
3544 &xdg_popup_interface,
3545 &xdg_popup_implementation);
3546}
3547
3548static const struct xdg_shell_interface xdg_implementation = {
3549 xdg_use_unstable_version,
3550 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003551 xdg_get_xdg_popup,
3552 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003553};
3554
3555static int
3556xdg_shell_unversioned_dispatch(const void *implementation,
3557 void *_target, uint32_t opcode,
3558 const struct wl_message *message,
3559 union wl_argument *args)
3560{
3561 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003562 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003563
3564 if (opcode != 0) {
3565 wl_resource_post_error(resource,
3566 WL_DISPLAY_ERROR_INVALID_OBJECT,
3567 "must call use_unstable_version first");
3568 return 0;
3569 }
3570
Kristian Høgsberg239902b2014-02-11 13:50:08 -08003571#define XDG_SERVER_VERSION 2
Rafael Antognollie2a34552013-12-03 15:35:45 -02003572
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003573 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3574 "shell implementation doesn't match protocol version");
3575
Rafael Antognollie2a34552013-12-03 15:35:45 -02003576 if (args[0].i != XDG_SERVER_VERSION) {
3577 wl_resource_post_error(resource,
3578 WL_DISPLAY_ERROR_INVALID_OBJECT,
3579 "incompatible version, server is %d "
3580 "client wants %d",
3581 XDG_SERVER_VERSION, args[0].i);
3582 return 0;
3583 }
3584
3585 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003586 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003587
3588 return 1;
3589}
3590
3591/* end of xdg-shell implementation */
3592/***********************************/
3593
Kristian Høgsberg07937562011-04-12 17:25:42 -04003594static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003595shell_fade(struct desktop_shell *shell, enum fade_type type);
3596
3597static int
3598screensaver_timeout(void *data)
3599{
3600 struct desktop_shell *shell = data;
3601
3602 shell_fade(shell, FADE_OUT);
3603
3604 return 1;
3605}
3606
3607static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003608handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003609{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003610 struct desktop_shell *shell =
3611 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003612
Pekka Paalanen18027e52011-12-02 16:31:49 +02003613 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003614
3615 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003616 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003617}
3618
3619static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003620launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003621{
3622 if (shell->screensaver.binding)
3623 return;
3624
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003625 if (!shell->screensaver.path) {
3626 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003627 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003628 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003629
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003630 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003631 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003632 return;
3633 }
3634
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003635 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003636 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003637 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003638 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003639}
3640
3641static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003642terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003643{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003644 if (shell->screensaver.process.pid == 0)
3645 return;
3646
3647 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003648}
3649
3650static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003651configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003652{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003653 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003654
Jason Ekstranda7af7042013-10-12 22:38:11 -05003655 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3656 if (v->output == ev->output && v != ev) {
3657 weston_view_unmap(v);
3658 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003659 }
3660 }
3661
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003662 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003663
Jason Ekstranda7af7042013-10-12 22:38:11 -05003664 if (wl_list_empty(&ev->layer_link)) {
3665 wl_list_insert(&layer->view_list, &ev->layer_link);
3666 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003667 }
3668}
3669
3670static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003671background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003672{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003673 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003674 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003675
Jason Ekstranda7af7042013-10-12 22:38:11 -05003676 view = container_of(es->views.next, struct weston_view, surface_link);
3677
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003678 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003679}
3680
3681static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003682desktop_shell_set_background(struct wl_client *client,
3683 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003684 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003685 struct wl_resource *surface_resource)
3686{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003687 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003688 struct weston_surface *surface =
3689 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003690 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003691
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003692 if (surface->configure) {
3693 wl_resource_post_error(surface_resource,
3694 WL_DISPLAY_ERROR_INVALID_OBJECT,
3695 "surface role already assigned");
3696 return;
3697 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003698
Jason Ekstranda7af7042013-10-12 22:38:11 -05003699 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3700 weston_view_destroy(view);
3701 view = weston_view_create(surface);
3702
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003703 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003704 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003705 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003706 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003707 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003708 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003709 surface->output->width,
3710 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003711}
3712
3713static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003714panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003715{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003716 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003717 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003718
Jason Ekstranda7af7042013-10-12 22:38:11 -05003719 view = container_of(es->views.next, struct weston_view, surface_link);
3720
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003721 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003722}
3723
3724static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003725desktop_shell_set_panel(struct wl_client *client,
3726 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003727 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003728 struct wl_resource *surface_resource)
3729{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003730 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003731 struct weston_surface *surface =
3732 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003733 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003734
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003735 if (surface->configure) {
3736 wl_resource_post_error(surface_resource,
3737 WL_DISPLAY_ERROR_INVALID_OBJECT,
3738 "surface role already assigned");
3739 return;
3740 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003741
Jason Ekstranda7af7042013-10-12 22:38:11 -05003742 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3743 weston_view_destroy(view);
3744 view = weston_view_create(surface);
3745
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003746 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003747 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003748 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003749 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003750 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003751 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003752 surface->output->width,
3753 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003754}
3755
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003756static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003757lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003758{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003759 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003760 struct weston_view *view;
3761
3762 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003763
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003764 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003765 return;
3766
Jason Ekstranda7af7042013-10-12 22:38:11 -05003767 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003768
3769 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003770 wl_list_insert(&shell->lock_layer.view_list,
3771 &view->layer_link);
3772 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003773 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003774 }
3775}
3776
3777static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003778handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003779{
Tiago Vignattibe143262012-04-16 17:31:41 +03003780 struct desktop_shell *shell =
3781 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003782
Martin Minarik6d118362012-06-07 18:01:59 +02003783 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003784 shell->lock_surface = NULL;
3785}
3786
3787static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003788desktop_shell_set_lock_surface(struct wl_client *client,
3789 struct wl_resource *resource,
3790 struct wl_resource *surface_resource)
3791{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003792 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003793 struct weston_surface *surface =
3794 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003795
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003796 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003797
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003798 if (!shell->locked)
3799 return;
3800
Pekka Paalanen98262232011-12-01 10:42:22 +02003801 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003802
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003803 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003804 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003805 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003806
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003807 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003808 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003809 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003810}
3811
3812static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003813resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003814{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003815 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003816
Pekka Paalanen77346a62011-11-30 16:26:35 +02003817 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003818
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003819 wl_list_remove(&shell->lock_layer.link);
3820 wl_list_insert(&shell->compositor->cursor_layer.link,
3821 &shell->fullscreen_layer.link);
3822 wl_list_insert(&shell->fullscreen_layer.link,
3823 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003824 if (shell->showing_input_panels) {
3825 wl_list_insert(&shell->panel_layer.link,
3826 &shell->input_panel_layer.link);
3827 wl_list_insert(&shell->input_panel_layer.link,
3828 &ws->layer.link);
3829 } else {
3830 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
3831 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003832
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003833 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003834
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003835 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003836 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003837 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003838}
3839
3840static void
3841desktop_shell_unlock(struct wl_client *client,
3842 struct wl_resource *resource)
3843{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003844 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003845
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003846 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003847
3848 if (shell->locked)
3849 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003850}
3851
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003852static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003853desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003854 struct wl_resource *resource,
3855 struct wl_resource *surface_resource)
3856{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003857 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003858
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003859 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003860 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003861}
3862
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003863static void
3864desktop_shell_desktop_ready(struct wl_client *client,
3865 struct wl_resource *resource)
3866{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003867 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003868
3869 shell_fade_startup(shell);
3870}
3871
Kristian Høgsberg75840622011-09-06 13:48:16 -04003872static const struct desktop_shell_interface desktop_shell_implementation = {
3873 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003874 desktop_shell_set_panel,
3875 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003876 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003877 desktop_shell_set_grab_surface,
3878 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04003879};
3880
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003881static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003882get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003883{
3884 struct shell_surface *shsurf;
3885
3886 shsurf = get_shell_surface(surface);
3887 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02003888 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003889 return shsurf->type;
3890}
3891
Kristian Høgsberg75840622011-09-06 13:48:16 -04003892static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003893move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003894{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003895 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003896 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003897 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003898
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003899 if (seat->pointer->focus == NULL)
3900 return;
3901
3902 focus = seat->pointer->focus->surface;
3903
Pekka Paalanen01388e22013-04-25 13:57:44 +03003904 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003905 if (surface == NULL)
3906 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003907
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003908 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003909 if (shsurf == NULL || shsurf->state.fullscreen ||
3910 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003911 return;
3912
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003913 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003914}
3915
3916static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003917maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3918{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01003919 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003920 struct weston_surface *surface;
3921 struct shell_surface *shsurf;
3922
3923 surface = weston_surface_get_main_surface(focus);
3924 if (surface == NULL)
3925 return;
3926
3927 shsurf = get_shell_surface(surface);
3928 if (shsurf == NULL)
3929 return;
3930
3931 if (!shell_surface_is_xdg_surface(shsurf))
3932 return;
3933
3934 if (shsurf->state.maximized)
3935 xdg_surface_send_request_unset_maximized(shsurf->resource);
3936 else
3937 xdg_surface_send_request_set_maximized(shsurf->resource);
3938}
3939
3940static void
3941fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3942{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01003943 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003944 struct weston_surface *surface;
3945 struct shell_surface *shsurf;
3946
3947 surface = weston_surface_get_main_surface(focus);
3948 if (surface == NULL)
3949 return;
3950
3951 shsurf = get_shell_surface(surface);
3952 if (shsurf == NULL)
3953 return;
3954
3955 if (!shell_surface_is_xdg_surface(shsurf))
3956 return;
3957
3958 if (shsurf->state.fullscreen)
3959 xdg_surface_send_request_unset_fullscreen(shsurf->resource);
3960 else
3961 xdg_surface_send_request_set_fullscreen(shsurf->resource);
3962}
3963
3964static void
Neil Robertsaba0f252013-10-03 16:43:05 +01003965touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
3966{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07003967 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003968 struct weston_surface *surface;
3969 struct shell_surface *shsurf;
3970
3971 surface = weston_surface_get_main_surface(focus);
3972 if (surface == NULL)
3973 return;
3974
3975 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02003976 if (shsurf == NULL || shsurf->state.fullscreen ||
3977 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01003978 return;
3979
3980 surface_touch_move(shsurf, (struct weston_seat *) seat);
3981}
3982
3983static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003984resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003985{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003986 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003987 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003988 uint32_t edges = 0;
3989 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003990 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003991
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003992 if (seat->pointer->focus == NULL)
3993 return;
3994
3995 focus = seat->pointer->focus->surface;
3996
Pekka Paalanen01388e22013-04-25 13:57:44 +03003997 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003998 if (surface == NULL)
3999 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004000
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004001 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004002 if (shsurf == NULL || shsurf->state.fullscreen ||
4003 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004004 return;
4005
Jason Ekstranda7af7042013-10-12 22:38:11 -05004006 weston_view_from_global(shsurf->view,
4007 wl_fixed_to_int(seat->pointer->grab_x),
4008 wl_fixed_to_int(seat->pointer->grab_y),
4009 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004010
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004011 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004012 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004013 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004014 edges |= 0;
4015 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004016 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004017
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004018 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004019 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004020 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004021 edges |= 0;
4022 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004023 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004024
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004025 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004026}
4027
4028static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004029surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004030 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004031{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004032 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004033 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004034 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004035 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004036
Pekka Paalanen01388e22013-04-25 13:57:44 +03004037 /* XXX: broken for windows containing sub-surfaces */
4038 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004039 if (surface == NULL)
4040 return;
4041
4042 shsurf = get_shell_surface(surface);
4043 if (!shsurf)
4044 return;
4045
Jason Ekstranda7af7042013-10-12 22:38:11 -05004046 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004047
Jason Ekstranda7af7042013-10-12 22:38:11 -05004048 if (shsurf->view->alpha > 1.0)
4049 shsurf->view->alpha = 1.0;
4050 if (shsurf->view->alpha < step)
4051 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004052
Jason Ekstranda7af7042013-10-12 22:38:11 -05004053 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004054 weston_surface_damage(surface);
4055}
4056
4057static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004058do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004059 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004060{
Daniel Stone37816df2012-05-16 18:45:18 +01004061 struct weston_seat *ws = (struct weston_seat *) seat;
4062 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004063 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004064 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004065
4066 wl_list_for_each(output, &compositor->output_list, link) {
4067 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004068 wl_fixed_to_double(seat->pointer->x),
4069 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004070 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004071 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004072 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004073 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004074 increment = -output->zoom.increment;
4075 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004076 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004077 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004078 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004079 else
4080 increment = 0;
4081
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004082 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004083
Scott Moreaue6603982012-06-11 13:07:51 -06004084 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004085 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004086 else if (output->zoom.level > output->zoom.max_level)
4087 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004088 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004089 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004090 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004091
Scott Moreaue6603982012-06-11 13:07:51 -06004092 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004093
Jason Ekstranda7af7042013-10-12 22:38:11 -05004094 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004095 }
4096 }
4097}
4098
Scott Moreauccbf29d2012-02-22 14:21:41 -07004099static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004100zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004101 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004102{
4103 do_zoom(seat, time, 0, axis, value);
4104}
4105
4106static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004107zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004108 void *data)
4109{
4110 do_zoom(seat, time, key, 0, 0);
4111}
4112
4113static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004114terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004115 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004116{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004117 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004118
Daniel Stone325fc2d2012-05-30 16:31:58 +01004119 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004120}
4121
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004122static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004123rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4124 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004125{
4126 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004127 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004128 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004129 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004130 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004131
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004132 weston_pointer_move(pointer, x, y);
4133
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004134 if (!shsurf)
4135 return;
4136
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004137 cx = 0.5f * shsurf->surface->width;
4138 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004139
Daniel Stone37816df2012-05-16 18:45:18 +01004140 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4141 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004142 r = sqrtf(dx * dx + dy * dy);
4143
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004144 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004145 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004146
4147 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004148 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004149 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004150
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004151 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004152 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004153
4154 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004155 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004156 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004157 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004158 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004159
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004160 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004161 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004162 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004163 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004164 wl_list_init(&shsurf->rotation.transform.link);
4165 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004166 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004167 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004168
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004169 /* We need to adjust the position of the surface
4170 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004171 cposx = shsurf->view->geometry.x + cx;
4172 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004173 dposx = rotate->center.x - cposx;
4174 dposy = rotate->center.y - cposy;
4175 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004176 weston_view_set_position(shsurf->view,
4177 shsurf->view->geometry.x + dposx,
4178 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004179 }
4180
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004181 /* Repaint implies weston_surface_update_transform(), which
4182 * lazily applies the damage due to rotation update.
4183 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004184 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004185}
4186
4187static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004188rotate_grab_button(struct weston_pointer_grab *grab,
4189 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004190{
4191 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004192 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004193 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004194 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004195 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004196
Daniel Stone4dbadb12012-05-30 16:31:51 +01004197 if (pointer->button_count == 0 &&
4198 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004199 if (shsurf)
4200 weston_matrix_multiply(&shsurf->rotation.rotation,
4201 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004202 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004203 free(rotate);
4204 }
4205}
4206
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004207static void
4208rotate_grab_cancel(struct weston_pointer_grab *grab)
4209{
4210 struct rotate_grab *rotate =
4211 container_of(grab, struct rotate_grab, base.grab);
4212
4213 shell_grab_end(&rotate->base);
4214 free(rotate);
4215}
4216
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004217static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004218 noop_grab_focus,
4219 rotate_grab_motion,
4220 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004221 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004222};
4223
4224static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004225surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004226{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004227 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004228 float dx, dy;
4229 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004230
Pekka Paalanen460099f2012-01-20 16:48:25 +02004231 rotate = malloc(sizeof *rotate);
4232 if (!rotate)
4233 return;
4234
Jason Ekstranda7af7042013-10-12 22:38:11 -05004235 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004236 surface->surface->width * 0.5f,
4237 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004238 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004239
Daniel Stone37816df2012-05-16 18:45:18 +01004240 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4241 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004242 r = sqrtf(dx * dx + dy * dy);
4243 if (r > 20.0f) {
4244 struct weston_matrix inverse;
4245
4246 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004247 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004248 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004249
4250 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004251 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004252 } else {
4253 weston_matrix_init(&surface->rotation.rotation);
4254 weston_matrix_init(&rotate->rotation);
4255 }
4256
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004257 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4258 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004259}
4260
4261static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004262rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004263 void *data)
4264{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004265 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004266 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004267 struct shell_surface *surface;
4268
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004269 if (seat->pointer->focus == NULL)
4270 return;
4271
4272 focus = seat->pointer->focus->surface;
4273
Pekka Paalanen01388e22013-04-25 13:57:44 +03004274 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004275 if (base_surface == NULL)
4276 return;
4277
4278 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004279 if (surface == NULL || surface->state.fullscreen ||
4280 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004281 return;
4282
4283 surface_rotate(surface, seat);
4284}
4285
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004286/* Move all fullscreen layers down to the current workspace in a non-reversible
4287 * manner. This should be used when implementing shell-wide overlays, such as
4288 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004289void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004290lower_fullscreen_layer(struct desktop_shell *shell)
4291{
4292 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004293 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004294
4295 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004296 wl_list_for_each_reverse_safe(view, prev,
4297 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004298 layer_link) {
4299 wl_list_remove(&view->layer_link);
4300 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4301 weston_view_damage_below(view);
4302 weston_surface_damage(view->surface);
4303 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004304}
4305
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004306void
Tiago Vignattibe143262012-04-16 17:31:41 +03004307activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01004308 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004309{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004310 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004311 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004312 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004313 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004314 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004315
Pekka Paalanen01388e22013-04-25 13:57:44 +03004316 main_surface = weston_surface_get_main_surface(es);
4317
Daniel Stone37816df2012-05-16 18:45:18 +01004318 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004319
Jonas Ådahl8538b222012-08-29 22:13:03 +02004320 state = ensure_focus_state(shell, seat);
4321 if (state == NULL)
4322 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004323
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004324 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004325 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004326
Rafael Antognolli03b16592013-12-03 15:35:42 -02004327 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004328 assert(shsurf);
4329
Rafael Antognolli03b16592013-12-03 15:35:42 -02004330 if (shsurf->state.fullscreen)
4331 shell_configure_fullscreen(shsurf);
4332 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004333 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004334
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004335 /* Update the surface’s layer. This brings it to the top of the stacking
4336 * order as appropriate. */
4337 shell_surface_update_layer(shsurf);
4338
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004339 if (shell->focus_animation_type != ANIMATION_NONE) {
4340 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004341 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004342 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004343}
4344
Alex Wu21858432012-04-01 20:13:08 +08004345/* no-op func for checking black surface */
4346static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004347black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004348{
4349}
4350
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004351static bool
Alex Wu21858432012-04-01 20:13:08 +08004352is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4353{
4354 if (es->configure == black_surface_configure) {
4355 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004356 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004357 return true;
4358 }
4359 return false;
4360}
4361
Kristian Høgsberg75840622011-09-06 13:48:16 -04004362static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004363activate_binding(struct weston_seat *seat,
4364 struct desktop_shell *shell,
4365 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004366{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004367 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004368
Alex Wu9c35e6b2012-03-05 14:13:13 +08004369 if (!focus)
4370 return;
4371
Pekka Paalanen01388e22013-04-25 13:57:44 +03004372 if (is_black_surface(focus, &main_surface))
4373 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004374
Pekka Paalanen01388e22013-04-25 13:57:44 +03004375 main_surface = weston_surface_get_main_surface(focus);
4376 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004377 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004378
Neil Robertsa28c6932013-10-03 16:43:04 +01004379 activate(shell, focus, seat);
4380}
4381
4382static void
4383click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4384 void *data)
4385{
4386 if (seat->pointer->grab != &seat->pointer->default_grab)
4387 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004388 if (seat->pointer->focus == NULL)
4389 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004390
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004391 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004392}
4393
4394static void
4395touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4396{
4397 if (seat->touch->grab != &seat->touch->default_grab)
4398 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004399 if (seat->touch->focus == NULL)
4400 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004401
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004402 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004403}
4404
4405static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004406lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004407{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004408 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004409
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004410 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004411 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004412 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004413 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004414
4415 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004416
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004417 /* Hide all surfaces by removing the fullscreen, panel and
4418 * toplevel layers. This way nothing else can show or receive
4419 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004420
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004421 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004422 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004423 if (shell->showing_input_panels)
4424 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004425 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004426 wl_list_insert(&shell->compositor->cursor_layer.link,
4427 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004428
Pekka Paalanen77346a62011-11-30 16:26:35 +02004429 launch_screensaver(shell);
4430
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004431 /* TODO: disable bindings that should not work while locked. */
4432
4433 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004434}
4435
4436static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004437unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004438{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004439 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004440 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004441 return;
4442 }
4443
4444 /* If desktop-shell client has gone away, unlock immediately. */
4445 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004446 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004447 return;
4448 }
4449
4450 if (shell->prepare_event_sent)
4451 return;
4452
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004453 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004454 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004455}
4456
4457static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004458shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004459{
4460 struct desktop_shell *shell = data;
4461
4462 shell->fade.animation = NULL;
4463
4464 switch (shell->fade.type) {
4465 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004466 weston_surface_destroy(shell->fade.view->surface);
4467 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004468 break;
4469 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004470 lock(shell);
4471 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004472 default:
4473 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004474 }
4475}
4476
Jason Ekstranda7af7042013-10-12 22:38:11 -05004477static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004478shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004479{
4480 struct weston_compositor *compositor = shell->compositor;
4481 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004482 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004483
4484 surface = weston_surface_create(compositor);
4485 if (!surface)
4486 return NULL;
4487
Jason Ekstranda7af7042013-10-12 22:38:11 -05004488 view = weston_view_create(surface);
4489 if (!view) {
4490 weston_surface_destroy(surface);
4491 return NULL;
4492 }
4493
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004494 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004495 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004496 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004497 wl_list_insert(&compositor->fade_layer.view_list,
4498 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004499 pixman_region32_init(&surface->input);
4500
Jason Ekstranda7af7042013-10-12 22:38:11 -05004501 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004502}
4503
4504static void
4505shell_fade(struct desktop_shell *shell, enum fade_type type)
4506{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004507 float tint;
4508
4509 switch (type) {
4510 case FADE_IN:
4511 tint = 0.0;
4512 break;
4513 case FADE_OUT:
4514 tint = 1.0;
4515 break;
4516 default:
4517 weston_log("shell: invalid fade type\n");
4518 return;
4519 }
4520
4521 shell->fade.type = type;
4522
Jason Ekstranda7af7042013-10-12 22:38:11 -05004523 if (shell->fade.view == NULL) {
4524 shell->fade.view = shell_fade_create_surface(shell);
4525 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004526 return;
4527
Jason Ekstranda7af7042013-10-12 22:38:11 -05004528 shell->fade.view->alpha = 1.0 - tint;
4529 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004530 }
4531
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004532 if (shell->fade.view->output == NULL) {
4533 /* If the black view gets a NULL output, we lost the
4534 * last output and we'll just cancel the fade. This
4535 * happens when you close the last window under the
4536 * X11 or Wayland backends. */
4537 shell->locked = false;
4538 weston_surface_destroy(shell->fade.view->surface);
4539 shell->fade.view = NULL;
4540 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004541 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004542 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004543 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004544 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004545 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004546 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004547 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004548}
4549
4550static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004551do_shell_fade_startup(void *data)
4552{
4553 struct desktop_shell *shell = data;
4554
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004555 if (shell->startup_animation_type == ANIMATION_FADE)
4556 shell_fade(shell, FADE_IN);
4557 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004558 weston_surface_destroy(shell->fade.view->surface);
4559 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004560 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004561}
4562
4563static void
4564shell_fade_startup(struct desktop_shell *shell)
4565{
4566 struct wl_event_loop *loop;
4567
4568 if (!shell->fade.startup_timer)
4569 return;
4570
4571 wl_event_source_remove(shell->fade.startup_timer);
4572 shell->fade.startup_timer = NULL;
4573
4574 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4575 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4576}
4577
4578static int
4579fade_startup_timeout(void *data)
4580{
4581 struct desktop_shell *shell = data;
4582
4583 shell_fade_startup(shell);
4584 return 0;
4585}
4586
4587static void
4588shell_fade_init(struct desktop_shell *shell)
4589{
4590 /* Make compositor output all black, and wait for the desktop-shell
4591 * client to signal it is ready, then fade in. The timer triggers a
4592 * fade-in, in case the desktop-shell client takes too long.
4593 */
4594
4595 struct wl_event_loop *loop;
4596
Jason Ekstranda7af7042013-10-12 22:38:11 -05004597 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004598 weston_log("%s: warning: fade surface already exists\n",
4599 __func__);
4600 return;
4601 }
4602
Jason Ekstranda7af7042013-10-12 22:38:11 -05004603 shell->fade.view = shell_fade_create_surface(shell);
4604 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004605 return;
4606
Jason Ekstranda7af7042013-10-12 22:38:11 -05004607 weston_view_update_transform(shell->fade.view);
4608 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004609
4610 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4611 shell->fade.startup_timer =
4612 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4613 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4614}
4615
4616static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004617idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004618{
4619 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004620 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004621 struct weston_seat *seat;
4622
4623 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4624 if (seat->pointer)
4625 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004626
4627 shell_fade(shell, FADE_OUT);
4628 /* lock() is called from shell_fade_done() */
4629}
4630
4631static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004632wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004633{
4634 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004635 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004636
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004637 unlock(shell);
4638}
4639
4640static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004641center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004642{
Giulio Camuffob8366642013-04-25 13:57:46 +03004643 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004644 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004645
Jason Ekstranda7af7042013-10-12 22:38:11 -05004646 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004647
4648 x = output->x + (output->width - width) / 2 - surf_x / 2;
4649 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004650
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004651 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004652}
4653
4654static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004655weston_view_set_initial_position(struct weston_view *view,
4656 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004657{
4658 struct weston_compositor *compositor = shell->compositor;
4659 int ix = 0, iy = 0;
4660 int range_x, range_y;
4661 int dx, dy, x, y, panel_height;
4662 struct weston_output *output, *target_output = NULL;
4663 struct weston_seat *seat;
4664
4665 /* As a heuristic place the new window on the same output as the
4666 * pointer. Falling back to the output containing 0, 0.
4667 *
4668 * TODO: Do something clever for touch too?
4669 */
4670 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004671 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004672 ix = wl_fixed_to_int(seat->pointer->x);
4673 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004674 break;
4675 }
4676 }
4677
4678 wl_list_for_each(output, &compositor->output_list, link) {
4679 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4680 target_output = output;
4681 break;
4682 }
4683 }
4684
4685 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004686 weston_view_set_position(view, 10 + random() % 400,
4687 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004688 return;
4689 }
4690
4691 /* Valid range within output where the surface will still be onscreen.
4692 * If this is negative it means that the surface is bigger than
4693 * output.
4694 */
4695 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004696 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004697 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004698 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004699
Rob Bradford4cb88c72012-08-13 15:18:44 +01004700 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004701 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004702 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004703 dx = 0;
4704
4705 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004706 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004707 else
4708 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004709
4710 x = target_output->x + dx;
4711 y = target_output->y + dy;
4712
Jason Ekstranda7af7042013-10-12 22:38:11 -05004713 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004714}
4715
4716static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004717map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004718 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004719{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004720 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004721 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004722 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03004723 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004724
Pekka Paalanen77346a62011-11-30 16:26:35 +02004725 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004726 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004727 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02004728 if (shsurf->state.fullscreen) {
4729 center_on_output(shsurf->view, shsurf->fullscreen_output);
4730 shell_map_fullscreen(shsurf);
4731 } else if (shsurf->state.maximized) {
4732 /* use surface configure to set the geometry */
4733 panel_height = get_output_panel_height(shell, shsurf->output);
4734 surface_subsurfaces_boundingbox(shsurf->surface,
4735 &surf_x, &surf_y, NULL, NULL);
4736 weston_view_set_position(shsurf->view,
4737 shsurf->output->x - surf_x,
4738 shsurf->output->y +
4739 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02004740 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02004741 weston_view_set_initial_position(shsurf->view, shell);
4742 }
Juan Zhao96879df2012-02-07 08:45:41 +08004743 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02004744 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04004745 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00004746 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02004747 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004748 weston_view_set_position(shsurf->view,
4749 shsurf->view->geometry.x + sx,
4750 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02004751 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004752 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02004753 default:
4754 ;
4755 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004756
Philip Withnalle1d75ae2013-11-25 18:01:41 +00004757 /* Surface stacking order, see also activate(). */
4758 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004759
Jason Ekstranda7af7042013-10-12 22:38:11 -05004760 if (shsurf->type != SHELL_SURFACE_NONE) {
4761 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004762 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004763 shsurf->surface->output = shsurf->output;
4764 shsurf->view->output = shsurf->output;
4765 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02004766 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05004767
Jason Ekstranda7af7042013-10-12 22:38:11 -05004768 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004769 /* XXX: xwayland's using the same fields for transient type */
4770 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03004771 if (shsurf->transient.flags ==
4772 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4773 break;
4774 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02004775 if (shsurf->state.relative &&
4776 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4777 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02004778 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02004779 break;
4780 wl_list_for_each(seat, &compositor->seat_list, link)
4781 activate(shell, shsurf->surface, seat);
Juan Zhao7bb92f02011-12-15 11:31:51 -05004782 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00004783 case SHELL_SURFACE_POPUP:
4784 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05004785 default:
4786 break;
4787 }
4788
Rafael Antognolli03b16592013-12-03 15:35:42 -02004789 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4790 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08004791 {
4792 switch (shell->win_animation_type) {
4793 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004794 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004795 break;
4796 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004797 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08004798 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004799 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08004800 default:
4801 break;
4802 }
4803 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004804}
4805
4806static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004807configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004808 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004809{
Pekka Paalanen77346a62011-11-30 16:26:35 +02004810 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004811 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004812 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004813
Pekka Paalanen77346a62011-11-30 16:26:35 +02004814 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004815
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004816 assert(shsurf);
4817
Rafael Antognolli03b16592013-12-03 15:35:42 -02004818 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08004819 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004820 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08004821 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03004822 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4823 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004824 mx = shsurf->output->x - surf_x;
4825 my = shsurf->output->y +
4826 get_output_panel_height(shell,shsurf->output) - surf_y;
4827 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004828 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004829 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04004830 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05004831
Alex Wu4539b082012-03-01 12:57:46 +08004832 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05004833 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004834 wl_list_for_each(view, &surface->views, surface_link)
4835 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004836
Rafael Antognolli03b16592013-12-03 15:35:42 -02004837 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08004838 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004839 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04004840}
4841
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004842static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004843shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004844{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03004845 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004846 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03004847 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004848
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004849 assert(shsurf);
4850
4851 shell = shsurf->shell;
4852
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04004853 if (!weston_surface_is_mapped(es) &&
4854 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01004855 remove_popup_grab(shsurf);
4856 }
4857
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004858 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004859 return;
4860
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08004861 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004862 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004863 type_changed = 1;
4864 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04004865
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004866 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004867 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04004868 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004869 shsurf->last_width != es->width ||
4870 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004871 float from_x, from_y;
4872 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004873
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08004874 if (shsurf->resize_edges) {
4875 sx = 0;
4876 sy = 0;
4877 }
4878
4879 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
4880 sx = shsurf->last_width - es->width;
4881 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
4882 sy = shsurf->last_height - es->height;
4883
4884 shsurf->last_width = es->width;
4885 shsurf->last_height = es->height;
4886
Jason Ekstranda7af7042013-10-12 22:38:11 -05004887 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4888 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004889 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004890 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004891 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03004892 }
4893}
4894
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004895static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004896
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004897static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004898desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004899{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004900 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03004901 struct desktop_shell *shell =
4902 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004903
4904 shell->child.process.pid = 0;
4905 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004906
4907 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4908 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004909 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004910 shell->child.deathstamp = time;
4911 shell->child.deathcount = 0;
4912 }
4913
4914 shell->child.deathcount++;
4915 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004916 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004917 return;
4918 }
4919
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004920 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004921 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004922 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004923}
4924
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004925static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004926desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4927{
4928 struct desktop_shell *shell;
4929
4930 shell = container_of(listener, struct desktop_shell,
4931 child.client_destroy_listener);
4932
4933 shell->child.client = NULL;
4934}
4935
4936static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004937launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004938{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004939 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004940
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004941 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004942 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004943 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004944 desktop_shell_sigchld);
4945
4946 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004947 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004948
4949 shell->child.client_destroy_listener.notify =
4950 desktop_shell_client_destroy;
4951 wl_client_add_destroy_listener(shell->child.client,
4952 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004953}
4954
4955static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004956handle_shell_client_destroy(struct wl_listener *listener, void *data)
4957{
4958 struct shell_client *sc =
4959 container_of(listener, struct shell_client, destroy_listener);
4960
4961 if (sc->ping_timer)
4962 wl_event_source_remove(sc->ping_timer);
4963 free(sc);
4964}
4965
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004966static struct shell_client *
4967shell_client_create(struct wl_client *client, struct desktop_shell *shell,
4968 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02004969{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004970 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02004971
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004972 sc = zalloc(sizeof *sc);
4973 if (sc == NULL) {
4974 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004975 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004976 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004977
4978 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004979 if (sc->resource == NULL) {
4980 free(sc);
4981 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004982 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08004983 }
4984
4985 sc->client = client;
4986 sc->shell = shell;
4987 sc->destroy_listener.notify = handle_shell_client_destroy;
4988 wl_client_add_destroy_listener(client, &sc->destroy_listener);
4989
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08004990 return sc;
4991}
4992
4993static void
4994bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
4995{
4996 struct desktop_shell *shell = data;
4997 struct shell_client *sc;
4998
4999 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5000 if (sc)
5001 wl_resource_set_implementation(sc->resource,
5002 &shell_implementation,
5003 shell, NULL);
5004}
5005
5006static void
5007bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5008{
5009 struct desktop_shell *shell = data;
5010 struct shell_client *sc;
5011
5012 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5013 if (sc)
5014 wl_resource_set_dispatcher(sc->resource,
5015 xdg_shell_unversioned_dispatch,
5016 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005017}
5018
5019static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005020unbind_desktop_shell(struct wl_resource *resource)
5021{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005022 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005023
5024 if (shell->locked)
5025 resume_desktop(shell);
5026
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005027 shell->child.desktop_shell = NULL;
5028 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005029}
5030
5031static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005032bind_desktop_shell(struct wl_client *client,
5033 void *data, uint32_t version, uint32_t id)
5034{
Tiago Vignattibe143262012-04-16 17:31:41 +03005035 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005036 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005037
Jason Ekstranda85118c2013-06-27 20:17:02 -05005038 resource = wl_resource_create(client, &desktop_shell_interface,
5039 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005040
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005041 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005042 wl_resource_set_implementation(resource,
5043 &desktop_shell_implementation,
5044 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005045 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005046
5047 if (version < 2)
5048 shell_fade_startup(shell);
5049
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005050 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005051 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005052
5053 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5054 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005055 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005056}
5057
Pekka Paalanen6e168112011-11-24 11:34:05 +02005058static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005059screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005060{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005061 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005062 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005063
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005064 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005065 return;
5066
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005067 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005068 if (!shell->locked)
5069 return;
5070
Jason Ekstranda7af7042013-10-12 22:38:11 -05005071 view = container_of(surface->views.next, struct weston_view, surface_link);
5072 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005073
Jason Ekstranda7af7042013-10-12 22:38:11 -05005074 if (wl_list_empty(&view->layer_link)) {
5075 wl_list_insert(shell->lock_layer.view_list.prev,
5076 &view->layer_link);
5077 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005078 wl_event_source_timer_update(shell->screensaver.timer,
5079 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005080 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005081 }
5082}
5083
5084static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005085screensaver_set_surface(struct wl_client *client,
5086 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005087 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005088 struct wl_resource *output_resource)
5089{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005090 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005091 struct weston_surface *surface =
5092 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005093 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005094 struct weston_view *view, *next;
5095
5096 /* Make sure we only have one view */
5097 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5098 weston_view_destroy(view);
5099 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005100
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005101 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005102 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005103 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005104}
5105
5106static const struct screensaver_interface screensaver_implementation = {
5107 screensaver_set_surface
5108};
5109
5110static void
5111unbind_screensaver(struct wl_resource *resource)
5112{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005113 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005114
Pekka Paalanen77346a62011-11-30 16:26:35 +02005115 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005116}
5117
5118static void
5119bind_screensaver(struct wl_client *client,
5120 void *data, uint32_t version, uint32_t id)
5121{
Tiago Vignattibe143262012-04-16 17:31:41 +03005122 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005123 struct wl_resource *resource;
5124
Jason Ekstranda85118c2013-06-27 20:17:02 -05005125 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005126
Pekka Paalanen77346a62011-11-30 16:26:35 +02005127 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005128 wl_resource_set_implementation(resource,
5129 &screensaver_implementation,
5130 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005131 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005132 return;
5133 }
5134
5135 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5136 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005137 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005138}
5139
Kristian Høgsberg07045392012-02-19 18:52:44 -05005140struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005141 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005142 struct weston_surface *current;
5143 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005144 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005145};
5146
5147static void
5148switcher_next(struct switcher *switcher)
5149{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005150 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005151 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005152 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005153 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005154
Jason Ekstranda7af7042013-10-12 22:38:11 -05005155 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005156 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005157 if (shsurf &&
5158 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5159 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005160 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005161 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005162 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005163 next = view->surface;
5164 prev = view->surface;
5165 view->alpha = 0.25;
5166 weston_view_geometry_dirty(view);
5167 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005168 }
Alex Wu1659daa2012-04-01 20:13:09 +08005169
Jason Ekstranda7af7042013-10-12 22:38:11 -05005170 if (is_black_surface(view->surface, NULL)) {
5171 view->alpha = 0.25;
5172 weston_view_geometry_dirty(view);
5173 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005174 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005175 }
5176
5177 if (next == NULL)
5178 next = first;
5179
Alex Wu07b26062012-03-12 16:06:01 +08005180 if (next == NULL)
5181 return;
5182
Kristian Høgsberg07045392012-02-19 18:52:44 -05005183 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005184 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005185
5186 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005187 wl_list_for_each(view, &next->views, surface_link)
5188 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005189
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005190 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005191 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005192 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005193}
5194
5195static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005196switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005197{
5198 struct switcher *switcher =
5199 container_of(listener, struct switcher, listener);
5200
5201 switcher_next(switcher);
5202}
5203
5204static void
Daniel Stone351eb612012-05-31 15:27:47 -04005205switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005206{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005207 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005208 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005209 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005210
Jason Ekstranda7af7042013-10-12 22:38:11 -05005211 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005212 if (is_focus_view(view))
5213 continue;
5214
Jason Ekstranda7af7042013-10-12 22:38:11 -05005215 view->alpha = 1.0;
5216 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005217 }
5218
Alex Wu07b26062012-03-12 16:06:01 +08005219 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005220 activate(switcher->shell, switcher->current,
5221 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005222 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005223 weston_keyboard_end_grab(keyboard);
5224 if (keyboard->input_method_resource)
5225 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005226 free(switcher);
5227}
5228
5229static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005230switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005231 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005232{
5233 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005234 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005235
Daniel Stonec9785ea2012-05-30 16:31:52 +01005236 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005237 switcher_next(switcher);
5238}
5239
5240static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005241switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005242 uint32_t mods_depressed, uint32_t mods_latched,
5243 uint32_t mods_locked, uint32_t group)
5244{
5245 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005246 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005247
Daniel Stone351eb612012-05-31 15:27:47 -04005248 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5249 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005250}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005251
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005252static void
5253switcher_cancel(struct weston_keyboard_grab *grab)
5254{
5255 struct switcher *switcher = container_of(grab, struct switcher, grab);
5256
5257 switcher_destroy(switcher);
5258}
5259
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005260static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005261 switcher_key,
5262 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005263 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005264};
5265
5266static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005267switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005268 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005269{
Tiago Vignattibe143262012-04-16 17:31:41 +03005270 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005271 struct switcher *switcher;
5272
5273 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005274 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005275 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005276 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005277 wl_list_init(&switcher->listener.link);
5278
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005279 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005280 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005281 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005282 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5283 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005284 switcher_next(switcher);
5285}
5286
Pekka Paalanen3c647232011-12-22 13:43:43 +02005287static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005288backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005289 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005290{
5291 struct weston_compositor *compositor = data;
5292 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005293 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005294
5295 /* TODO: we're limiting to simple use cases, where we assume just
5296 * control on the primary display. We'd have to extend later if we
5297 * ever get support for setting backlights on random desktop LCD
5298 * panels though */
5299 output = get_default_output(compositor);
5300 if (!output)
5301 return;
5302
5303 if (!output->set_backlight)
5304 return;
5305
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005306 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5307 backlight_new = output->backlight_current - 25;
5308 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5309 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005310
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005311 if (backlight_new < 5)
5312 backlight_new = 5;
5313 if (backlight_new > 255)
5314 backlight_new = 255;
5315
5316 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005317 output->set_backlight(output, output->backlight_current);
5318}
5319
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005320struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005321 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005322 struct weston_seat *seat;
5323 uint32_t key[2];
5324 int key_released[2];
5325};
5326
5327static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005328debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005329 uint32_t key, uint32_t state)
5330{
5331 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005332 struct weston_compositor *ec = db->seat->compositor;
5333 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005334 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005335 uint32_t serial;
5336 int send = 0, terminate = 0;
5337 int check_binding = 1;
5338 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005339 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005340
5341 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5342 /* Do not run bindings on key releases */
5343 check_binding = 0;
5344
5345 for (i = 0; i < 2; i++)
5346 if (key == db->key[i])
5347 db->key_released[i] = 1;
5348
5349 if (db->key_released[0] && db->key_released[1]) {
5350 /* All key releases been swalled so end the grab */
5351 terminate = 1;
5352 } else if (key != db->key[0] && key != db->key[1]) {
5353 /* Should not swallow release of other keys */
5354 send = 1;
5355 }
5356 } else if (key == db->key[0] && !db->key_released[0]) {
5357 /* Do not check bindings for the first press of the binding
5358 * key. This allows it to be used as a debug shortcut.
5359 * We still need to swallow this event. */
5360 check_binding = 0;
5361 } else if (db->key[1]) {
5362 /* If we already ran a binding don't process another one since
5363 * we can't keep track of all the binding keys that were
5364 * pressed in order to swallow the release events. */
5365 send = 1;
5366 check_binding = 0;
5367 }
5368
5369 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005370 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5371 key, state)) {
5372 /* We ran a binding so swallow the press and keep the
5373 * grab to swallow the released too. */
5374 send = 0;
5375 terminate = 0;
5376 db->key[1] = key;
5377 } else {
5378 /* Terminate the grab since the key pressed is not a
5379 * debug binding key. */
5380 send = 1;
5381 terminate = 1;
5382 }
5383 }
5384
5385 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005386 serial = wl_display_next_serial(display);
5387 resource_list = &grab->keyboard->focus_resource_list;
5388 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005389 wl_keyboard_send_key(resource, serial, time, key, state);
5390 }
5391 }
5392
5393 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005394 weston_keyboard_end_grab(grab->keyboard);
5395 if (grab->keyboard->input_method_resource)
5396 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005397 free(db);
5398 }
5399}
5400
5401static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005402debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005403 uint32_t mods_depressed, uint32_t mods_latched,
5404 uint32_t mods_locked, uint32_t group)
5405{
5406 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005407 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005408
Neil Roberts96d790e2013-09-19 17:32:00 +01005409 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005410
Neil Roberts96d790e2013-09-19 17:32:00 +01005411 wl_resource_for_each(resource, resource_list) {
5412 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5413 mods_latched, mods_locked, group);
5414 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005415}
5416
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005417static void
5418debug_binding_cancel(struct weston_keyboard_grab *grab)
5419{
5420 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5421
5422 weston_keyboard_end_grab(grab->keyboard);
5423 free(db);
5424}
5425
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005426struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005427 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005428 debug_binding_modifiers,
5429 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005430};
5431
5432static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005433debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005434{
5435 struct debug_binding_grab *grab;
5436
5437 grab = calloc(1, sizeof *grab);
5438 if (!grab)
5439 return;
5440
5441 grab->seat = (struct weston_seat *) seat;
5442 grab->key[0] = key;
5443 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005444 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005445}
5446
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005447static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005448force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005449 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005450{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005451 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005452 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005453 struct desktop_shell *shell = data;
5454 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005455 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005456
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005457 focus_surface = seat->keyboard->focus;
5458 if (!focus_surface)
5459 return;
5460
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005461 wl_signal_emit(&compositor->kill_signal, focus_surface);
5462
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005463 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005464 wl_client_get_credentials(client, &pid, NULL, NULL);
5465
5466 /* Skip clients that we launched ourselves (the credentials of
5467 * the socketpair is ours) */
5468 if (pid == getpid())
5469 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005470
Daniel Stone325fc2d2012-05-30 16:31:58 +01005471 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005472}
5473
5474static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005475workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005476 uint32_t key, void *data)
5477{
5478 struct desktop_shell *shell = data;
5479 unsigned int new_index = shell->workspaces.current;
5480
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005481 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005482 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005483 if (new_index != 0)
5484 new_index--;
5485
5486 change_workspace(shell, new_index);
5487}
5488
5489static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005490workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005491 uint32_t key, void *data)
5492{
5493 struct desktop_shell *shell = data;
5494 unsigned int new_index = shell->workspaces.current;
5495
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005496 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005497 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005498 if (new_index < shell->workspaces.num - 1)
5499 new_index++;
5500
5501 change_workspace(shell, new_index);
5502}
5503
5504static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005505workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005506 uint32_t key, void *data)
5507{
5508 struct desktop_shell *shell = data;
5509 unsigned int new_index;
5510
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005511 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005512 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005513 new_index = key - KEY_F1;
5514 if (new_index >= shell->workspaces.num)
5515 new_index = shell->workspaces.num - 1;
5516
5517 change_workspace(shell, new_index);
5518}
5519
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005520static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005521workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005522 uint32_t key, void *data)
5523{
5524 struct desktop_shell *shell = data;
5525 unsigned int new_index = shell->workspaces.current;
5526
5527 if (shell->locked)
5528 return;
5529
5530 if (new_index != 0)
5531 new_index--;
5532
5533 take_surface_to_workspace_by_seat(shell, seat, new_index);
5534}
5535
5536static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005537workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005538 uint32_t key, void *data)
5539{
5540 struct desktop_shell *shell = data;
5541 unsigned int new_index = shell->workspaces.current;
5542
5543 if (shell->locked)
5544 return;
5545
5546 if (new_index < shell->workspaces.num - 1)
5547 new_index++;
5548
5549 take_surface_to_workspace_by_seat(shell, seat, new_index);
5550}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005551
5552static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005553shell_reposition_view_on_output_destroy(struct weston_view *view)
5554{
5555 struct weston_output *output, *first_output;
5556 struct weston_compositor *ec = view->surface->compositor;
5557 struct shell_surface *shsurf;
5558 float x, y;
5559 int visible;
5560
5561 x = view->geometry.x;
5562 y = view->geometry.y;
5563
5564 /* At this point the destroyed output is not in the list anymore.
5565 * If the view is still visible somewhere, we leave where it is,
5566 * otherwise, move it to the first output. */
5567 visible = 0;
5568 wl_list_for_each(output, &ec->output_list, link) {
5569 if (pixman_region32_contains_point(&output->region,
5570 x, y, NULL)) {
5571 visible = 1;
5572 break;
5573 }
5574 }
5575
5576 if (!visible) {
5577 first_output = container_of(ec->output_list.next,
5578 struct weston_output, link);
5579
5580 x = first_output->x + first_output->width / 4;
5581 y = first_output->y + first_output->height / 4;
5582 }
5583
5584 weston_view_set_position(view, x, y);
5585
5586 shsurf = get_shell_surface(view->surface);
5587
5588 if (shsurf) {
5589 shsurf->saved_position_valid = false;
5590 shsurf->next_state.maximized = false;
5591 shsurf->next_state.fullscreen = false;
5592 shsurf->state_changed = true;
5593 }
5594}
5595
5596static void
5597shell_reposition_views_on_output_destroy(struct shell_output *shell_output)
5598{
5599 struct desktop_shell *shell = shell_output->shell;
5600 struct weston_output *output = shell_output->output;
5601 struct weston_layer *layer;
5602 struct weston_view *view;
5603
5604 /* Move all views in the layers owned by the shell */
5605 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5606 wl_list_for_each(view, &layer->view_list, layer_link) {
5607 if (view->output != output)
5608 continue;
5609
5610 shell_reposition_view_on_output_destroy(view);
5611 }
5612
5613 /* We don't start from the beggining of the layer list, so
5614 * make sure we don't wrap around it. */
5615 if (layer == &shell->background_layer)
5616 break;
5617 }
5618}
5619
5620static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005621handle_output_destroy(struct wl_listener *listener, void *data)
5622{
5623 struct shell_output *output_listener =
5624 container_of(listener, struct shell_output, destroy_listener);
5625
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005626 shell_reposition_views_on_output_destroy(output_listener);
5627
Xiong Zhang6b481422013-10-23 13:58:32 +08005628 wl_list_remove(&output_listener->destroy_listener.link);
5629 wl_list_remove(&output_listener->link);
5630 free(output_listener);
5631}
5632
5633static void
5634create_shell_output(struct desktop_shell *shell,
5635 struct weston_output *output)
5636{
5637 struct shell_output *shell_output;
5638
5639 shell_output = zalloc(sizeof *shell_output);
5640 if (shell_output == NULL)
5641 return;
5642
5643 shell_output->output = output;
5644 shell_output->shell = shell;
5645 shell_output->destroy_listener.notify = handle_output_destroy;
5646 wl_signal_add(&output->destroy_signal,
5647 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005648 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005649}
5650
5651static void
5652handle_output_create(struct wl_listener *listener, void *data)
5653{
5654 struct desktop_shell *shell =
5655 container_of(listener, struct desktop_shell, output_create_listener);
5656 struct weston_output *output = (struct weston_output *)data;
5657
5658 create_shell_output(shell, output);
5659}
5660
5661static void
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005662handle_output_move(struct wl_listener *listener, void *data)
5663{
5664 struct desktop_shell *shell;
5665 struct weston_output *output;
5666 struct weston_layer *layer;
5667 struct weston_view *view;
5668 float x, y;
5669
5670 shell = container_of(listener, struct desktop_shell,
5671 output_move_listener);
5672 output = data;
5673
5674 /* Move all views in the layers owned by the shell */
5675 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5676 wl_list_for_each(view, &layer->view_list, layer_link) {
5677 if (view->output != output)
5678 continue;
5679
5680 x = view->geometry.x + output->move_x;
5681 y = view->geometry.y + output->move_y;
5682 weston_view_set_position(view, x, y);
5683 }
5684
5685 /* We don't start from the beggining of the layer list, so
5686 * make sure we don't wrap around it. */
5687 if (layer == &shell->background_layer)
5688 break;
5689 }
5690}
5691
5692static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005693setup_output_destroy_handler(struct weston_compositor *ec,
5694 struct desktop_shell *shell)
5695{
5696 struct weston_output *output;
5697
5698 wl_list_init(&shell->output_list);
5699 wl_list_for_each(output, &ec->output_list, link)
5700 create_shell_output(shell, output);
5701
5702 shell->output_create_listener.notify = handle_output_create;
5703 wl_signal_add(&ec->output_created_signal,
5704 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005705
5706 shell->output_move_listener.notify = handle_output_move;
5707 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005708}
5709
5710static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005711shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005712{
Tiago Vignattibe143262012-04-16 17:31:41 +03005713 struct desktop_shell *shell =
5714 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005715 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005716 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005717
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005718 /* Force state to unlocked so we don't try to fade */
5719 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005720 if (shell->child.client)
5721 wl_client_destroy(shell->child.client);
5722
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005723 wl_list_remove(&shell->idle_listener.link);
5724 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005725
5726 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005727
Xiong Zhang6b481422013-10-23 13:58:32 +08005728 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5729 wl_list_remove(&shell_output->destroy_listener.link);
5730 wl_list_remove(&shell_output->link);
5731 free(shell_output);
5732 }
5733
5734 wl_list_remove(&shell->output_create_listener.link);
5735
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005736 wl_array_for_each(ws, &shell->workspaces.array)
5737 workspace_destroy(*ws);
5738 wl_array_release(&shell->workspaces.array);
5739
Pekka Paalanen3c647232011-12-22 13:43:43 +02005740 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005741 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005742 free(shell);
5743}
5744
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005745static void
5746shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5747{
5748 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005749 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005750
5751 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005752 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5753 MODIFIER_CTRL | MODIFIER_ALT,
5754 terminate_binding, ec);
5755 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5756 click_to_activate_binding,
5757 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005758 weston_compositor_add_touch_binding(ec, 0,
5759 touch_to_activate_binding,
5760 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005761 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5762 MODIFIER_SUPER | MODIFIER_ALT,
5763 surface_opacity_binding, NULL);
5764 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5765 MODIFIER_SUPER, zoom_axis_binding,
5766 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005767
5768 /* configurable bindings */
5769 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01005770 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5771 zoom_key_binding, NULL);
5772 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5773 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005774 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5775 maximize_binding, NULL);
5776 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5777 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005778 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5779 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005780 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005781 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5782 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005783 weston_compositor_add_button_binding(ec, BTN_LEFT,
5784 mod | MODIFIER_SHIFT,
5785 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005786
5787 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5788 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5789 rotate_binding, NULL);
5790
Daniel Stone325fc2d2012-05-30 16:31:58 +01005791 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5792 shell);
5793 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5794 ec);
5795 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5796 backlight_binding, ec);
5797 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5798 ec);
5799 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5800 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005801 weston_compositor_add_key_binding(ec, KEY_K, mod,
5802 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005803 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5804 workspace_up_binding, shell);
5805 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5806 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005807 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5808 workspace_move_surface_up_binding,
5809 shell);
5810 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5811 workspace_move_surface_down_binding,
5812 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005813
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08005814 if (shell->exposay_modifier)
5815 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5816 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01005817
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005818 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5819 if (shell->workspaces.num > 1) {
5820 num_workspace_bindings = shell->workspaces.num;
5821 if (num_workspace_bindings > 6)
5822 num_workspace_bindings = 6;
5823 for (i = 0; i < num_workspace_bindings; i++)
5824 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5825 workspace_f_binding,
5826 shell);
5827 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005828
5829 /* Debug bindings */
5830 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5831 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005832}
5833
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005834WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005835module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07005836 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005837{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005838 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005839 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005840 struct workspace **pws;
5841 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005842 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005843
Peter Huttererf3d62272013-08-08 11:57:05 +10005844 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005845 if (shell == NULL)
5846 return -1;
5847
Kristian Høgsberg75840622011-09-06 13:48:16 -04005848 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005849
5850 shell->destroy_listener.notify = shell_destroy;
5851 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005852 shell->idle_listener.notify = idle_handler;
5853 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5854 shell->wake_listener.notify = wake_handler;
5855 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005856
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04005857 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005858 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005859 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03005860 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04005861 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05005862 ec->shell_interface.set_fullscreen = set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005863 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04005864 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04005865 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02005866 ec->shell_interface.set_title = set_title;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005867
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005868 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5869 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005870 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5871 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02005872 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005873
5874 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005875 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005876
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005877 if (input_panel_setup(shell) < 0)
5878 return -1;
5879
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005880 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005881
Daniel Stonedf8133b2013-11-19 11:37:14 +01005882 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5883 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5884
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005885 for (i = 0; i < shell->workspaces.num; i++) {
5886 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5887 if (pws == NULL)
5888 return -1;
5889
5890 *pws = workspace_create();
5891 if (*pws == NULL)
5892 return -1;
5893 }
5894 activate_workspace(shell, 0);
5895
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005896 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005897 wl_list_init(&shell->workspaces.animation.link);
5898 shell->workspaces.animation.frame = animate_workspace_change_frame;
5899
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005900 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005901 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005902 return -1;
5903
Rafael Antognollie2a34552013-12-03 15:35:45 -02005904 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
5905 shell, bind_xdg_shell) == NULL)
5906 return -1;
5907
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005908 if (wl_global_create(ec->wl_display,
5909 &desktop_shell_interface, 2,
5910 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005911 return -1;
5912
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005913 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
5914 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02005915 return -1;
5916
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005917 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
5918 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02005919 return -1;
5920
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005921 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005922
Xiong Zhang6b481422013-10-23 13:58:32 +08005923 setup_output_destroy_handler(ec, shell);
5924
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005925 loop = wl_display_get_event_loop(ec->wl_display);
5926 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005927
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005928 shell->screensaver.timer =
5929 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
5930
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05005931 wl_list_for_each(seat, &ec->seat_list, link) {
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005932 create_pointer_focus_listener(seat);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05005933 create_keyboard_focus_listener(seat);
5934 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005935
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005936 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005937
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005938 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005939
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005940 return 0;
5941}