blob: 593c7f3ba952012469dba7591ca5ef57b5afafa0 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
26
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040028#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050029#include <string.h>
30#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040031#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020032#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020033#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020034#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040035#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080037#include "shell.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040038#include "desktop-shell-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020039#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020040#include "../shared/config-parser.h"
Rafael Antognollie2a34552013-12-03 15:35:45 -020041#include "xdg-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020044#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020045
Giulio Camuffo1aaf3e42013-12-09 22:47:58 +010046#ifndef static_assert
47#define static_assert(cond, msg)
48#endif
49
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
51 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040052 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050059enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020060 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050061 SHELL_SURFACE_TOPLEVEL,
Tiago Vignattifb2adba2013-06-12 15:43:21 -030062 SHELL_SURFACE_POPUP,
63 SHELL_SURFACE_XWAYLAND
Pekka Paalanen57da4a82011-11-23 16:42:16 +020064};
65
Jason Ekstrand9e9512f2014-04-16 21:12:10 -050066struct shell_client;
67
Philip Withnall648a4dd2013-11-25 18:01:44 +000068/*
69 * Surface stacking and ordering.
70 *
71 * This is handled using several linked lists of surfaces, organised into
72 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
73 * above all of the surfaces in the layer below. The set of layers is static and
74 * in the following order (top-most first):
75 * • Lock layer (only ever displayed on its own)
76 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010077 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000078 * • Fullscreen layer
79 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000080 * • Workspace layers
81 * • Background layer
82 *
83 * The list of layers may be manipulated to remove whole layers of surfaces from
84 * display. For example, when locking the screen, all layers except the lock
85 * layer are removed.
86 *
87 * A surface’s layer is modified on configuring the surface, in
88 * set_surface_type() (which is only called when the surface’s type change is
89 * _committed_). If a surface’s type changes (e.g. when making a window
90 * fullscreen) its layer changes too.
91 *
92 * In order to allow popup and transient surfaces to be correctly stacked above
93 * their parent surfaces, each surface tracks both its parent surface, and a
94 * linked list of its children. When a surface’s layer is updated, so are the
95 * layers of its children. Note that child surfaces are *not* the same as
96 * subsurfaces — child/parent surfaces are purely for maintaining stacking
97 * order.
98 *
99 * The children_link list of siblings of a surface (i.e. those surfaces which
100 * have the same parent) only contains weston_surfaces which have a
101 * shell_surface. Stacking is not implemented for non-shell_surface
102 * weston_surfaces. This means that the following implication does *not* hold:
103 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
104 */
105
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200106struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500107 struct wl_resource *resource;
108 struct wl_signal destroy_signal;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -0500109 struct shell_client *owner;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200110
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500111 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500112 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600113 int32_t last_width, last_height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200114 struct wl_listener surface_destroy_listener;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700115 struct wl_listener resource_destroy_listener;
116
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400117 struct weston_surface *parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +0000118 struct wl_list children_list; /* child surfaces of this one */
119 struct wl_list children_link; /* sibling surfaces of this one */
Tiago Vignattibe143262012-04-16 17:31:41 +0300120 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200121
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -0800122 enum shell_surface_type type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400123 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500124 int32_t saved_x, saved_y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200125 int32_t saved_width, saved_height;
Alex Wu4539b082012-03-01 12:57:46 +0800126 bool saved_position_valid;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -0200127 bool saved_size_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800128 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700129 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800130 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100131
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500132 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200133 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500134 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200135 } rotation;
136
137 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100138 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500139 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100140 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400141 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500142 } popup;
143
Alex Wu4539b082012-03-01 12:57:46 +0800144 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300145 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400146 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300147 } transient;
148
149 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800150 enum wl_shell_surface_fullscreen_method type;
151 struct weston_transform transform; /* matrix from x, y */
152 uint32_t framerate;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500153 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800154 } fullscreen;
155
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200156 struct weston_transform workspace_transform;
157
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500158 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500159 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100160 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400161
162 const struct weston_shell_client *client;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200163
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700164 struct surface_state {
Rafael Antognolli03b16592013-12-03 15:35:42 -0200165 bool maximized;
166 bool fullscreen;
Rafael Antognollied207b42013-12-03 15:35:43 -0200167 bool relative;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100168 bool lowered;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500169 } state, next_state, requested_state; /* surface states */
Rafael Antognolli03b16592013-12-03 15:35:42 -0200170 bool state_changed;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500171 bool state_requested;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500172
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700173 struct {
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -0400174 int32_t x, y, width, height;
175 } geometry, next_geometry;
176 bool has_set_geometry, has_next_geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700177
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500178 int focus_count;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200179};
180
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300181struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400182 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300183 struct shell_surface *shsurf;
184 struct wl_listener shsurf_destroy_listener;
185};
186
Rusty Lynch1084da52013-08-15 09:10:08 -0700187struct shell_touch_grab {
188 struct weston_touch_grab grab;
189 struct shell_surface *shsurf;
190 struct wl_listener shsurf_destroy_listener;
191 struct weston_touch *touch;
192};
193
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300194struct weston_move_grab {
195 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100196 wl_fixed_t dx, dy;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700197 int client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500198};
199
Rusty Lynch1084da52013-08-15 09:10:08 -0700200struct weston_touch_move_grab {
201 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800202 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700203 wl_fixed_t dx, dy;
204};
205
Pekka Paalanen460099f2012-01-20 16:48:25 +0200206struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300207 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500208 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200209 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200210 float x;
211 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200212 } center;
213};
214
Giulio Camuffo5085a752013-03-25 21:42:45 +0100215struct shell_seat {
216 struct weston_seat *seat;
217 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500218 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100219
Jason Ekstrand024177c2014-04-21 19:42:58 -0500220 struct wl_listener caps_changed_listener;
221 struct wl_listener pointer_focus_listener;
222 struct wl_listener keyboard_focus_listener;
223
Giulio Camuffo5085a752013-03-25 21:42:45 +0100224 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400225 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100226 struct wl_list surfaces_list;
227 struct wl_client *client;
228 int32_t initial_up;
229 } popup_grab;
230};
231
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800232struct shell_client {
233 struct wl_resource *resource;
234 struct wl_client *client;
235 struct desktop_shell *shell;
236 struct wl_listener destroy_listener;
237 struct wl_event_source *ping_timer;
238 uint32_t ping_serial;
239 int unresponsive;
240};
241
Alex Wubd3354b2012-04-17 17:20:49 +0800242static struct desktop_shell *
243shell_surface_get_shell(struct shell_surface *shsurf);
244
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500245static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400246surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500247
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300248static void
249shell_fade_startup(struct desktop_shell *shell);
250
Philip Withnallbecb77e2013-11-25 18:01:30 +0000251static struct shell_seat *
252get_shell_seat(struct weston_seat *seat);
253
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700254static int
255get_output_panel_height(struct desktop_shell *shell,
256 struct weston_output *output);
257
Philip Withnall648a4dd2013-11-25 18:01:44 +0000258static void
259shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
260
Alex Wubd3354b2012-04-17 17:20:49 +0800261static bool
Rafael Antognollie2a34552013-12-03 15:35:45 -0200262shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
263
264static bool
265shell_surface_is_xdg_surface(struct shell_surface *shsurf);
266
267static bool
268shell_surface_is_xdg_popup(struct shell_surface *shsurf);
269
270static void
271shell_surface_set_parent(struct shell_surface *shsurf,
272 struct weston_surface *parent);
273
274static bool
Alex Wubd3354b2012-04-17 17:20:49 +0800275shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
276{
277 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278 struct weston_view *top_fs_ev;
Alex Wubd3354b2012-04-17 17:20:49 +0800279
280 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100281
Jason Ekstranda7af7042013-10-12 22:38:11 -0500282 if (wl_list_empty(&shell->fullscreen_layer.view_list))
Alex Wubd3354b2012-04-17 17:20:49 +0800283 return false;
284
Jason Ekstranda7af7042013-10-12 22:38:11 -0500285 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
286 struct weston_view,
Alex Wubd3354b2012-04-17 17:20:49 +0800287 layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500288 return (shsurf == get_shell_surface(top_fs_ev->surface));
Alex Wubd3354b2012-04-17 17:20:49 +0800289}
290
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500291static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400292destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300293{
294 struct shell_grab *grab;
295
296 grab = container_of(listener, struct shell_grab,
297 shsurf_destroy_listener);
298
299 grab->shsurf = NULL;
300}
301
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800302struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500303get_default_view(struct weston_surface *surface)
304{
305 struct shell_surface *shsurf;
306 struct weston_view *view;
307
308 if (!surface || wl_list_empty(&surface->views))
309 return NULL;
310
311 shsurf = get_shell_surface(surface);
312 if (shsurf)
313 return shsurf->view;
314
315 wl_list_for_each(view, &surface->views, surface_link)
316 if (weston_view_is_mapped(view))
317 return view;
318
319 return container_of(surface->views.next, struct weston_view, surface_link);
320}
321
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300322static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400323popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400324
325static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300326shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400327 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300328 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400329 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300330 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300331{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300332 struct desktop_shell *shell = shsurf->shell;
333
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400334 popup_grab_end(pointer);
335
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300336 grab->grab.interface = interface;
337 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400338 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500339 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400340 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300341
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700342 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400343 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400344 if (shell->child.desktop_shell) {
345 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
346 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500347 weston_pointer_set_focus(pointer,
348 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400349 wl_fixed_from_int(0),
350 wl_fixed_from_int(0));
351 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300352}
353
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700354static int
355get_output_panel_height(struct desktop_shell *shell,
356 struct weston_output *output)
357{
358 struct weston_view *view;
359 int panel_height = 0;
360
361 if (!output)
362 return 0;
363
364 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
365 if (view->surface->output == output) {
366 panel_height = view->surface->height;
367 break;
368 }
369 }
370
371 return panel_height;
372}
373
374static void
375send_configure_for_surface(struct shell_surface *shsurf)
376{
377 int32_t width, height;
378 struct surface_state *state;
379
380 if (shsurf->state_requested)
381 state = &shsurf->requested_state;
382 else if (shsurf->state_changed)
383 state = &shsurf->next_state;
384 else
385 state = &shsurf->state;
386
387 if (state->fullscreen) {
388 width = shsurf->output->width;
389 height = shsurf->output->height;
390 } else if (state->maximized) {
391 struct desktop_shell *shell;
392 uint32_t panel_height = 0;
393
394 shell = shell_surface_get_shell(shsurf);
395 panel_height = get_output_panel_height(shell, shsurf->output);
396
397 width = shsurf->output->width;
398 height = shsurf->output->height - panel_height;
399 } else {
400 width = 0;
401 height = 0;
402 }
403
404 shsurf->client->send_configure(shsurf->surface, width, height);
405}
406
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300407static void
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400408shell_surface_state_changed(struct shell_surface *shsurf)
409{
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700410 if (shell_surface_is_xdg_surface(shsurf))
411 send_configure_for_surface(shsurf);
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400412}
413
414static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300415shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300416{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700417 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400418 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700419 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400420
421 if (grab->shsurf->resize_edges) {
422 grab->shsurf->resize_edges = 0;
423 shell_surface_state_changed(grab->shsurf);
424 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700425 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300426
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700427 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300428}
429
430static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700431shell_touch_grab_start(struct shell_touch_grab *grab,
432 const struct weston_touch_grab_interface *interface,
433 struct shell_surface *shsurf,
434 struct weston_touch *touch)
435{
436 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800437
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700438 if (touch->seat->pointer)
439 popup_grab_end(touch->seat->pointer);
440
Rusty Lynch1084da52013-08-15 09:10:08 -0700441 grab->grab.interface = interface;
442 grab->shsurf = shsurf;
443 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
444 wl_signal_add(&shsurf->destroy_signal,
445 &grab->shsurf_destroy_listener);
446
447 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700448 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700449
450 weston_touch_start_grab(touch, &grab->grab);
451 if (shell->child.desktop_shell)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500452 weston_touch_set_focus(touch->seat,
453 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700454}
455
456static void
457shell_touch_grab_end(struct shell_touch_grab *grab)
458{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700459 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700460 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700461 grab->shsurf->grabbed = 0;
462 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700463
464 weston_touch_end_grab(grab->touch);
465}
466
467static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500468center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800469 struct weston_output *output);
470
Daniel Stone496ca172012-05-30 16:31:42 +0100471static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300472get_modifier(char *modifier)
473{
474 if (!modifier)
475 return MODIFIER_SUPER;
476
477 if (!strcmp("ctrl", modifier))
478 return MODIFIER_CTRL;
479 else if (!strcmp("alt", modifier))
480 return MODIFIER_ALT;
481 else if (!strcmp("super", modifier))
482 return MODIFIER_SUPER;
483 else
484 return MODIFIER_SUPER;
485}
486
Juan Zhaoe10d2792012-04-25 19:09:52 +0800487static enum animation_type
488get_animation_type(char *animation)
489{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800490 if (!animation)
491 return ANIMATION_NONE;
492
Juan Zhaoe10d2792012-04-25 19:09:52 +0800493 if (!strcmp("zoom", animation))
494 return ANIMATION_ZOOM;
495 else if (!strcmp("fade", animation))
496 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100497 else if (!strcmp("dim-layer", animation))
498 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800499 else
500 return ANIMATION_NONE;
501}
502
Alex Wu4539b082012-03-01 12:57:46 +0800503static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400504shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200505{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400506 struct weston_config_section *section;
507 int duration;
508 char *s;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200509
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400510 section = weston_config_get_section(shell->compositor->config,
511 "screensaver", NULL, NULL);
512 weston_config_section_get_string(section,
513 "path", &shell->screensaver.path, NULL);
514 weston_config_section_get_int(section, "duration", &duration, 60);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200515 shell->screensaver.duration = duration * 1000;
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400516
517 section = weston_config_get_section(shell->compositor->config,
518 "shell", NULL, NULL);
519 weston_config_section_get_string(section,
Emilio Pozuelo Monfort8a81b832013-12-02 12:53:32 +0100520 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100521 shell->client = s;
522 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400523 "binding-modifier", &s, "super");
524 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200525 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800526
527 weston_config_section_get_string(section,
528 "exposay-modifier", &s, "none");
529 if (strcmp(s, "none") == 0)
530 shell->exposay_modifier = 0;
531 else
532 shell->exposay_modifier = get_modifier(s);
533 free(s);
534
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400535 weston_config_section_get_string(section, "animation", &s, "none");
536 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200537 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700538 weston_config_section_get_string(section,
539 "startup-animation", &s, "fade");
540 shell->startup_animation_type = get_animation_type(s);
541 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700542 if (shell->startup_animation_type == ANIMATION_ZOOM)
543 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100544 weston_config_section_get_string(section, "focus-animation", &s, "none");
545 shell->focus_animation_type = get_animation_type(s);
546 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400547 weston_config_section_get_uint(section, "num-workspaces",
548 &shell->workspaces.num,
549 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200550}
551
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800552struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100553get_default_output(struct weston_compositor *compositor)
554{
555 return container_of(compositor->output_list.next,
556 struct weston_output, link);
557}
558
559
560/* no-op func for checking focus surface */
561static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600562focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100563{
564}
565
566static struct focus_surface *
567get_focus_surface(struct weston_surface *surface)
568{
569 if (surface->configure == focus_surface_configure)
570 return surface->configure_private;
571 else
572 return NULL;
573}
574
575static bool
576is_focus_surface (struct weston_surface *es)
577{
578 return (es->configure == focus_surface_configure);
579}
580
581static bool
582is_focus_view (struct weston_view *view)
583{
584 return is_focus_surface (view->surface);
585}
586
587static struct focus_surface *
588create_focus_surface(struct weston_compositor *ec,
589 struct weston_output *output)
590{
591 struct focus_surface *fsurf = NULL;
592 struct weston_surface *surface = NULL;
593
594 fsurf = malloc(sizeof *fsurf);
595 if (!fsurf)
596 return NULL;
597
598 fsurf->surface = weston_surface_create(ec);
599 surface = fsurf->surface;
600 if (surface == NULL) {
601 free(fsurf);
602 return NULL;
603 }
604
605 surface->configure = focus_surface_configure;
606 surface->output = output;
607 surface->configure_private = fsurf;
608
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800609 fsurf->view = weston_view_create(surface);
610 if (fsurf->view == NULL) {
611 weston_surface_destroy(surface);
612 free(fsurf);
613 return NULL;
614 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100615 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100616
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600617 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600618 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100619 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
620 pixman_region32_fini(&surface->opaque);
621 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
622 output->width, output->height);
623 pixman_region32_fini(&surface->input);
624 pixman_region32_init(&surface->input);
625
626 wl_list_init(&fsurf->workspace_transform.link);
627
628 return fsurf;
629}
630
631static void
632focus_surface_destroy(struct focus_surface *fsurf)
633{
634 weston_surface_destroy(fsurf->surface);
635 free(fsurf);
636}
637
638static void
639focus_animation_done(struct weston_view_animation *animation, void *data)
640{
641 struct workspace *ws = data;
642
643 ws->focus_animation = NULL;
644}
645
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200646static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200647focus_state_destroy(struct focus_state *state)
648{
649 wl_list_remove(&state->seat_destroy_listener.link);
650 wl_list_remove(&state->surface_destroy_listener.link);
651 free(state);
652}
653
654static void
655focus_state_seat_destroy(struct wl_listener *listener, void *data)
656{
657 struct focus_state *state = container_of(listener,
658 struct focus_state,
659 seat_destroy_listener);
660
661 wl_list_remove(&state->link);
662 focus_state_destroy(state);
663}
664
665static void
666focus_state_surface_destroy(struct wl_listener *listener, void *data)
667{
668 struct focus_state *state = container_of(listener,
669 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400670 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400671 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500672 struct weston_surface *main_surface, *next;
673 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200674
Pekka Paalanen01388e22013-04-25 13:57:44 +0300675 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
676
Kristian Høgsberge3778222012-07-31 17:29:30 -0400677 next = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500678 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
679 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400680 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100681 if (is_focus_view(view))
682 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400683
Jason Ekstranda7af7042013-10-12 22:38:11 -0500684 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400685 break;
686 }
687
Pekka Paalanen01388e22013-04-25 13:57:44 +0300688 /* if the focus was a sub-surface, activate its main surface */
689 if (main_surface != state->keyboard_focus)
690 next = main_surface;
691
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100692 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400693 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100694 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100695 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400696 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100697 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
698 if (state->ws->focus_animation)
699 weston_view_animation_destroy(state->ws->focus_animation);
700
701 state->ws->focus_animation = weston_fade_run(
702 state->ws->fsurf_front->view,
703 state->ws->fsurf_front->view->alpha, 0.0, 300,
704 focus_animation_done, state->ws);
705 }
706
Kristian Høgsberge3778222012-07-31 17:29:30 -0400707 wl_list_remove(&state->link);
708 focus_state_destroy(state);
709 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200710}
711
712static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400713focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200714{
Jonas Ådahl04769742012-06-13 00:01:24 +0200715 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200716
717 state = malloc(sizeof *state);
718 if (state == NULL)
719 return NULL;
720
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100721 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400722 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200723 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400724 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200725
726 state->seat_destroy_listener.notify = focus_state_seat_destroy;
727 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400728 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200729 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400730 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200731
732 return state;
733}
734
Jonas Ådahl8538b222012-08-29 22:13:03 +0200735static struct focus_state *
736ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
737{
738 struct workspace *ws = get_current_workspace(shell);
739 struct focus_state *state;
740
741 wl_list_for_each(state, &ws->focus_list, link)
742 if (state->seat == seat)
743 break;
744
745 if (&state->link == &ws->focus_list)
746 state = focus_state_create(seat, ws);
747
748 return state;
749}
750
Jonas Ådahl04769742012-06-13 00:01:24 +0200751static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800752focus_state_set_focus(struct focus_state *state,
753 struct weston_surface *surface)
754{
755 if (state->keyboard_focus) {
756 wl_list_remove(&state->surface_destroy_listener.link);
757 wl_list_init(&state->surface_destroy_listener.link);
758 }
759
760 state->keyboard_focus = surface;
761 if (surface)
762 wl_signal_add(&surface->destroy_signal,
763 &state->surface_destroy_listener);
764}
765
766static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400767restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200768{
769 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400770 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100771 struct wl_list pending_seat_list;
772 struct weston_seat *seat, *next_seat;
773
774 /* Temporarily steal the list of seats so that we can keep
775 * track of the seats we've already processed */
776 wl_list_init(&pending_seat_list);
777 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
778 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200779
780 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100781 wl_list_remove(&state->seat->link);
782 wl_list_insert(&shell->compositor->seat_list,
783 &state->seat->link);
784
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800785 if (state->seat->keyboard == NULL)
786 continue;
787
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400788 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200789
Kristian Høgsberge3148752013-05-06 23:19:49 -0400790 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200791 }
Neil Roberts4237f502014-04-09 16:33:31 +0100792
793 /* For any remaining seats that we don't have a focus state
794 * for we'll reset the keyboard focus to NULL */
795 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
796 wl_list_insert(&shell->compositor->seat_list, &seat->link);
797
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300798 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100799 continue;
800
801 weston_keyboard_set_focus(seat->keyboard, NULL);
802 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200803}
804
805static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200806replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
807 struct weston_seat *seat)
808{
809 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200810
811 wl_list_for_each(state, &ws->focus_list, link) {
812 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800813 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200814 return;
815 }
816 }
817}
818
819static void
820drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
821 struct weston_surface *surface)
822{
823 struct focus_state *state;
824
825 wl_list_for_each(state, &ws->focus_list, link)
826 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800827 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200828}
829
830static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100831animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
832 struct weston_view *from, struct weston_view *to)
833{
834 struct weston_output *output;
835 bool focus_surface_created = false;
836
837 /* FIXME: Only support dim animation using two layers */
838 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
839 return;
840
841 output = get_default_output(shell->compositor);
842 if (ws->fsurf_front == NULL && (from || to)) {
843 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800844 if (ws->fsurf_front == NULL)
845 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100846 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800847
848 ws->fsurf_back = create_focus_surface(shell->compositor, output);
849 if (ws->fsurf_back == NULL) {
850 focus_surface_destroy(ws->fsurf_front);
851 return;
852 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100853 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800854
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100855 focus_surface_created = true;
856 } else {
857 wl_list_remove(&ws->fsurf_front->view->layer_link);
858 wl_list_remove(&ws->fsurf_back->view->layer_link);
859 }
860
861 if (ws->focus_animation) {
862 weston_view_animation_destroy(ws->focus_animation);
863 ws->focus_animation = NULL;
864 }
865
866 if (to)
867 wl_list_insert(&to->layer_link,
868 &ws->fsurf_front->view->layer_link);
869 else if (from)
870 wl_list_insert(&ws->layer.view_list,
871 &ws->fsurf_front->view->layer_link);
872
873 if (focus_surface_created) {
874 ws->focus_animation = weston_fade_run(
875 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200876 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100877 focus_animation_done, ws);
878 } else if (from) {
879 wl_list_insert(&from->layer_link,
880 &ws->fsurf_back->view->layer_link);
881 ws->focus_animation = weston_stable_fade_run(
882 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200883 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100884 focus_animation_done, ws);
885 } else if (to) {
886 wl_list_insert(&ws->layer.view_list,
887 &ws->fsurf_back->view->layer_link);
888 ws->focus_animation = weston_stable_fade_run(
889 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200890 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100891 focus_animation_done, ws);
892 }
893}
894
895static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200896workspace_destroy(struct workspace *ws)
897{
Jonas Ådahl04769742012-06-13 00:01:24 +0200898 struct focus_state *state, *next;
899
900 wl_list_for_each_safe(state, next, &ws->focus_list, link)
901 focus_state_destroy(state);
902
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100903 if (ws->fsurf_front)
904 focus_surface_destroy(ws->fsurf_front);
905 if (ws->fsurf_back)
906 focus_surface_destroy(ws->fsurf_back);
907
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200908 free(ws);
909}
910
Jonas Ådahl04769742012-06-13 00:01:24 +0200911static void
912seat_destroyed(struct wl_listener *listener, void *data)
913{
914 struct weston_seat *seat = data;
915 struct focus_state *state, *next;
916 struct workspace *ws = container_of(listener,
917 struct workspace,
918 seat_destroyed_listener);
919
920 wl_list_for_each_safe(state, next, &ws->focus_list, link)
921 if (state->seat == seat)
922 wl_list_remove(&state->link);
923}
924
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200925static struct workspace *
926workspace_create(void)
927{
928 struct workspace *ws = malloc(sizeof *ws);
929 if (ws == NULL)
930 return NULL;
931
932 weston_layer_init(&ws->layer, NULL);
933
Jonas Ådahl04769742012-06-13 00:01:24 +0200934 wl_list_init(&ws->focus_list);
935 wl_list_init(&ws->seat_destroyed_listener.link);
936 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100937 ws->fsurf_front = NULL;
938 ws->fsurf_back = NULL;
939 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200940
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200941 return ws;
942}
943
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200944static int
945workspace_is_empty(struct workspace *ws)
946{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500947 return wl_list_empty(&ws->layer.view_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200948}
949
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200950static struct workspace *
951get_workspace(struct desktop_shell *shell, unsigned int index)
952{
953 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200954 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200955 pws += index;
956 return *pws;
957}
958
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800959struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200960get_current_workspace(struct desktop_shell *shell)
961{
962 return get_workspace(shell, shell->workspaces.current);
963}
964
965static void
966activate_workspace(struct desktop_shell *shell, unsigned int index)
967{
968 struct workspace *ws;
969
970 ws = get_workspace(shell, index);
971 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
972
973 shell->workspaces.current = index;
974}
975
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200976static unsigned int
977get_output_height(struct weston_output *output)
978{
979 return abs(output->region.extents.y1 - output->region.extents.y2);
980}
981
982static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100983view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200984{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200985 struct weston_transform *transform;
986
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100987 if (is_focus_view(view)) {
988 struct focus_surface *fsurf = get_focus_surface(view->surface);
989 transform = &fsurf->workspace_transform;
990 } else {
991 struct shell_surface *shsurf = get_shell_surface(view->surface);
992 transform = &shsurf->workspace_transform;
993 }
994
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200995 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500996 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100997 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200998
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100999 weston_matrix_init(&transform->matrix);
1000 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001001 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001002 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001003}
1004
1005static void
1006workspace_translate_out(struct workspace *ws, double fraction)
1007{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001009 unsigned int height;
1010 double d;
1011
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
1013 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001014 d = height * fraction;
1015
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001016 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001017 }
1018}
1019
1020static void
1021workspace_translate_in(struct workspace *ws, double fraction)
1022{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001024 unsigned int height;
1025 double d;
1026
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
1028 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001029
1030 if (fraction > 0)
1031 d = -(height - height * fraction);
1032 else
1033 d = height + height * fraction;
1034
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001035 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001036 }
1037}
1038
1039static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001040broadcast_current_workspace_state(struct desktop_shell *shell)
1041{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001042 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001043
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001044 wl_resource_for_each(resource, &shell->workspaces.client_list)
1045 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001046 shell->workspaces.current,
1047 shell->workspaces.num);
1048}
1049
1050static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001051reverse_workspace_change_animation(struct desktop_shell *shell,
1052 unsigned int index,
1053 struct workspace *from,
1054 struct workspace *to)
1055{
1056 shell->workspaces.current = index;
1057
1058 shell->workspaces.anim_to = to;
1059 shell->workspaces.anim_from = from;
1060 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1061 shell->workspaces.anim_timestamp = 0;
1062
Scott Moreau4272e632012-08-13 09:58:41 -06001063 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001064}
1065
1066static void
1067workspace_deactivate_transforms(struct workspace *ws)
1068{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001069 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001070 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001071
Jason Ekstranda7af7042013-10-12 22:38:11 -05001072 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001073 if (is_focus_view(view)) {
1074 struct focus_surface *fsurf = get_focus_surface(view->surface);
1075 transform = &fsurf->workspace_transform;
1076 } else {
1077 struct shell_surface *shsurf = get_shell_surface(view->surface);
1078 transform = &shsurf->workspace_transform;
1079 }
1080
1081 if (!wl_list_empty(&transform->link)) {
1082 wl_list_remove(&transform->link);
1083 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001084 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001085 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001086 }
1087}
1088
1089static void
1090finish_workspace_change_animation(struct desktop_shell *shell,
1091 struct workspace *from,
1092 struct workspace *to)
1093{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001094 struct weston_view *view;
1095
Scott Moreau4272e632012-08-13 09:58:41 -06001096 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001097
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001098 /* Views that extend past the bottom of the output are still
1099 * visible after the workspace animation ends but before its layer
1100 * is hidden. In that case, we need to damage below those views so
1101 * that the screen is properly repainted. */
1102 wl_list_for_each(view, &from->layer.view_list, layer_link)
1103 weston_view_damage_below(view);
1104
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001105 wl_list_remove(&shell->workspaces.animation.link);
1106 workspace_deactivate_transforms(from);
1107 workspace_deactivate_transforms(to);
1108 shell->workspaces.anim_to = NULL;
1109
1110 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1111}
1112
1113static void
1114animate_workspace_change_frame(struct weston_animation *animation,
1115 struct weston_output *output, uint32_t msecs)
1116{
1117 struct desktop_shell *shell =
1118 container_of(animation, struct desktop_shell,
1119 workspaces.animation);
1120 struct workspace *from = shell->workspaces.anim_from;
1121 struct workspace *to = shell->workspaces.anim_to;
1122 uint32_t t;
1123 double x, y;
1124
1125 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1126 finish_workspace_change_animation(shell, from, to);
1127 return;
1128 }
1129
1130 if (shell->workspaces.anim_timestamp == 0) {
1131 if (shell->workspaces.anim_current == 0.0)
1132 shell->workspaces.anim_timestamp = msecs;
1133 else
1134 shell->workspaces.anim_timestamp =
1135 msecs -
1136 /* Invers of movement function 'y' below. */
1137 (asin(1.0 - shell->workspaces.anim_current) *
1138 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1139 M_2_PI);
1140 }
1141
1142 t = msecs - shell->workspaces.anim_timestamp;
1143
1144 /*
1145 * x = [0, π/2]
1146 * y(x) = sin(x)
1147 */
1148 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1149 y = sin(x);
1150
1151 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001152 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001153
1154 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1155 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1156 shell->workspaces.anim_current = y;
1157
Scott Moreau4272e632012-08-13 09:58:41 -06001158 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001159 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001160 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001161 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001162}
1163
1164static void
1165animate_workspace_change(struct desktop_shell *shell,
1166 unsigned int index,
1167 struct workspace *from,
1168 struct workspace *to)
1169{
1170 struct weston_output *output;
1171
1172 int dir;
1173
1174 if (index > shell->workspaces.current)
1175 dir = -1;
1176 else
1177 dir = 1;
1178
1179 shell->workspaces.current = index;
1180
1181 shell->workspaces.anim_dir = dir;
1182 shell->workspaces.anim_from = from;
1183 shell->workspaces.anim_to = to;
1184 shell->workspaces.anim_current = 0.0;
1185 shell->workspaces.anim_timestamp = 0;
1186
1187 output = container_of(shell->compositor->output_list.next,
1188 struct weston_output, link);
1189 wl_list_insert(&output->animation_list,
1190 &shell->workspaces.animation.link);
1191
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001192 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001193
1194 workspace_translate_in(to, 0);
1195
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001196 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001197
Scott Moreau4272e632012-08-13 09:58:41 -06001198 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001199}
1200
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001201static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001202update_workspace(struct desktop_shell *shell, unsigned int index,
1203 struct workspace *from, struct workspace *to)
1204{
1205 shell->workspaces.current = index;
1206 wl_list_insert(&from->layer.link, &to->layer.link);
1207 wl_list_remove(&from->layer.link);
1208}
1209
1210static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001211change_workspace(struct desktop_shell *shell, unsigned int index)
1212{
1213 struct workspace *from;
1214 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001215 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001216
1217 if (index == shell->workspaces.current)
1218 return;
1219
1220 /* Don't change workspace when there is any fullscreen surfaces. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001221 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001222 return;
1223
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001224 from = get_current_workspace(shell);
1225 to = get_workspace(shell, index);
1226
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001227 if (shell->workspaces.anim_from == to &&
1228 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001229 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001230 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001231 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001232 return;
1233 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001234
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001235 if (shell->workspaces.anim_to != NULL)
1236 finish_workspace_change_animation(shell,
1237 shell->workspaces.anim_from,
1238 shell->workspaces.anim_to);
1239
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001240 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001241
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001242 if (shell->focus_animation_type != ANIMATION_NONE) {
1243 wl_list_for_each(state, &from->focus_list, link)
1244 if (state->keyboard_focus)
1245 animate_focus_change(shell, from,
1246 get_default_view(state->keyboard_focus), NULL);
1247
1248 wl_list_for_each(state, &to->focus_list, link)
1249 if (state->keyboard_focus)
1250 animate_focus_change(shell, to,
1251 NULL, get_default_view(state->keyboard_focus));
1252 }
1253
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001254 if (workspace_is_empty(to) && workspace_is_empty(from))
1255 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001256 else
1257 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001258
1259 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001260}
1261
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001262static bool
1263workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1264{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 struct wl_list *list = &ws->layer.view_list;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001266 struct wl_list *e;
1267
1268 if (wl_list_empty(list))
1269 return false;
1270
1271 e = list->next;
1272
1273 if (e->next != list)
1274 return false;
1275
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 return container_of(e, struct weston_view, layer_link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001277}
1278
1279static void
Philip Withnall659163d2013-11-25 18:01:36 +00001280move_surface_to_workspace(struct desktop_shell *shell,
1281 struct shell_surface *shsurf,
1282 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001283{
1284 struct workspace *from;
1285 struct workspace *to;
1286 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001287 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001288 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001289
1290 if (workspace == shell->workspaces.current)
1291 return;
1292
Philip Withnall659163d2013-11-25 18:01:36 +00001293 view = get_default_view(shsurf->surface);
1294 if (!view)
1295 return;
1296
1297 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1298
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001299 if (workspace >= shell->workspaces.num)
1300 workspace = shell->workspaces.num - 1;
1301
Jonas Ådahle9d22502012-08-29 22:13:01 +02001302 from = get_current_workspace(shell);
1303 to = get_workspace(shell, workspace);
1304
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 wl_list_remove(&view->layer_link);
1306 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001307
Philip Withnall648a4dd2013-11-25 18:01:44 +00001308 shell_surface_update_child_surface_layers(shsurf);
1309
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001311 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1312 if (!seat->keyboard)
1313 continue;
1314
1315 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001317 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001318 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001319
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001321}
1322
1323static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001324take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001325 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001326 unsigned int index)
1327{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001328 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001329 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001330 struct shell_surface *shsurf;
1331 struct workspace *from;
1332 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001333 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001334
Pekka Paalanen01388e22013-04-25 13:57:44 +03001335 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336 view = get_default_view(surface);
1337 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001338 index == shell->workspaces.current ||
1339 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001340 return;
1341
1342 from = get_current_workspace(shell);
1343 to = get_workspace(shell, index);
1344
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345 wl_list_remove(&view->layer_link);
1346 wl_list_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001347
Philip Withnall659163d2013-11-25 18:01:36 +00001348 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001349 if (shsurf != NULL)
1350 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001351
Jonas Ådahle9d22502012-08-29 22:13:01 +02001352 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001353 drop_focus_state(shell, from, surface);
1354
1355 if (shell->workspaces.anim_from == to &&
1356 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001357 wl_list_remove(&to->layer.link);
1358 wl_list_insert(from->layer.link.prev, &to->layer.link);
1359
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001360 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001361 broadcast_current_workspace_state(shell);
1362
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001363 return;
1364 }
1365
1366 if (shell->workspaces.anim_to != NULL)
1367 finish_workspace_change_animation(shell,
1368 shell->workspaces.anim_from,
1369 shell->workspaces.anim_to);
1370
1371 if (workspace_is_empty(from) &&
1372 workspace_has_only(to, surface))
1373 update_workspace(shell, index, from, to);
1374 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001375 if (shsurf != NULL &&
1376 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001377 wl_list_insert(&shell->workspaces.anim_sticky_list,
1378 &shsurf->workspace_transform.link);
1379
1380 animate_workspace_change(shell, index, from, to);
1381 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001382
1383 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001384
1385 state = ensure_focus_state(shell, seat);
1386 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001387 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001388}
1389
1390static void
1391workspace_manager_move_surface(struct wl_client *client,
1392 struct wl_resource *resource,
1393 struct wl_resource *surface_resource,
1394 uint32_t workspace)
1395{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001396 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001397 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001398 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001399 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001400 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001401
Pekka Paalanen01388e22013-04-25 13:57:44 +03001402 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001403 shell_surface = get_shell_surface(main_surface);
1404 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001406
1407 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001408}
1409
1410static const struct workspace_manager_interface workspace_manager_implementation = {
1411 workspace_manager_move_surface,
1412};
1413
1414static void
1415unbind_resource(struct wl_resource *resource)
1416{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001417 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001418}
1419
1420static void
1421bind_workspace_manager(struct wl_client *client,
1422 void *data, uint32_t version, uint32_t id)
1423{
1424 struct desktop_shell *shell = data;
1425 struct wl_resource *resource;
1426
Jason Ekstranda85118c2013-06-27 20:17:02 -05001427 resource = wl_resource_create(client,
1428 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001429
1430 if (resource == NULL) {
1431 weston_log("couldn't add workspace manager object");
1432 return;
1433 }
1434
Jason Ekstranda85118c2013-06-27 20:17:02 -05001435 wl_resource_set_implementation(resource,
1436 &workspace_manager_implementation,
1437 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001438 wl_list_insert(&shell->workspaces.client_list,
1439 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001440
1441 workspace_manager_send_state(resource,
1442 shell->workspaces.current,
1443 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001444}
1445
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001446static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001447touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1448 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1449{
1450}
1451
1452static void
1453touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1454{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001455 struct weston_touch_move_grab *move =
1456 (struct weston_touch_move_grab *) container_of(
1457 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001458
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001459 if (touch_id == 0)
1460 move->active = 0;
1461
Jonas Ådahl9484b692013-12-02 22:05:03 +01001462 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001463 shell_touch_grab_end(&move->base);
1464 free(move);
1465 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001466}
1467
1468static void
1469touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1470 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1471{
1472 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1473 struct shell_surface *shsurf = move->base.shsurf;
1474 struct weston_surface *es;
1475 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1476 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1477
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001478 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001479 return;
1480
1481 es = shsurf->surface;
1482
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001483 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001484
1485 weston_compositor_schedule_repaint(es->compositor);
1486}
1487
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001488static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001489touch_move_grab_frame(struct weston_touch_grab *grab)
1490{
1491}
1492
1493static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001494touch_move_grab_cancel(struct weston_touch_grab *grab)
1495{
1496 struct weston_touch_move_grab *move =
1497 (struct weston_touch_move_grab *) container_of(
1498 grab, struct shell_touch_grab, grab);
1499
1500 shell_touch_grab_end(&move->base);
1501 free(move);
1502}
1503
Rusty Lynch1084da52013-08-15 09:10:08 -07001504static const struct weston_touch_grab_interface touch_move_grab_interface = {
1505 touch_move_grab_down,
1506 touch_move_grab_up,
1507 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001508 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001509 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001510};
1511
1512static int
1513surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1514{
1515 struct weston_touch_move_grab *move;
1516
1517 if (!shsurf)
1518 return -1;
1519
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001520 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001521 return 0;
1522
1523 move = malloc(sizeof *move);
1524 if (!move)
1525 return -1;
1526
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001527 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001529 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001530 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001531 seat->touch->grab_y;
1532
1533 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1534 seat->touch);
1535
1536 return 0;
1537}
1538
1539static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001540noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001541{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001542}
1543
1544static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001545constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1546{
1547 struct shell_surface *shsurf = move->base.shsurf;
1548 struct weston_pointer *pointer = move->base.grab.pointer;
Kristian Høgsberg3434b332014-04-29 16:38:23 -07001549 int x, y, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001550 const int safety = 50;
1551
1552 x = wl_fixed_to_int(pointer->x + move->dx);
1553 y = wl_fixed_to_int(pointer->y + move->dy);
1554
1555 panel_height = get_output_panel_height(shsurf->shell,
1556 shsurf->surface->output);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001557 bottom = y + shsurf->geometry.height;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001558 if (bottom - panel_height < safety)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001559 y = panel_height + safety - shsurf->geometry.height;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001560
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001561 if (move->client_initiated && y + shsurf->geometry.y < panel_height)
1562 y = panel_height - shsurf->geometry.y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001563
1564 *cx = x;
1565 *cy = y;
1566}
1567
1568static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001569move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1570 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001571{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001572 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001573 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001574 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001575 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001576
1577 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001578 if (!shsurf)
1579 return;
1580
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001581 constrain_position(move, &cx, &cy);
1582
1583 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001584
Jason Ekstranda7af7042013-10-12 22:38:11 -05001585 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001586}
1587
1588static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001589move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001590 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001591{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001592 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1593 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001594 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001595 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001596
Daniel Stone4dbadb12012-05-30 16:31:51 +01001597 if (pointer->button_count == 0 &&
1598 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001599 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001600 free(grab);
1601 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001602}
1603
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001604static void
1605move_grab_cancel(struct weston_pointer_grab *grab)
1606{
1607 struct shell_grab *shell_grab =
1608 container_of(grab, struct shell_grab, grab);
1609
1610 shell_grab_end(shell_grab);
1611 free(grab);
1612}
1613
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001614static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001615 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001616 move_grab_motion,
1617 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001618 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001619};
1620
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001621static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001622surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1623 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001624{
1625 struct weston_move_grab *move;
1626
1627 if (!shsurf)
1628 return -1;
1629
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001630 if (shsurf->grabbed ||
1631 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001632 return 0;
1633
1634 move = malloc(sizeof *move);
1635 if (!move)
1636 return -1;
1637
Jason Ekstranda7af7042013-10-12 22:38:11 -05001638 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001639 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001640 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001641 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001642 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001643
1644 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001645 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001646
1647 return 0;
1648}
1649
1650static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001651common_surface_move(struct wl_resource *resource,
1652 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001653{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001654 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001655 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001656 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001657
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001658 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001659 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001660 seat->pointer->button_count > 0 &&
1661 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001662 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001663 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001664 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001665 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001666 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001667 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001668 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001669 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001670 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001671 (surface_touch_move(shsurf, seat) < 0))
1672 wl_resource_post_no_memory(resource);
1673 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001674}
1675
Rafael Antognollie2a34552013-12-03 15:35:45 -02001676static void
1677shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1678 struct wl_resource *seat_resource, uint32_t serial)
1679{
1680 common_surface_move(resource, seat_resource, serial);
1681}
1682
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001683struct weston_resize_grab {
1684 struct shell_grab base;
1685 uint32_t edges;
1686 int32_t width, height;
1687};
1688
1689static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001690resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1691 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001692{
1693 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001694 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001695 struct shell_surface *shsurf = resize->base.shsurf;
1696 int32_t width, height;
1697 wl_fixed_t from_x, from_y;
1698 wl_fixed_t to_x, to_y;
1699
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001700 weston_pointer_move(pointer, x, y);
1701
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001702 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001703 return;
1704
Jason Ekstranda7af7042013-10-12 22:38:11 -05001705 weston_view_from_global_fixed(shsurf->view,
1706 pointer->grab_x, pointer->grab_y,
1707 &from_x, &from_y);
1708 weston_view_from_global_fixed(shsurf->view,
1709 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001710
1711 width = resize->width;
1712 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1713 width += wl_fixed_to_int(from_x - to_x);
1714 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1715 width += wl_fixed_to_int(to_x - from_x);
1716 }
1717
1718 height = resize->height;
1719 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1720 height += wl_fixed_to_int(from_y - to_y);
1721 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1722 height += wl_fixed_to_int(to_y - from_y);
1723 }
1724
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001725 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001726}
1727
1728static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001729send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001730{
1731 struct shell_surface *shsurf = get_shell_surface(surface);
1732
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001733 assert(shsurf);
1734
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001735 if (shsurf->resource)
1736 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001737 shsurf->resize_edges,
1738 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001739}
1740
1741static const struct weston_shell_client shell_client = {
1742 send_configure
1743};
1744
1745static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001746resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001747 uint32_t time, uint32_t button, uint32_t state_w)
1748{
1749 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001750 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001751 enum wl_pointer_button_state state = state_w;
1752
1753 if (pointer->button_count == 0 &&
1754 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1755 shell_grab_end(&resize->base);
1756 free(grab);
1757 }
1758}
1759
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001760static void
1761resize_grab_cancel(struct weston_pointer_grab *grab)
1762{
1763 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1764
1765 shell_grab_end(&resize->base);
1766 free(grab);
1767}
1768
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001769static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001770 noop_grab_focus,
1771 resize_grab_motion,
1772 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001773 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001774};
1775
Giulio Camuffob8366642013-04-25 13:57:46 +03001776/*
1777 * Returns the bounding box of a surface and all its sub-surfaces,
1778 * in the surface coordinates system. */
1779static void
1780surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1781 int32_t *y, int32_t *w, int32_t *h) {
1782 pixman_region32_t region;
1783 pixman_box32_t *box;
1784 struct weston_subsurface *subsurface;
1785
1786 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001787 surface->width,
1788 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001789
1790 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1791 pixman_region32_union_rect(&region, &region,
1792 subsurface->position.x,
1793 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001794 subsurface->surface->width,
1795 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001796 }
1797
1798 box = pixman_region32_extents(&region);
1799 if (x)
1800 *x = box->x1;
1801 if (y)
1802 *y = box->y1;
1803 if (w)
1804 *w = box->x2 - box->x1;
1805 if (h)
1806 *h = box->y2 - box->y1;
1807
1808 pixman_region32_fini(&region);
1809}
1810
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001811static int
1812surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001813 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001814{
1815 struct weston_resize_grab *resize;
1816
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001817 if (shsurf->grabbed ||
1818 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001819 return 0;
1820
1821 if (edges == 0 || edges > 15 ||
1822 (edges & 3) == 3 || (edges & 12) == 12)
1823 return 0;
1824
1825 resize = malloc(sizeof *resize);
1826 if (!resize)
1827 return -1;
1828
1829 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001830
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001831 resize->width = shsurf->geometry.width;
1832 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001833
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001834 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001835 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001836 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001837 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001838
1839 return 0;
1840}
1841
1842static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001843common_surface_resize(struct wl_resource *resource,
1844 struct wl_resource *seat_resource, uint32_t serial,
1845 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001846{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001847 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001848 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001849 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001850
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001851 if (seat->pointer == NULL ||
1852 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001853 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001854 seat->pointer->focus == NULL)
1855 return;
1856
1857 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1858 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001859 return;
1860
Kristian Høgsberge3148752013-05-06 23:19:49 -04001861 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001862 wl_resource_post_no_memory(resource);
1863}
1864
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001865static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001866shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1867 struct wl_resource *seat_resource, uint32_t serial,
1868 uint32_t edges)
1869{
1870 common_surface_resize(resource, seat_resource, serial, edges);
1871}
1872
1873static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001874busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001875{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001876 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001877 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001878 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001879 wl_fixed_t sx, sy;
1880
Jason Ekstranda7af7042013-10-12 22:38:11 -05001881 view = weston_compositor_pick_view(pointer->seat->compositor,
1882 pointer->x, pointer->y,
1883 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001884
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001885 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1886 shell_grab_end(grab);
1887 free(grab);
1888 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001889}
1890
1891static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001892busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1893 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001894{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001895 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001896}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001897
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001898static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001899busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001900 uint32_t time, uint32_t button, uint32_t state)
1901{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001902 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001903 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001904 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001905
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001906 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001907 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001908 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001909 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001910 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001911 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001912 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001913}
1914
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001915static void
1916busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1917{
1918 struct shell_grab *grab = (struct shell_grab *) base;
1919
1920 shell_grab_end(grab);
1921 free(grab);
1922}
1923
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001924static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001925 busy_cursor_grab_focus,
1926 busy_cursor_grab_motion,
1927 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001928 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001929};
1930
1931static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001932set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001933{
1934 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001935
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001936 if (pointer->grab->interface == &busy_cursor_grab_interface)
1937 return;
1938
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001939 grab = malloc(sizeof *grab);
1940 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001941 return;
1942
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001943 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1944 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03001945 /* Mark the shsurf as ungrabbed so that button binding is able
1946 * to move it. */
1947 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001948}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001949
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001950static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001951end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001952{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001953 struct shell_grab *grab;
1954 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001955
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001956 wl_list_for_each(seat, &compositor->seat_list, link) {
1957 if (seat->pointer == NULL)
1958 continue;
1959
1960 grab = (struct shell_grab *) seat->pointer->grab;
1961 if (grab->grab.interface == &busy_cursor_grab_interface &&
1962 wl_resource_get_client(grab->shsurf->resource) == client) {
1963 shell_grab_end(grab);
1964 free(grab);
1965 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001966 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001967}
1968
Scott Moreau9521d5e2012-04-19 13:06:17 -06001969static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001970handle_shell_client_destroy(struct wl_listener *listener, void *data);
1971
1972static int
1973xdg_ping_timeout_handler(void *data)
1974{
1975 struct shell_client *sc = data;
1976 struct weston_seat *seat;
1977 struct shell_surface *shsurf;
1978
1979 /* Client is not responding */
1980 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001981 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1982 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1983 continue;
1984 if (seat->pointer->focus->surface->resource == NULL)
1985 continue;
1986
1987 shsurf = get_shell_surface(seat->pointer->focus->surface);
1988 if (shsurf &&
1989 wl_resource_get_client(shsurf->resource) == sc->client)
1990 set_busy_cursor(shsurf, seat->pointer);
1991 }
1992
1993 return 1;
1994}
1995
1996static void
1997handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1998{
1999 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002000 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002001 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002002 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002003
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002004 if (sc->unresponsive) {
2005 xdg_ping_timeout_handler(sc);
2006 return;
2007 }
2008
2009 sc->ping_serial = serial;
2010 loop = wl_display_get_event_loop(compositor->wl_display);
2011 if (sc->ping_timer == NULL)
2012 sc->ping_timer =
2013 wl_event_loop_add_timer(loop,
2014 xdg_ping_timeout_handler, sc);
2015 if (sc->ping_timer == NULL)
2016 return;
2017
2018 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2019
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002020 if (shell_surface_is_xdg_surface(shsurf) ||
2021 shell_surface_is_xdg_popup(shsurf))
2022 xdg_shell_send_ping(sc->resource, serial);
2023 else if (shell_surface_is_wl_shell_surface(shsurf))
2024 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002025}
2026
Scott Moreauff1db4a2012-04-17 19:06:18 -06002027static void
2028ping_handler(struct weston_surface *surface, uint32_t serial)
2029{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002030 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002031
2032 if (!shsurf)
2033 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002034 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002035 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002036 if (shsurf->surface == shsurf->shell->grab_surface)
2037 return;
2038
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002039 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002040}
2041
2042static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002043handle_pointer_focus(struct wl_listener *listener, void *data)
2044{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002045 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002046 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002047 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002048 uint32_t serial;
2049
Jason Ekstranda7af7042013-10-12 22:38:11 -05002050 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002051 return;
2052
Jason Ekstranda7af7042013-10-12 22:38:11 -05002053 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002054 serial = wl_display_next_serial(compositor->wl_display);
2055 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002056}
2057
2058static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002059shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2060{
2061 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002062 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002063}
2064
2065static void
2066shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2067{
2068 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002069 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002070}
2071
2072static void
2073handle_keyboard_focus(struct wl_listener *listener, void *data)
2074{
2075 struct weston_keyboard *keyboard = data;
2076 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2077
2078 if (seat->focused_surface) {
2079 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2080 if (shsurf)
2081 shell_surface_lose_keyboard_focus(shsurf);
2082 }
2083
2084 seat->focused_surface = keyboard->focus;
2085
2086 if (seat->focused_surface) {
2087 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2088 if (shsurf)
2089 shell_surface_gain_keyboard_focus(shsurf);
2090 }
2091}
2092
2093static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002094shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002095{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002096 if (sc->ping_serial != serial)
2097 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002098
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002099 sc->unresponsive = 0;
2100 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002101
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002102 if (sc->ping_timer) {
2103 wl_event_source_remove(sc->ping_timer);
2104 sc->ping_timer = NULL;
2105 }
2106
2107}
2108
2109static void
2110shell_surface_pong(struct wl_client *client,
2111 struct wl_resource *resource, uint32_t serial)
2112{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002113 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2114 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002115
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002116 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002117}
2118
2119static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002120set_title(struct shell_surface *shsurf, const char *title)
2121{
2122 free(shsurf->title);
2123 shsurf->title = strdup(title);
2124}
2125
2126static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002127set_window_geometry(struct shell_surface *shsurf,
2128 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002129{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002130 shsurf->next_geometry.x = x;
2131 shsurf->next_geometry.y = y;
2132 shsurf->next_geometry.width = width;
2133 shsurf->next_geometry.height = height;
2134 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002135}
2136
2137static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002138shell_surface_set_title(struct wl_client *client,
2139 struct wl_resource *resource, const char *title)
2140{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002141 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002142
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002143 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002144}
2145
2146static void
2147shell_surface_set_class(struct wl_client *client,
2148 struct wl_resource *resource, const char *class)
2149{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002150 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002151
2152 free(shsurf->class);
2153 shsurf->class = strdup(class);
2154}
2155
Alex Wu4539b082012-03-01 12:57:46 +08002156static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002157restore_output_mode(struct weston_output *output)
2158{
Hardening57388e42013-09-18 23:56:36 +02002159 if (output->current_mode != output->original_mode ||
2160 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002161 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002162 output->original_mode,
2163 output->original_scale,
2164 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002165}
2166
2167static void
2168restore_all_output_modes(struct weston_compositor *compositor)
2169{
2170 struct weston_output *output;
2171
2172 wl_list_for_each(output, &compositor->output_list, link)
2173 restore_output_mode(output);
2174}
2175
Philip Withnall07926d92013-11-25 18:01:40 +00002176/* The surface will be inserted into the list immediately after the link
2177 * returned by this function (i.e. will be stacked immediately above the
2178 * returned link). */
2179static struct wl_list *
2180shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2181{
2182 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002183 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002184
2185 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002186 case SHELL_SURFACE_XWAYLAND:
2187 return &shsurf->shell->fullscreen_layer.view_list;
2188
2189 case SHELL_SURFACE_NONE:
2190 return NULL;
2191
Kristian Høgsbergead52422014-01-01 13:51:52 -08002192 case SHELL_SURFACE_POPUP:
2193 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002194 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002195 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002196 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002197 /* Move the surface to its parent layer so
2198 * that surfaces which are transient for
2199 * fullscreen surfaces don't get hidden by the
2200 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002201
2202 /* TODO: Handle a parent with multiple views */
2203 parent = get_default_view(shsurf->parent);
2204 if (parent)
2205 return parent->layer_link.prev;
2206 }
Philip Withnall07926d92013-11-25 18:01:40 +00002207
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002208 /* Move the surface to a normal workspace layer so that surfaces
2209 * which were previously fullscreen or transient are no longer
2210 * rendered on top. */
2211 ws = get_current_workspace(shsurf->shell);
2212 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002213 }
2214
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002215 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002216}
2217
Philip Withnall648a4dd2013-11-25 18:01:44 +00002218static void
2219shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2220{
2221 struct shell_surface *child;
2222
2223 /* Move the child layers to the same workspace as shsurf. They will be
2224 * stacked above shsurf. */
2225 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2226 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002227 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002228 weston_view_geometry_dirty(child->view);
2229 wl_list_remove(&child->view->layer_link);
2230 wl_list_insert(shsurf->view->layer_link.prev,
2231 &child->view->layer_link);
2232 weston_view_geometry_dirty(child->view);
2233 weston_surface_damage(child->surface);
2234
2235 /* Recurse. We don’t expect this to recurse very far (if
2236 * at all) because that would imply we have transient
2237 * (or popup) children of transient surfaces, which
2238 * would be unusual. */
2239 shell_surface_update_child_surface_layers(child);
2240 }
2241 }
2242}
2243
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002244/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002245 * geometry to ensure the changes are redrawn.
2246 *
2247 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2248 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002249static void
2250shell_surface_update_layer(struct shell_surface *shsurf)
2251{
2252 struct wl_list *new_layer_link;
2253
2254 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2255
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002256 if (new_layer_link == NULL)
2257 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002258 if (new_layer_link == &shsurf->view->layer_link)
2259 return;
2260
2261 weston_view_geometry_dirty(shsurf->view);
2262 wl_list_remove(&shsurf->view->layer_link);
2263 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2264 weston_view_geometry_dirty(shsurf->view);
2265 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002266
2267 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002268}
2269
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002270static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002271shell_surface_set_parent(struct shell_surface *shsurf,
2272 struct weston_surface *parent)
2273{
2274 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002275
2276 wl_list_remove(&shsurf->children_link);
2277 wl_list_init(&shsurf->children_link);
2278
2279 /* Insert into the parent surface’s child list. */
2280 if (parent != NULL) {
2281 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2282 if (parent_shsurf != NULL)
2283 wl_list_insert(&parent_shsurf->children_list,
2284 &shsurf->children_link);
2285 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002286}
2287
2288static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002289shell_surface_set_output(struct shell_surface *shsurf,
2290 struct weston_output *output)
2291{
2292 struct weston_surface *es = shsurf->surface;
2293
2294 /* get the default output, if the client set it as NULL
2295 check whether the ouput is available */
2296 if (output)
2297 shsurf->output = output;
2298 else if (es->output)
2299 shsurf->output = es->output;
2300 else
2301 shsurf->output = get_default_output(es->compositor);
2302}
2303
2304static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002305surface_clear_next_states(struct shell_surface *shsurf)
2306{
2307 shsurf->next_state.maximized = false;
2308 shsurf->next_state.fullscreen = false;
2309
2310 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2311 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2312 shsurf->state_changed = true;
2313}
2314
2315static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002316set_toplevel(struct shell_surface *shsurf)
2317{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002318 shell_surface_set_parent(shsurf, NULL);
2319 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002320 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002321
2322 /* The layer_link is updated in set_surface_type(),
2323 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002324}
2325
2326static void
2327shell_surface_set_toplevel(struct wl_client *client,
2328 struct wl_resource *resource)
2329{
2330 struct shell_surface *surface = wl_resource_get_user_data(resource);
2331
2332 set_toplevel(surface);
2333}
2334
2335static void
2336set_transient(struct shell_surface *shsurf,
2337 struct weston_surface *parent, int x, int y, uint32_t flags)
2338{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002339 assert(parent != NULL);
2340
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002341 shell_surface_set_parent(shsurf, parent);
2342
2343 surface_clear_next_states(shsurf);
2344
Philip Withnallbecb77e2013-11-25 18:01:30 +00002345 shsurf->transient.x = x;
2346 shsurf->transient.y = y;
2347 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002348
Rafael Antognollied207b42013-12-03 15:35:43 -02002349 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002350 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002351 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002352
2353 /* The layer_link is updated in set_surface_type(),
2354 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002355}
2356
2357static void
2358shell_surface_set_transient(struct wl_client *client,
2359 struct wl_resource *resource,
2360 struct wl_resource *parent_resource,
2361 int x, int y, uint32_t flags)
2362{
2363 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2364 struct weston_surface *parent =
2365 wl_resource_get_user_data(parent_resource);
2366
2367 set_transient(shsurf, parent, x, y, flags);
2368}
2369
2370static void
2371set_fullscreen(struct shell_surface *shsurf,
2372 uint32_t method,
2373 uint32_t framerate,
2374 struct weston_output *output)
2375{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002376 shell_surface_set_output(shsurf, output);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002377 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002378
2379 shsurf->fullscreen_output = shsurf->output;
2380 shsurf->fullscreen.type = method;
2381 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002382
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002383 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002384}
2385
2386static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002387weston_view_set_initial_position(struct weston_view *view,
2388 struct desktop_shell *shell);
2389
2390static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002391unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002392{
Philip Withnallf85fe842013-11-25 18:01:37 +00002393 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002394 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2395 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002396 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002397 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002398
Alex Wu4539b082012-03-01 12:57:46 +08002399 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2400 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002401
Alex Wu4539b082012-03-01 12:57:46 +08002402 wl_list_remove(&shsurf->fullscreen.transform.link);
2403 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002404
Jason Ekstranda7af7042013-10-12 22:38:11 -05002405 if (shsurf->fullscreen.black_view)
2406 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2407 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002408
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002409 if (shsurf->saved_position_valid)
2410 weston_view_set_position(shsurf->view,
2411 shsurf->saved_x, shsurf->saved_y);
2412 else
2413 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2414
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002415 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002416 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002417 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002418 shsurf->saved_rotation_valid = false;
2419 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002420
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002421 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002422}
2423
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002424static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002425shell_surface_set_fullscreen(struct wl_client *client,
2426 struct wl_resource *resource,
2427 uint32_t method,
2428 uint32_t framerate,
2429 struct wl_resource *output_resource)
2430{
2431 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2432 struct weston_output *output;
2433
2434 if (output_resource)
2435 output = wl_resource_get_user_data(output_resource);
2436 else
2437 output = NULL;
2438
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002439 shell_surface_set_parent(shsurf, NULL);
2440
Rafael Antognolli03b16592013-12-03 15:35:42 -02002441 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002442 shsurf->next_state.fullscreen = true;
2443 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002444 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002445}
2446
2447static void
2448set_popup(struct shell_surface *shsurf,
2449 struct weston_surface *parent,
2450 struct weston_seat *seat,
2451 uint32_t serial,
2452 int32_t x,
2453 int32_t y)
2454{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002455 assert(parent != NULL);
2456
Philip Withnallbecb77e2013-11-25 18:01:30 +00002457 shsurf->popup.shseat = get_shell_seat(seat);
2458 shsurf->popup.serial = serial;
2459 shsurf->popup.x = x;
2460 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002461
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002462 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002463}
2464
2465static void
2466shell_surface_set_popup(struct wl_client *client,
2467 struct wl_resource *resource,
2468 struct wl_resource *seat_resource,
2469 uint32_t serial,
2470 struct wl_resource *parent_resource,
2471 int32_t x, int32_t y, uint32_t flags)
2472{
2473 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002474 struct weston_surface *parent =
2475 wl_resource_get_user_data(parent_resource);
2476
2477 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002478
Rafael Antognolli03b16592013-12-03 15:35:42 -02002479 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002480 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002481 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002482 wl_resource_get_user_data(seat_resource),
2483 serial, x, y);
2484}
2485
2486static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002487unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002488{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002489 /* undo all maximized things here */
2490 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002491
2492 if (shsurf->saved_position_valid)
2493 weston_view_set_position(shsurf->view,
2494 shsurf->saved_x, shsurf->saved_y);
2495 else
2496 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002497
2498 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002499 wl_list_insert(&shsurf->view->geometry.transformation_list,
2500 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002501 shsurf->saved_rotation_valid = false;
2502 }
2503
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002504 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002505}
2506
Philip Withnallbecb77e2013-11-25 18:01:30 +00002507static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002508set_minimized(struct weston_surface *surface, uint32_t is_true)
2509{
2510 struct shell_surface *shsurf;
2511 struct workspace *current_ws;
2512 struct weston_seat *seat;
2513 struct weston_surface *focus;
2514 struct weston_view *view;
2515
2516 view = get_default_view(surface);
2517 if (!view)
2518 return;
2519
2520 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2521
2522 shsurf = get_shell_surface(surface);
2523 current_ws = get_current_workspace(shsurf->shell);
2524
2525 wl_list_remove(&view->layer_link);
2526 /* hide or show, depending on the state */
2527 if (is_true) {
2528 wl_list_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2529
2530 drop_focus_state(shsurf->shell, current_ws, view->surface);
2531 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2532 if (!seat->keyboard)
2533 continue;
2534 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2535 if (focus == view->surface)
2536 weston_keyboard_set_focus(seat->keyboard, NULL);
2537 }
2538 }
2539 else {
2540 wl_list_insert(&current_ws->layer.view_list, &view->layer_link);
2541
2542 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2543 if (!seat->keyboard)
2544 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002545 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002546 }
2547 }
2548
2549 shell_surface_update_child_surface_layers(shsurf);
2550
2551 weston_view_damage_below(view);
2552}
2553
2554static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002555shell_surface_set_maximized(struct wl_client *client,
2556 struct wl_resource *resource,
2557 struct wl_resource *output_resource)
2558{
2559 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2560 struct weston_output *output;
2561
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002562 surface_clear_next_states(shsurf);
2563 shsurf->next_state.maximized = true;
2564 shsurf->state_changed = true;
2565
2566 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2567 shell_surface_set_parent(shsurf, NULL);
2568
Philip Withnallbecb77e2013-11-25 18:01:30 +00002569 if (output_resource)
2570 output = wl_resource_get_user_data(output_resource);
2571 else
2572 output = NULL;
2573
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002574 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002575
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002576 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002577}
2578
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002579/* This is only ever called from set_surface_type(), so there’s no need to
2580 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002581static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002582reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002583{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002584 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002585 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002586 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002587 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002588
Pekka Paalanen98262232011-12-01 10:42:22 +02002589 return 0;
2590}
2591
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002592static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002593set_full_output(struct shell_surface *shsurf)
2594{
2595 shsurf->saved_x = shsurf->view->geometry.x;
2596 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002597 shsurf->saved_width = shsurf->surface->width;
2598 shsurf->saved_height = shsurf->surface->height;
2599 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002600 shsurf->saved_position_valid = true;
2601
2602 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2603 wl_list_remove(&shsurf->rotation.transform.link);
2604 wl_list_init(&shsurf->rotation.transform.link);
2605 weston_view_geometry_dirty(shsurf->view);
2606 shsurf->saved_rotation_valid = true;
2607 }
2608}
2609
2610static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002611set_surface_type(struct shell_surface *shsurf)
2612{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002613 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002614 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002615
Philip Withnallbecb77e2013-11-25 18:01:30 +00002616 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002617
Rafael Antognolli03b16592013-12-03 15:35:42 -02002618 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002619 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002620
2621 switch (shsurf->type) {
2622 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002623 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002624 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002625 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002626 weston_view_set_position(shsurf->view,
2627 pev->geometry.x + shsurf->transient.x,
2628 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002629 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002630 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002631
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002632 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002633 weston_view_set_position(shsurf->view, shsurf->transient.x,
2634 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002635 break;
2636
Philip Withnall0f640e22013-11-25 18:01:31 +00002637 case SHELL_SURFACE_POPUP:
2638 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002639 default:
2640 break;
2641 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002642
2643 /* Update the surface’s layer. */
2644 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002645}
2646
Tiago Vignattibe143262012-04-16 17:31:41 +03002647static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002648shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002649{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002650 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002651}
2652
Alex Wu21858432012-04-01 20:13:08 +08002653static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002654black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002655
Jason Ekstranda7af7042013-10-12 22:38:11 -05002656static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002657create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002658 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002659 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002660{
2661 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002662 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002663
2664 surface = weston_surface_create(ec);
2665 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002666 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002667 return NULL;
2668 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002669 view = weston_view_create(surface);
2670 if (surface == NULL) {
2671 weston_log("no memory\n");
2672 weston_surface_destroy(surface);
2673 return NULL;
2674 }
Alex Wu4539b082012-03-01 12:57:46 +08002675
Alex Wu21858432012-04-01 20:13:08 +08002676 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002677 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002678 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002679 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002680 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002681 pixman_region32_fini(&surface->input);
2682 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002683
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002684 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002685 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002686
2687 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002688}
2689
Philip Withnalled8f1a92013-11-25 18:01:43 +00002690static void
2691shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2692{
2693 struct weston_output *output = shsurf->fullscreen_output;
2694
Rafael Antognolli03b16592013-12-03 15:35:42 -02002695 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002696
2697 if (!shsurf->fullscreen.black_view)
2698 shsurf->fullscreen.black_view =
2699 create_black_surface(shsurf->surface->compositor,
2700 shsurf->surface,
2701 output->x, output->y,
2702 output->width,
2703 output->height);
2704
2705 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2706 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2707 wl_list_insert(&shsurf->view->layer_link,
2708 &shsurf->fullscreen.black_view->layer_link);
2709 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2710 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002711
2712 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002713}
2714
Alex Wu4539b082012-03-01 12:57:46 +08002715/* Create black surface and append it to the associated fullscreen surface.
2716 * Handle size dismatch and positioning according to the method. */
2717static void
2718shell_configure_fullscreen(struct shell_surface *shsurf)
2719{
2720 struct weston_output *output = shsurf->fullscreen_output;
2721 struct weston_surface *surface = shsurf->surface;
2722 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002723 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002724 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002725
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002726 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2727 restore_output_mode(output);
2728
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002729 /* Reverse the effect of lower_fullscreen_layer() */
2730 wl_list_remove(&shsurf->view->layer_link);
2731 wl_list_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
2732
Philip Withnalled8f1a92013-11-25 18:01:43 +00002733 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002734
Jason Ekstranda7af7042013-10-12 22:38:11 -05002735 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002736 &surf_width, &surf_height);
2737
Alex Wu4539b082012-03-01 12:57:46 +08002738 switch (shsurf->fullscreen.type) {
2739 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002740 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002741 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002742 break;
2743 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002744 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002745 if (output->width == surf_width &&
2746 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002747 weston_view_set_position(shsurf->view,
2748 output->x - surf_x,
2749 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002750 break;
2751 }
2752
Alex Wu4539b082012-03-01 12:57:46 +08002753 matrix = &shsurf->fullscreen.transform.matrix;
2754 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002755
Scott Moreau1bad5db2012-08-18 01:04:05 -06002756 output_aspect = (float) output->width /
2757 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002758 /* XXX: Use surf_width and surf_height here? */
2759 surface_aspect = (float) surface->width /
2760 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002761 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002762 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002763 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002764 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002765 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002766 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002767
Alex Wu4539b082012-03-01 12:57:46 +08002768 weston_matrix_scale(matrix, scale, scale, 1);
2769 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002770 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002771 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002772 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2773 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002774 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002775
Alex Wu4539b082012-03-01 12:57:46 +08002776 break;
2777 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002778 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002779 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002780 surf_width * surface->buffer_viewport.buffer.scale,
2781 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002782 shsurf->fullscreen.framerate};
2783
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002784 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002785 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002786 weston_view_set_position(shsurf->view,
2787 output->x - surf_x,
2788 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002789 shsurf->fullscreen.black_view->surface->width = output->width;
2790 shsurf->fullscreen.black_view->surface->height = output->height;
2791 weston_view_set_position(shsurf->fullscreen.black_view,
2792 output->x - surf_x,
2793 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002794 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002795 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002796 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002797 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002798 }
Alex Wubd3354b2012-04-17 17:20:49 +08002799 }
Alex Wu4539b082012-03-01 12:57:46 +08002800 break;
2801 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002802 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002803 break;
2804 default:
2805 break;
2806 }
2807}
2808
Alex Wu4539b082012-03-01 12:57:46 +08002809static void
2810shell_map_fullscreen(struct shell_surface *shsurf)
2811{
Alex Wubd3354b2012-04-17 17:20:49 +08002812 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002813}
2814
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002815static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002816set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2817{
2818 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002819 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002820 shsurf->transient.x = x;
2821 shsurf->transient.y = y;
2822 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002823
2824 shell_surface_set_parent(shsurf, NULL);
2825
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002826 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002827 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002828}
2829
Kristian Høgsberg47792412014-05-04 13:47:06 -07002830static void
2831shell_interface_set_fullscreen(struct shell_surface *shsurf,
2832 uint32_t method,
2833 uint32_t framerate,
2834 struct weston_output *output)
2835{
2836 surface_clear_next_states(shsurf);
2837 set_fullscreen(shsurf, method, framerate, output);
2838
2839 shsurf->next_state.fullscreen = true;
2840 shsurf->state_changed = true;
2841}
2842
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002843static int
2844shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2845{
2846 return surface_move(shsurf, ws, 1);
2847}
2848
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002849static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002850
2851static void
2852destroy_shell_seat(struct wl_listener *listener, void *data)
2853{
2854 struct shell_seat *shseat =
2855 container_of(listener,
2856 struct shell_seat, seat_destroy_listener);
2857 struct shell_surface *shsurf, *prev = NULL;
2858
2859 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002860 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002861 shseat->popup_grab.client = NULL;
2862
2863 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2864 shsurf->popup.shseat = NULL;
2865 if (prev) {
2866 wl_list_init(&prev->popup.grab_link);
2867 }
2868 prev = shsurf;
2869 }
2870 wl_list_init(&prev->popup.grab_link);
2871 }
2872
2873 wl_list_remove(&shseat->seat_destroy_listener.link);
2874 free(shseat);
2875}
2876
Jason Ekstrand024177c2014-04-21 19:42:58 -05002877static void
2878shell_seat_caps_changed(struct wl_listener *listener, void *data)
2879{
2880 struct shell_seat *seat;
2881
2882 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2883
2884 if (seat->seat->keyboard &&
2885 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2886 wl_signal_add(&seat->seat->keyboard->focus_signal,
2887 &seat->keyboard_focus_listener);
2888 } else if (!seat->seat->keyboard) {
2889 wl_list_init(&seat->keyboard_focus_listener.link);
2890 }
2891
2892 if (seat->seat->pointer &&
2893 wl_list_empty(&seat->pointer_focus_listener.link)) {
2894 wl_signal_add(&seat->seat->pointer->focus_signal,
2895 &seat->pointer_focus_listener);
2896 } else if (!seat->seat->pointer) {
2897 wl_list_init(&seat->pointer_focus_listener.link);
2898 }
2899}
2900
Giulio Camuffo5085a752013-03-25 21:42:45 +01002901static struct shell_seat *
2902create_shell_seat(struct weston_seat *seat)
2903{
2904 struct shell_seat *shseat;
2905
2906 shseat = calloc(1, sizeof *shseat);
2907 if (!shseat) {
2908 weston_log("no memory to allocate shell seat\n");
2909 return NULL;
2910 }
2911
2912 shseat->seat = seat;
2913 wl_list_init(&shseat->popup_grab.surfaces_list);
2914
2915 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2916 wl_signal_add(&seat->destroy_signal,
2917 &shseat->seat_destroy_listener);
2918
Jason Ekstrand024177c2014-04-21 19:42:58 -05002919 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2920 wl_list_init(&shseat->keyboard_focus_listener.link);
2921
2922 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2923 wl_list_init(&shseat->pointer_focus_listener.link);
2924
2925 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2926 wl_signal_add(&seat->updated_caps_signal,
2927 &shseat->caps_changed_listener);
2928 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2929
Giulio Camuffo5085a752013-03-25 21:42:45 +01002930 return shseat;
2931}
2932
2933static struct shell_seat *
2934get_shell_seat(struct weston_seat *seat)
2935{
2936 struct wl_listener *listener;
2937
2938 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002939 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002940
2941 return container_of(listener,
2942 struct shell_seat, seat_destroy_listener);
2943}
2944
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002945static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002946popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002947{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002948 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002949 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002950 struct shell_seat *shseat =
2951 container_of(grab, struct shell_seat, popup_grab.grab);
2952 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002953 wl_fixed_t sx, sy;
2954
Jason Ekstranda7af7042013-10-12 22:38:11 -05002955 view = weston_compositor_pick_view(pointer->seat->compositor,
2956 pointer->x, pointer->y,
2957 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002958
Jason Ekstranda7af7042013-10-12 22:38:11 -05002959 if (view && view->surface->resource &&
2960 wl_resource_get_client(view->surface->resource) == client) {
2961 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002962 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002963 weston_pointer_set_focus(pointer, NULL,
2964 wl_fixed_from_int(0),
2965 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002966 }
2967}
2968
2969static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002970popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2971 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002972{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002973 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002974 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002975 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002976
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002977 weston_pointer_move(pointer, x, y);
2978
Neil Roberts96d790e2013-09-19 17:32:00 +01002979 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002980 weston_view_from_global_fixed(pointer->focus,
2981 pointer->x, pointer->y,
2982 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002983 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002984 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002985}
2986
2987static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002988popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002989 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002990{
2991 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002992 struct shell_seat *shseat =
2993 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002994 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002995 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002996 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01002997 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002998
Neil Roberts96d790e2013-09-19 17:32:00 +01002999 resource_list = &grab->pointer->focus_resource_list;
3000 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003001 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003002 wl_resource_for_each(resource, resource_list) {
3003 wl_pointer_send_button(resource, serial,
3004 time, button, state);
3005 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003006 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003007 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003008 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003009 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003010 }
3011
Daniel Stone4dbadb12012-05-30 16:31:51 +01003012 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003013 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003014}
3015
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003016static void
3017popup_grab_cancel(struct weston_pointer_grab *grab)
3018{
3019 popup_grab_end(grab->pointer);
3020}
3021
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003022static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003023 popup_grab_focus,
3024 popup_grab_motion,
3025 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003026 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003027};
3028
3029static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003030shell_surface_send_popup_done(struct shell_surface *shsurf)
3031{
3032 if (shell_surface_is_wl_shell_surface(shsurf))
3033 wl_shell_surface_send_popup_done(shsurf->resource);
3034 else if (shell_surface_is_xdg_popup(shsurf))
3035 xdg_popup_send_popup_done(shsurf->resource,
3036 shsurf->popup.serial);
3037}
3038
3039static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003040popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003041{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003042 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003043 struct shell_seat *shseat =
3044 container_of(grab, struct shell_seat, popup_grab.grab);
3045 struct shell_surface *shsurf;
3046 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003047
3048 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003049 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003050 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003051 shseat->popup_grab.grab.interface = NULL;
3052 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003053 /* Send the popup_done event to all the popups open */
3054 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003055 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003056 shsurf->popup.shseat = NULL;
3057 if (prev) {
3058 wl_list_init(&prev->popup.grab_link);
3059 }
3060 prev = shsurf;
3061 }
3062 wl_list_init(&prev->popup.grab_link);
3063 wl_list_init(&shseat->popup_grab.surfaces_list);
3064 }
3065}
3066
3067static void
3068add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
3069{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003070 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003071
3072 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003073 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003074 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003075 /* We must make sure here that this popup was opened after
3076 * a mouse press, and not just by moving around with other
3077 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04003078 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003079 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003080
Rob Bradforddfe31052013-06-26 19:49:11 +01003081 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003082 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003083 } else {
3084 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003085 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003086}
3087
3088static void
3089remove_popup_grab(struct shell_surface *shsurf)
3090{
3091 struct shell_seat *shseat = shsurf->popup.shseat;
3092
3093 wl_list_remove(&shsurf->popup.grab_link);
3094 wl_list_init(&shsurf->popup.grab_link);
3095 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003096 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003097 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003098 }
3099}
3100
3101static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003102shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003103{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003104 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003105 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003106
Jason Ekstranda7af7042013-10-12 22:38:11 -05003107 shsurf->surface->output = parent_view->output;
3108 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003109
Jason Ekstranda7af7042013-10-12 22:38:11 -05003110 weston_view_set_transform_parent(shsurf->view, parent_view);
3111 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3112 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003113
Kristian Høgsberge3148752013-05-06 23:19:49 -04003114 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003115 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003116 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003117 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003118 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003119 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003120}
3121
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003122static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003123 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003124 shell_surface_move,
3125 shell_surface_resize,
3126 shell_surface_set_toplevel,
3127 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003128 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003129 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003130 shell_surface_set_maximized,
3131 shell_surface_set_title,
3132 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003133};
3134
3135static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003136destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003137{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003138 struct shell_surface *child, *next;
3139
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003140 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3141
Giulio Camuffo5085a752013-03-25 21:42:45 +01003142 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3143 remove_popup_grab(shsurf);
3144 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003145
Alex Wubd3354b2012-04-17 17:20:49 +08003146 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003147 shell_surface_is_top_fullscreen(shsurf))
3148 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003149
Jason Ekstranda7af7042013-10-12 22:38:11 -05003150 if (shsurf->fullscreen.black_view)
3151 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003152
Alex Wubd3354b2012-04-17 17:20:49 +08003153 /* As destroy_resource() use wl_list_for_each_safe(),
3154 * we can always remove the listener.
3155 */
3156 wl_list_remove(&shsurf->surface_destroy_listener.link);
3157 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003158 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003159
Jason Ekstranda7af7042013-10-12 22:38:11 -05003160 weston_view_destroy(shsurf->view);
3161
Philip Withnall648a4dd2013-11-25 18:01:44 +00003162 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003163 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3164 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003165
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003166 wl_list_remove(&shsurf->link);
3167 free(shsurf);
3168}
3169
3170static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003171shell_destroy_shell_surface(struct wl_resource *resource)
3172{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003173 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003174
Bryan Caina46b9462014-04-04 17:41:24 -05003175 if (!wl_list_empty(&shsurf->popup.grab_link))
3176 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003177 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003178}
3179
3180static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003181shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003182{
3183 struct shell_surface *shsurf = container_of(listener,
3184 struct shell_surface,
3185 surface_destroy_listener);
3186
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003187 if (shsurf->resource)
3188 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003189
3190 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003191}
3192
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003193static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003194fade_out_done(struct weston_view_animation *animation, void *data)
3195{
3196 struct shell_surface *shsurf = data;
3197
3198 weston_surface_destroy(shsurf->surface);
3199}
3200
3201static void
3202handle_resource_destroy(struct wl_listener *listener, void *data)
3203{
3204 struct shell_surface *shsurf =
3205 container_of(listener, struct shell_surface,
3206 resource_destroy_listener);
3207
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003208 if (!weston_surface_is_mapped(shsurf->surface))
3209 return;
3210
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003211 shsurf->surface->ref_count++;
3212
3213 pixman_region32_fini(&shsurf->surface->pending.input);
3214 pixman_region32_init(&shsurf->surface->pending.input);
3215 pixman_region32_fini(&shsurf->surface->input);
3216 pixman_region32_init(&shsurf->surface->input);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003217 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3218 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003219}
3220
3221static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003222shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003223
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003224struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003225get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003226{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003227 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003228 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003229 else
3230 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003231}
3232
Rafael Antognollie2a34552013-12-03 15:35:45 -02003233static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003234create_common_surface(struct shell_client *owner, void *shell,
3235 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003236 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003237{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003238 struct shell_surface *shsurf;
3239
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003240 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003241 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003242 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003243 }
3244
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003245 shsurf = calloc(1, sizeof *shsurf);
3246 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003247 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003248 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003249 }
3250
Jason Ekstranda7af7042013-10-12 22:38:11 -05003251 shsurf->view = weston_view_create(surface);
3252 if (!shsurf->view) {
3253 weston_log("no memory to allocate shell surface\n");
3254 free(shsurf);
3255 return NULL;
3256 }
3257
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003258 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003259 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003260
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003261 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3262 wl_resource_add_destroy_listener(surface->resource,
3263 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003264 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003265
Tiago Vignattibc052c92012-04-19 16:18:18 +03003266 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003267 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003268 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003269 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003270 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003271 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003272 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3273 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003274 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003275 wl_list_init(&shsurf->fullscreen.transform.link);
3276
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003277 shsurf->output = get_default_output(shsurf->shell->compositor);
3278
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003279 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003280 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003281 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003282 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003283
3284 /* init link so its safe to always remove it in destroy_shell_surface */
3285 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003286 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003287
Pekka Paalanen460099f2012-01-20 16:48:25 +02003288 /* empty when not in use */
3289 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003290 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003291
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003292 wl_list_init(&shsurf->workspace_transform.link);
3293
Philip Withnall648a4dd2013-11-25 18:01:44 +00003294 wl_list_init(&shsurf->children_link);
3295 wl_list_init(&shsurf->children_list);
3296 shsurf->parent = NULL;
3297
Pekka Paalanen98262232011-12-01 10:42:22 +02003298 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003299
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003300 shsurf->client = client;
3301
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003302 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003303}
3304
Rafael Antognollie2a34552013-12-03 15:35:45 -02003305static struct shell_surface *
3306create_shell_surface(void *shell, struct weston_surface *surface,
3307 const struct weston_shell_client *client)
3308{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003309 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003310}
3311
Jason Ekstranda7af7042013-10-12 22:38:11 -05003312static struct weston_view *
3313get_primary_view(void *shell, struct shell_surface *shsurf)
3314{
3315 return shsurf->view;
3316}
3317
Tiago Vignattibc052c92012-04-19 16:18:18 +03003318static void
3319shell_get_shell_surface(struct wl_client *client,
3320 struct wl_resource *resource,
3321 uint32_t id,
3322 struct wl_resource *surface_resource)
3323{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003324 struct weston_surface *surface =
3325 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003326 struct shell_client *sc = wl_resource_get_user_data(resource);
3327 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003328 struct shell_surface *shsurf;
3329
3330 if (get_shell_surface(surface)) {
3331 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003332 WL_DISPLAY_ERROR_INVALID_OBJECT,
3333 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003334 return;
3335 }
3336
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003337 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003338 if (!shsurf) {
3339 wl_resource_post_error(surface_resource,
3340 WL_DISPLAY_ERROR_INVALID_OBJECT,
3341 "surface->configure already set");
3342 return;
3343 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003344
Jason Ekstranda85118c2013-06-27 20:17:02 -05003345 shsurf->resource =
3346 wl_resource_create(client,
3347 &wl_shell_surface_interface, 1, id);
3348 wl_resource_set_implementation(shsurf->resource,
3349 &shell_surface_implementation,
3350 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003351}
3352
Rafael Antognollie2a34552013-12-03 15:35:45 -02003353static bool
3354shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3355{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003356 /* A shell surface without a resource is created from xwayland
3357 * and is considered a wl_shell surface for now. */
3358
3359 return shsurf->resource == NULL ||
3360 wl_resource_instance_of(shsurf->resource,
3361 &wl_shell_surface_interface,
3362 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003363}
3364
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003365static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003366 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003367};
3368
Rafael Antognollie2a34552013-12-03 15:35:45 -02003369/****************************
3370 * xdg-shell implementation */
3371
3372static void
3373xdg_surface_destroy(struct wl_client *client,
3374 struct wl_resource *resource)
3375{
3376 wl_resource_destroy(resource);
3377}
3378
3379static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003380xdg_surface_set_parent(struct wl_client *client,
3381 struct wl_resource *resource,
3382 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003383{
3384 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3385 struct weston_surface *parent;
3386
3387 if (parent_resource)
3388 parent = wl_resource_get_user_data(parent_resource);
3389 else
3390 parent = NULL;
3391
3392 shell_surface_set_parent(shsurf, parent);
3393}
3394
3395static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003396xdg_surface_set_app_id(struct wl_client *client,
3397 struct wl_resource *resource,
3398 const char *app_id)
3399{
3400 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3401
3402 free(shsurf->class);
3403 shsurf->class = strdup(app_id);
3404}
3405
3406static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003407xdg_surface_show_window_menu(struct wl_client *client,
3408 struct wl_resource *surface_resource,
3409 struct wl_resource *seat_resource,
3410 uint32_t serial,
3411 int32_t x,
3412 int32_t y)
3413{
3414 /* TODO */
3415}
3416
3417static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003418xdg_surface_set_title(struct wl_client *client,
3419 struct wl_resource *resource, const char *title)
3420{
3421 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3422
3423 set_title(shsurf, title);
3424}
3425
3426static void
3427xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3428 struct wl_resource *seat_resource, uint32_t serial)
3429{
3430 common_surface_move(resource, seat_resource, serial);
3431}
3432
3433static void
3434xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3435 struct wl_resource *seat_resource, uint32_t serial,
3436 uint32_t edges)
3437{
3438 common_surface_resize(resource, seat_resource, serial, edges);
3439}
3440
3441static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003442xdg_surface_ack_configure(struct wl_client *client,
3443 struct wl_resource *resource,
3444 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003445{
3446 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3447
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003448 if (shsurf->state_requested) {
3449 shsurf->next_state = shsurf->requested_state;
3450 shsurf->state_changed = true;
3451 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003452 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003453}
3454
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003455static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003456xdg_surface_set_window_geometry(struct wl_client *client,
3457 struct wl_resource *resource,
3458 int32_t x,
3459 int32_t y,
3460 int32_t width,
3461 int32_t height)
3462{
3463 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3464
3465 set_window_geometry(shsurf, x, y, width, height);
3466}
3467
3468static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003469xdg_surface_set_maximized(struct wl_client *client,
3470 struct wl_resource *resource)
3471{
3472 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3473
3474 shsurf->state_requested = true;
3475 shsurf->requested_state.maximized = true;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003476 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003477}
3478
3479static void
3480xdg_surface_unset_maximized(struct wl_client *client,
3481 struct wl_resource *resource)
3482{
3483 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3484
3485 shsurf->state_requested = true;
3486 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003487 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003488}
3489
3490static void
3491xdg_surface_set_fullscreen(struct wl_client *client,
3492 struct wl_resource *resource,
3493 struct wl_resource *output_resource)
3494{
3495 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3496 struct weston_output *output;
3497
3498 shsurf->state_requested = true;
3499 shsurf->requested_state.fullscreen = true;
3500
3501 if (output_resource)
3502 output = wl_resource_get_user_data(output_resource);
3503 else
3504 output = NULL;
3505
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003506 shell_surface_set_output(shsurf, output);
3507 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003508
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003509 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003510}
3511
3512static void
3513xdg_surface_unset_fullscreen(struct wl_client *client,
3514 struct wl_resource *resource)
3515{
3516 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3517
3518 shsurf->state_requested = true;
3519 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003520 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003521}
3522
3523static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003524xdg_surface_set_minimized(struct wl_client *client,
3525 struct wl_resource *resource)
3526{
3527 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3528
3529 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3530 return;
3531
3532 /* apply compositor's own minimization logic (hide) */
3533 set_minimized(shsurf->surface, 1);
3534}
3535
Rafael Antognollie2a34552013-12-03 15:35:45 -02003536static const struct xdg_surface_interface xdg_surface_implementation = {
3537 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003538 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003539 xdg_surface_set_title,
3540 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003541 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003542 xdg_surface_move,
3543 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003544 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003545 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003546 xdg_surface_set_maximized,
3547 xdg_surface_unset_maximized,
3548 xdg_surface_set_fullscreen,
3549 xdg_surface_unset_fullscreen,
3550 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003551};
3552
3553static void
3554xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003555 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003556{
3557 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003558 uint32_t *s;
3559 struct wl_array states;
3560 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003561
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003562 assert(shsurf);
3563
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003564 if (!shsurf->resource)
3565 return;
3566
3567 wl_array_init(&states);
3568 if (shsurf->requested_state.fullscreen) {
3569 s = wl_array_add(&states, sizeof *s);
3570 *s = XDG_SURFACE_STATE_FULLSCREEN;
3571 } else if (shsurf->requested_state.maximized) {
3572 s = wl_array_add(&states, sizeof *s);
3573 *s = XDG_SURFACE_STATE_MAXIMIZED;
3574 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003575 if (shsurf->resize_edges != 0) {
3576 s = wl_array_add(&states, sizeof *s);
3577 *s = XDG_SURFACE_STATE_RESIZING;
3578 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003579 if (shsurf->focus_count > 0) {
3580 s = wl_array_add(&states, sizeof *s);
3581 *s = XDG_SURFACE_STATE_ACTIVATED;
3582 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003583
3584 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3585 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3586
3587 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003588}
3589
3590static const struct weston_shell_client xdg_client = {
3591 xdg_send_configure
3592};
3593
3594static void
3595xdg_use_unstable_version(struct wl_client *client,
3596 struct wl_resource *resource,
3597 int32_t version)
3598{
3599 if (version > 1) {
3600 wl_resource_post_error(resource,
3601 1,
3602 "xdg-shell:: version not implemented yet.");
3603 return;
3604 }
3605}
3606
3607static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003608create_xdg_surface(struct shell_client *owner, void *shell,
3609 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003610 const struct weston_shell_client *client)
3611{
3612 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003613
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003614 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003615 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003616
3617 return shsurf;
3618}
3619
3620static void
3621xdg_get_xdg_surface(struct wl_client *client,
3622 struct wl_resource *resource,
3623 uint32_t id,
3624 struct wl_resource *surface_resource)
3625{
3626 struct weston_surface *surface =
3627 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003628 struct shell_client *sc = wl_resource_get_user_data(resource);
3629 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003630 struct shell_surface *shsurf;
3631
3632 if (get_shell_surface(surface)) {
3633 wl_resource_post_error(surface_resource,
3634 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003635 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003636 return;
3637 }
3638
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003639 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003640 if (!shsurf) {
3641 wl_resource_post_error(surface_resource,
3642 WL_DISPLAY_ERROR_INVALID_OBJECT,
3643 "surface->configure already set");
3644 return;
3645 }
3646
3647 shsurf->resource =
3648 wl_resource_create(client,
3649 &xdg_surface_interface, 1, id);
3650 wl_resource_set_implementation(shsurf->resource,
3651 &xdg_surface_implementation,
3652 shsurf, shell_destroy_shell_surface);
3653}
3654
3655static bool
3656shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3657{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003658 return shsurf->resource &&
3659 wl_resource_instance_of(shsurf->resource,
3660 &xdg_surface_interface,
3661 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003662}
3663
3664/* xdg-popup implementation */
3665
3666static void
3667xdg_popup_destroy(struct wl_client *client,
3668 struct wl_resource *resource)
3669{
3670 wl_resource_destroy(resource);
3671}
3672
Rafael Antognollie2a34552013-12-03 15:35:45 -02003673static const struct xdg_popup_interface xdg_popup_implementation = {
3674 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003675};
3676
3677static void
3678xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003679 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003680{
3681}
3682
3683static const struct weston_shell_client xdg_popup_client = {
3684 xdg_popup_send_configure
3685};
3686
3687static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003688create_xdg_popup(struct shell_client *owner, void *shell,
3689 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003690 const struct weston_shell_client *client,
3691 struct weston_surface *parent,
3692 struct shell_seat *seat,
3693 uint32_t serial,
3694 int32_t x, int32_t y)
3695{
3696 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003697
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003698 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003699 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003700 shsurf->popup.shseat = seat;
3701 shsurf->popup.serial = serial;
3702 shsurf->popup.x = x;
3703 shsurf->popup.y = y;
3704 shell_surface_set_parent(shsurf, parent);
3705
3706 return shsurf;
3707}
3708
3709static void
3710xdg_get_xdg_popup(struct wl_client *client,
3711 struct wl_resource *resource,
3712 uint32_t id,
3713 struct wl_resource *surface_resource,
3714 struct wl_resource *parent_resource,
3715 struct wl_resource *seat_resource,
3716 uint32_t serial,
3717 int32_t x, int32_t y, uint32_t flags)
3718{
3719 struct weston_surface *surface =
3720 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003721 struct shell_client *sc = wl_resource_get_user_data(resource);
3722 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003723 struct shell_surface *shsurf;
3724 struct weston_surface *parent;
3725 struct shell_seat *seat;
3726
3727 if (get_shell_surface(surface)) {
3728 wl_resource_post_error(surface_resource,
3729 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003730 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003731 return;
3732 }
3733
3734 if (!parent_resource) {
3735 wl_resource_post_error(surface_resource,
3736 WL_DISPLAY_ERROR_INVALID_OBJECT,
3737 "xdg_shell::get_xdg_popup requires a parent shell surface");
3738 }
3739
3740 parent = wl_resource_get_user_data(parent_resource);
3741 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3742
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003743 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003744 parent, seat, serial, x, y);
3745 if (!shsurf) {
3746 wl_resource_post_error(surface_resource,
3747 WL_DISPLAY_ERROR_INVALID_OBJECT,
3748 "surface->configure already set");
3749 return;
3750 }
3751
3752 shsurf->resource =
3753 wl_resource_create(client,
3754 &xdg_popup_interface, 1, id);
3755 wl_resource_set_implementation(shsurf->resource,
3756 &xdg_popup_implementation,
3757 shsurf, shell_destroy_shell_surface);
3758}
3759
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003760static void
3761xdg_pong(struct wl_client *client,
3762 struct wl_resource *resource, uint32_t serial)
3763{
3764 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003765
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003766 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003767}
3768
Rafael Antognollie2a34552013-12-03 15:35:45 -02003769static bool
3770shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3771{
3772 return wl_resource_instance_of(shsurf->resource,
3773 &xdg_popup_interface,
3774 &xdg_popup_implementation);
3775}
3776
3777static const struct xdg_shell_interface xdg_implementation = {
3778 xdg_use_unstable_version,
3779 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003780 xdg_get_xdg_popup,
3781 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003782};
3783
3784static int
3785xdg_shell_unversioned_dispatch(const void *implementation,
3786 void *_target, uint32_t opcode,
3787 const struct wl_message *message,
3788 union wl_argument *args)
3789{
3790 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003791 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003792
3793 if (opcode != 0) {
3794 wl_resource_post_error(resource,
3795 WL_DISPLAY_ERROR_INVALID_OBJECT,
3796 "must call use_unstable_version first");
3797 return 0;
3798 }
3799
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003800#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003801
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003802 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3803 "shell implementation doesn't match protocol version");
3804
Rafael Antognollie2a34552013-12-03 15:35:45 -02003805 if (args[0].i != XDG_SERVER_VERSION) {
3806 wl_resource_post_error(resource,
3807 WL_DISPLAY_ERROR_INVALID_OBJECT,
3808 "incompatible version, server is %d "
3809 "client wants %d",
3810 XDG_SERVER_VERSION, args[0].i);
3811 return 0;
3812 }
3813
3814 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003815 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003816
3817 return 1;
3818}
3819
3820/* end of xdg-shell implementation */
3821/***********************************/
3822
Kristian Høgsberg07937562011-04-12 17:25:42 -04003823static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003824shell_fade(struct desktop_shell *shell, enum fade_type type);
3825
3826static int
3827screensaver_timeout(void *data)
3828{
3829 struct desktop_shell *shell = data;
3830
3831 shell_fade(shell, FADE_OUT);
3832
3833 return 1;
3834}
3835
3836static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003837handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003838{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003839 struct desktop_shell *shell =
3840 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003841
Pekka Paalanen18027e52011-12-02 16:31:49 +02003842 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003843
3844 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003845 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003846}
3847
3848static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003849launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003850{
3851 if (shell->screensaver.binding)
3852 return;
3853
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003854 if (!shell->screensaver.path) {
3855 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003856 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003857 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003858
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003859 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003860 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003861 return;
3862 }
3863
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003864 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003865 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003866 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003867 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003868}
3869
3870static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003871terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003872{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003873 if (shell->screensaver.process.pid == 0)
3874 return;
3875
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003876 /* Disarm the screensaver timer, otherwise it may fire when the
3877 * compositor is not in the idle state. In that case, the screen will
3878 * be locked, but the wake_signal won't fire on user input, making the
3879 * system unresponsive. */
3880 wl_event_source_timer_update(shell->screensaver.timer, 0);
3881
Pekka Paalanen18027e52011-12-02 16:31:49 +02003882 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003883}
3884
3885static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003886configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003887{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003888 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003889
Jason Ekstranda7af7042013-10-12 22:38:11 -05003890 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3891 if (v->output == ev->output && v != ev) {
3892 weston_view_unmap(v);
3893 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003894 }
3895 }
3896
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003897 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003898
Jason Ekstranda7af7042013-10-12 22:38:11 -05003899 if (wl_list_empty(&ev->layer_link)) {
3900 wl_list_insert(&layer->view_list, &ev->layer_link);
3901 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003902 }
3903}
3904
3905static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003906background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003907{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003908 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003909 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003910
Jason Ekstranda7af7042013-10-12 22:38:11 -05003911 view = container_of(es->views.next, struct weston_view, surface_link);
3912
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003913 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003914}
3915
3916static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003917desktop_shell_set_background(struct wl_client *client,
3918 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003919 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003920 struct wl_resource *surface_resource)
3921{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003922 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003923 struct weston_surface *surface =
3924 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003925 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003926
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003927 if (surface->configure) {
3928 wl_resource_post_error(surface_resource,
3929 WL_DISPLAY_ERROR_INVALID_OBJECT,
3930 "surface role already assigned");
3931 return;
3932 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003933
Jason Ekstranda7af7042013-10-12 22:38:11 -05003934 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3935 weston_view_destroy(view);
3936 view = weston_view_create(surface);
3937
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003938 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003939 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003940 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003941 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003942 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003943 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003944 surface->output->width,
3945 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003946}
3947
3948static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003949panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003950{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003951 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003952 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003953
Jason Ekstranda7af7042013-10-12 22:38:11 -05003954 view = container_of(es->views.next, struct weston_view, surface_link);
3955
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003956 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003957}
3958
3959static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003960desktop_shell_set_panel(struct wl_client *client,
3961 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003962 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003963 struct wl_resource *surface_resource)
3964{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003965 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003966 struct weston_surface *surface =
3967 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003968 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003969
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003970 if (surface->configure) {
3971 wl_resource_post_error(surface_resource,
3972 WL_DISPLAY_ERROR_INVALID_OBJECT,
3973 "surface role already assigned");
3974 return;
3975 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003976
Jason Ekstranda7af7042013-10-12 22:38:11 -05003977 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3978 weston_view_destroy(view);
3979 view = weston_view_create(surface);
3980
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003981 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003982 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003983 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003984 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003985 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003986 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003987 surface->output->width,
3988 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003989}
3990
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003991static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003992lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003993{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003994 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003995 struct weston_view *view;
3996
3997 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003998
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003999 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004000 return;
4001
Jason Ekstranda7af7042013-10-12 22:38:11 -05004002 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004003
4004 if (!weston_surface_is_mapped(surface)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004005 wl_list_insert(&shell->lock_layer.view_list,
4006 &view->layer_link);
4007 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004008 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004009 }
4010}
4011
4012static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004013handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004014{
Tiago Vignattibe143262012-04-16 17:31:41 +03004015 struct desktop_shell *shell =
4016 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004017
Martin Minarik6d118362012-06-07 18:01:59 +02004018 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004019 shell->lock_surface = NULL;
4020}
4021
4022static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004023desktop_shell_set_lock_surface(struct wl_client *client,
4024 struct wl_resource *resource,
4025 struct wl_resource *surface_resource)
4026{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004027 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004028 struct weston_surface *surface =
4029 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004030
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004031 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004032
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004033 if (!shell->locked)
4034 return;
4035
Pekka Paalanen98262232011-12-01 10:42:22 +02004036 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004037
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004038 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004039 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004040 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004041
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004042 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004043 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004044 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004045}
4046
4047static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004048resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004049{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004050 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004051
Pekka Paalanen77346a62011-11-30 16:26:35 +02004052 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004053
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004054 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004055 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004056 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004057 &shell->input_panel_layer.link);
4058 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004059 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004060 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004061 wl_list_insert(&shell->compositor->cursor_layer.link,
4062 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004063 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004064 wl_list_insert(&shell->fullscreen_layer.link,
4065 &shell->panel_layer.link);
4066 wl_list_insert(&shell->panel_layer.link,
4067 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004068
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004069 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004070
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004071 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004072 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004073 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004074}
4075
4076static void
4077desktop_shell_unlock(struct wl_client *client,
4078 struct wl_resource *resource)
4079{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004080 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004081
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004082 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004083
4084 if (shell->locked)
4085 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004086}
4087
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004088static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004089desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004090 struct wl_resource *resource,
4091 struct wl_resource *surface_resource)
4092{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004093 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004094
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004095 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004096 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004097}
4098
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004099static void
4100desktop_shell_desktop_ready(struct wl_client *client,
4101 struct wl_resource *resource)
4102{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004103 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004104
4105 shell_fade_startup(shell);
4106}
4107
Kristian Høgsberg75840622011-09-06 13:48:16 -04004108static const struct desktop_shell_interface desktop_shell_implementation = {
4109 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004110 desktop_shell_set_panel,
4111 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004112 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004113 desktop_shell_set_grab_surface,
4114 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04004115};
4116
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004117static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004118get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004119{
4120 struct shell_surface *shsurf;
4121
4122 shsurf = get_shell_surface(surface);
4123 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004124 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004125 return shsurf->type;
4126}
4127
Kristian Høgsberg75840622011-09-06 13:48:16 -04004128static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004129move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004130{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004131 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004132 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004133 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004134
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004135 if (seat->pointer->focus == NULL)
4136 return;
4137
4138 focus = seat->pointer->focus->surface;
4139
Pekka Paalanen01388e22013-04-25 13:57:44 +03004140 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004141 if (surface == NULL)
4142 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004143
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004144 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004145 if (shsurf == NULL || shsurf->state.fullscreen ||
4146 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004147 return;
4148
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004149 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004150}
4151
4152static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004153maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4154{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004155 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004156 struct weston_surface *surface;
4157 struct shell_surface *shsurf;
4158
4159 surface = weston_surface_get_main_surface(focus);
4160 if (surface == NULL)
4161 return;
4162
4163 shsurf = get_shell_surface(surface);
4164 if (shsurf == NULL)
4165 return;
4166
4167 if (!shell_surface_is_xdg_surface(shsurf))
4168 return;
4169
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004170 shsurf->state_requested = true;
4171 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004172 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004173}
4174
4175static void
4176fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4177{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004178 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004179 struct weston_surface *surface;
4180 struct shell_surface *shsurf;
4181
4182 surface = weston_surface_get_main_surface(focus);
4183 if (surface == NULL)
4184 return;
4185
4186 shsurf = get_shell_surface(surface);
4187 if (shsurf == NULL)
4188 return;
4189
4190 if (!shell_surface_is_xdg_surface(shsurf))
4191 return;
4192
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004193 shsurf->state_requested = true;
4194 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004195 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004196 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004197}
4198
4199static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004200touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4201{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004202 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004203 struct weston_surface *surface;
4204 struct shell_surface *shsurf;
4205
4206 surface = weston_surface_get_main_surface(focus);
4207 if (surface == NULL)
4208 return;
4209
4210 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004211 if (shsurf == NULL || shsurf->state.fullscreen ||
4212 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004213 return;
4214
4215 surface_touch_move(shsurf, (struct weston_seat *) seat);
4216}
4217
4218static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004219resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004220{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004221 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004222 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004223 uint32_t edges = 0;
4224 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004225 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004226
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004227 if (seat->pointer->focus == NULL)
4228 return;
4229
4230 focus = seat->pointer->focus->surface;
4231
Pekka Paalanen01388e22013-04-25 13:57:44 +03004232 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004233 if (surface == NULL)
4234 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004235
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004236 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004237 if (shsurf == NULL || shsurf->state.fullscreen ||
4238 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004239 return;
4240
Jason Ekstranda7af7042013-10-12 22:38:11 -05004241 weston_view_from_global(shsurf->view,
4242 wl_fixed_to_int(seat->pointer->grab_x),
4243 wl_fixed_to_int(seat->pointer->grab_y),
4244 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004245
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004246 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004247 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004248 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004249 edges |= 0;
4250 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004251 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004252
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004253 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004254 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004255 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004256 edges |= 0;
4257 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004258 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004259
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004260 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004261}
4262
4263static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004264surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004265 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004266{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004267 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004268 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004269 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004270 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004271
Pekka Paalanen01388e22013-04-25 13:57:44 +03004272 /* XXX: broken for windows containing sub-surfaces */
4273 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004274 if (surface == NULL)
4275 return;
4276
4277 shsurf = get_shell_surface(surface);
4278 if (!shsurf)
4279 return;
4280
Jason Ekstranda7af7042013-10-12 22:38:11 -05004281 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004282
Jason Ekstranda7af7042013-10-12 22:38:11 -05004283 if (shsurf->view->alpha > 1.0)
4284 shsurf->view->alpha = 1.0;
4285 if (shsurf->view->alpha < step)
4286 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004287
Jason Ekstranda7af7042013-10-12 22:38:11 -05004288 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004289 weston_surface_damage(surface);
4290}
4291
4292static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004293do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004294 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004295{
Daniel Stone37816df2012-05-16 18:45:18 +01004296 struct weston_seat *ws = (struct weston_seat *) seat;
4297 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004298 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004299 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004300
4301 wl_list_for_each(output, &compositor->output_list, link) {
4302 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004303 wl_fixed_to_double(seat->pointer->x),
4304 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004305 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004306 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004307 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004308 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004309 increment = -output->zoom.increment;
4310 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004311 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004312 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004313 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004314 else
4315 increment = 0;
4316
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004317 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004318
Scott Moreaue6603982012-06-11 13:07:51 -06004319 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004320 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004321 else if (output->zoom.level > output->zoom.max_level)
4322 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004323 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004324 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004325 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004326
Scott Moreaue6603982012-06-11 13:07:51 -06004327 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004328
Jason Ekstranda7af7042013-10-12 22:38:11 -05004329 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004330 }
4331 }
4332}
4333
Scott Moreauccbf29d2012-02-22 14:21:41 -07004334static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004335zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004336 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004337{
4338 do_zoom(seat, time, 0, axis, value);
4339}
4340
4341static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004342zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004343 void *data)
4344{
4345 do_zoom(seat, time, key, 0, 0);
4346}
4347
4348static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004349terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004350 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004351{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004352 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004353
Daniel Stone325fc2d2012-05-30 16:31:58 +01004354 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004355}
4356
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004357static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004358rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4359 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004360{
4361 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004362 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004363 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004364 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004365 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004366
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004367 weston_pointer_move(pointer, x, y);
4368
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004369 if (!shsurf)
4370 return;
4371
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004372 cx = 0.5f * shsurf->surface->width;
4373 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004374
Daniel Stone37816df2012-05-16 18:45:18 +01004375 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4376 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004377 r = sqrtf(dx * dx + dy * dy);
4378
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004379 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004380 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004381
4382 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004383 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004384 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004385
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004386 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004387 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004388
4389 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004390 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004391 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004392 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004393 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004394
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004395 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004396 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004397 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004398 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004399 wl_list_init(&shsurf->rotation.transform.link);
4400 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004401 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004402 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004403
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004404 /* We need to adjust the position of the surface
4405 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004406 cposx = shsurf->view->geometry.x + cx;
4407 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004408 dposx = rotate->center.x - cposx;
4409 dposy = rotate->center.y - cposy;
4410 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004411 weston_view_set_position(shsurf->view,
4412 shsurf->view->geometry.x + dposx,
4413 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004414 }
4415
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004416 /* Repaint implies weston_surface_update_transform(), which
4417 * lazily applies the damage due to rotation update.
4418 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004419 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004420}
4421
4422static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004423rotate_grab_button(struct weston_pointer_grab *grab,
4424 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004425{
4426 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004427 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004428 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004429 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004430 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004431
Daniel Stone4dbadb12012-05-30 16:31:51 +01004432 if (pointer->button_count == 0 &&
4433 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004434 if (shsurf)
4435 weston_matrix_multiply(&shsurf->rotation.rotation,
4436 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004437 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004438 free(rotate);
4439 }
4440}
4441
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004442static void
4443rotate_grab_cancel(struct weston_pointer_grab *grab)
4444{
4445 struct rotate_grab *rotate =
4446 container_of(grab, struct rotate_grab, base.grab);
4447
4448 shell_grab_end(&rotate->base);
4449 free(rotate);
4450}
4451
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004452static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004453 noop_grab_focus,
4454 rotate_grab_motion,
4455 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004456 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004457};
4458
4459static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004460surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004461{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004462 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004463 float dx, dy;
4464 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004465
Pekka Paalanen460099f2012-01-20 16:48:25 +02004466 rotate = malloc(sizeof *rotate);
4467 if (!rotate)
4468 return;
4469
Jason Ekstranda7af7042013-10-12 22:38:11 -05004470 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004471 surface->surface->width * 0.5f,
4472 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004473 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004474
Daniel Stone37816df2012-05-16 18:45:18 +01004475 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4476 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004477 r = sqrtf(dx * dx + dy * dy);
4478 if (r > 20.0f) {
4479 struct weston_matrix inverse;
4480
4481 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004482 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004483 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004484
4485 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004486 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004487 } else {
4488 weston_matrix_init(&surface->rotation.rotation);
4489 weston_matrix_init(&rotate->rotation);
4490 }
4491
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004492 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4493 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004494}
4495
4496static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004497rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004498 void *data)
4499{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004500 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004501 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004502 struct shell_surface *surface;
4503
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004504 if (seat->pointer->focus == NULL)
4505 return;
4506
4507 focus = seat->pointer->focus->surface;
4508
Pekka Paalanen01388e22013-04-25 13:57:44 +03004509 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004510 if (base_surface == NULL)
4511 return;
4512
4513 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004514 if (surface == NULL || surface->state.fullscreen ||
4515 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004516 return;
4517
4518 surface_rotate(surface, seat);
4519}
4520
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004521/* Move all fullscreen layers down to the current workspace and hide their
4522 * black views. The surfaces' state is set to both fullscreen and lowered,
4523 * and this is reversed when such a surface is re-configured, see
4524 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4525 *
4526 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004527 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004528void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004529lower_fullscreen_layer(struct desktop_shell *shell)
4530{
4531 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004532 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004533
4534 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004535 wl_list_for_each_reverse_safe(view, prev,
4536 &shell->fullscreen_layer.view_list,
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004537 layer_link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004538 struct shell_surface *shsurf = get_shell_surface(view->surface);
4539
4540 if (!shsurf)
4541 continue;
4542
4543 /* We can have a non-fullscreen popup for a fullscreen surface
4544 * in the fullscreen layer. */
4545 if (shsurf->state.fullscreen) {
4546 /* Hide the black view */
4547 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
4548 wl_list_init(&shsurf->fullscreen.black_view->layer_link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07004549 weston_view_damage_below(shsurf->fullscreen.black_view);
4550
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004551 }
4552
4553 /* Lower the view to the workspace layer */
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004554 wl_list_remove(&view->layer_link);
4555 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4556 weston_view_damage_below(view);
4557 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004558
4559 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004560 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004561}
4562
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004563void
Tiago Vignattibe143262012-04-16 17:31:41 +03004564activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004565 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004566{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004567 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004568 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004569 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004570 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004571 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004572
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004573 lower_fullscreen_layer(shell);
4574
Pekka Paalanen01388e22013-04-25 13:57:44 +03004575 main_surface = weston_surface_get_main_surface(es);
4576
Daniel Stone37816df2012-05-16 18:45:18 +01004577 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004578
Jonas Ådahl8538b222012-08-29 22:13:03 +02004579 state = ensure_focus_state(shell, seat);
4580 if (state == NULL)
4581 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004582
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004583 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004584 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004585
Rafael Antognolli03b16592013-12-03 15:35:42 -02004586 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004587 assert(shsurf);
4588
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004589 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004590 shell_configure_fullscreen(shsurf);
4591 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004592 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004593
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004594 /* Update the surface’s layer. This brings it to the top of the stacking
4595 * order as appropriate. */
4596 shell_surface_update_layer(shsurf);
4597
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004598 if (shell->focus_animation_type != ANIMATION_NONE) {
4599 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004600 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004601 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004602}
4603
Alex Wu21858432012-04-01 20:13:08 +08004604/* no-op func for checking black surface */
4605static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004606black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004607{
4608}
4609
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004610static bool
Alex Wu21858432012-04-01 20:13:08 +08004611is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4612{
4613 if (es->configure == black_surface_configure) {
4614 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004615 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004616 return true;
4617 }
4618 return false;
4619}
4620
Kristian Høgsberg75840622011-09-06 13:48:16 -04004621static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004622activate_binding(struct weston_seat *seat,
4623 struct desktop_shell *shell,
4624 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004625{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004626 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004627
Alex Wu9c35e6b2012-03-05 14:13:13 +08004628 if (!focus)
4629 return;
4630
Pekka Paalanen01388e22013-04-25 13:57:44 +03004631 if (is_black_surface(focus, &main_surface))
4632 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004633
Pekka Paalanen01388e22013-04-25 13:57:44 +03004634 main_surface = weston_surface_get_main_surface(focus);
4635 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004636 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004637
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004638 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004639}
4640
4641static void
4642click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4643 void *data)
4644{
4645 if (seat->pointer->grab != &seat->pointer->default_grab)
4646 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004647 if (seat->pointer->focus == NULL)
4648 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004649
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004650 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004651}
4652
4653static void
4654touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4655{
4656 if (seat->touch->grab != &seat->touch->default_grab)
4657 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004658 if (seat->touch->focus == NULL)
4659 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004660
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004661 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004662}
4663
4664static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004665unfocus_all_seats(struct desktop_shell *shell)
4666{
4667 struct weston_seat *seat, *next;
4668
4669 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4670 if (seat->keyboard == NULL)
4671 continue;
4672
4673 weston_keyboard_set_focus(seat->keyboard, NULL);
4674 }
4675}
4676
4677static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004678lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004679{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004680 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004681
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004682 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004683 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004684 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004685 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004686
4687 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004688
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004689 /* Hide all surfaces by removing the fullscreen, panel and
4690 * toplevel layers. This way nothing else can show or receive
4691 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004692
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004693 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004694 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004695 if (shell->showing_input_panels)
4696 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004697 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004698 wl_list_insert(&shell->compositor->cursor_layer.link,
4699 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004700
Pekka Paalanen77346a62011-11-30 16:26:35 +02004701 launch_screensaver(shell);
4702
Neil Robertsb4a91702014-04-09 16:33:32 +01004703 /* Remove the keyboard focus on all seats. This will be
4704 * restored to the workspace's saved state via
4705 * restore_focus_state when the compositor is unlocked */
4706 unfocus_all_seats(shell);
4707
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004708 /* TODO: disable bindings that should not work while locked. */
4709
4710 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004711}
4712
4713static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004714unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004715{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004716 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004717 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004718 return;
4719 }
4720
4721 /* If desktop-shell client has gone away, unlock immediately. */
4722 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004723 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004724 return;
4725 }
4726
4727 if (shell->prepare_event_sent)
4728 return;
4729
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004730 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004731 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004732}
4733
4734static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004735shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004736{
4737 struct desktop_shell *shell = data;
4738
4739 shell->fade.animation = NULL;
4740
4741 switch (shell->fade.type) {
4742 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004743 weston_surface_destroy(shell->fade.view->surface);
4744 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004745 break;
4746 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004747 lock(shell);
4748 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004749 default:
4750 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004751 }
4752}
4753
Jason Ekstranda7af7042013-10-12 22:38:11 -05004754static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004755shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004756{
4757 struct weston_compositor *compositor = shell->compositor;
4758 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004759 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004760
4761 surface = weston_surface_create(compositor);
4762 if (!surface)
4763 return NULL;
4764
Jason Ekstranda7af7042013-10-12 22:38:11 -05004765 view = weston_view_create(surface);
4766 if (!view) {
4767 weston_surface_destroy(surface);
4768 return NULL;
4769 }
4770
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004771 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004772 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004773 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004774 wl_list_insert(&compositor->fade_layer.view_list,
4775 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004776 pixman_region32_init(&surface->input);
4777
Jason Ekstranda7af7042013-10-12 22:38:11 -05004778 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004779}
4780
4781static void
4782shell_fade(struct desktop_shell *shell, enum fade_type type)
4783{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004784 float tint;
4785
4786 switch (type) {
4787 case FADE_IN:
4788 tint = 0.0;
4789 break;
4790 case FADE_OUT:
4791 tint = 1.0;
4792 break;
4793 default:
4794 weston_log("shell: invalid fade type\n");
4795 return;
4796 }
4797
4798 shell->fade.type = type;
4799
Jason Ekstranda7af7042013-10-12 22:38:11 -05004800 if (shell->fade.view == NULL) {
4801 shell->fade.view = shell_fade_create_surface(shell);
4802 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004803 return;
4804
Jason Ekstranda7af7042013-10-12 22:38:11 -05004805 shell->fade.view->alpha = 1.0 - tint;
4806 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004807 }
4808
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004809 if (shell->fade.view->output == NULL) {
4810 /* If the black view gets a NULL output, we lost the
4811 * last output and we'll just cancel the fade. This
4812 * happens when you close the last window under the
4813 * X11 or Wayland backends. */
4814 shell->locked = false;
4815 weston_surface_destroy(shell->fade.view->surface);
4816 shell->fade.view = NULL;
4817 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004818 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004819 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004820 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004821 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004822 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004823 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004824 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004825}
4826
4827static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004828do_shell_fade_startup(void *data)
4829{
4830 struct desktop_shell *shell = data;
4831
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004832 if (shell->startup_animation_type == ANIMATION_FADE)
4833 shell_fade(shell, FADE_IN);
4834 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004835 weston_surface_destroy(shell->fade.view->surface);
4836 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004837 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004838}
4839
4840static void
4841shell_fade_startup(struct desktop_shell *shell)
4842{
4843 struct wl_event_loop *loop;
4844
4845 if (!shell->fade.startup_timer)
4846 return;
4847
4848 wl_event_source_remove(shell->fade.startup_timer);
4849 shell->fade.startup_timer = NULL;
4850
4851 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4852 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4853}
4854
4855static int
4856fade_startup_timeout(void *data)
4857{
4858 struct desktop_shell *shell = data;
4859
4860 shell_fade_startup(shell);
4861 return 0;
4862}
4863
4864static void
4865shell_fade_init(struct desktop_shell *shell)
4866{
4867 /* Make compositor output all black, and wait for the desktop-shell
4868 * client to signal it is ready, then fade in. The timer triggers a
4869 * fade-in, in case the desktop-shell client takes too long.
4870 */
4871
4872 struct wl_event_loop *loop;
4873
Jason Ekstranda7af7042013-10-12 22:38:11 -05004874 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004875 weston_log("%s: warning: fade surface already exists\n",
4876 __func__);
4877 return;
4878 }
4879
Jason Ekstranda7af7042013-10-12 22:38:11 -05004880 shell->fade.view = shell_fade_create_surface(shell);
4881 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004882 return;
4883
Jason Ekstranda7af7042013-10-12 22:38:11 -05004884 weston_view_update_transform(shell->fade.view);
4885 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004886
4887 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4888 shell->fade.startup_timer =
4889 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4890 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4891}
4892
4893static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004894idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004895{
4896 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004897 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004898 struct weston_seat *seat;
4899
4900 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4901 if (seat->pointer)
4902 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004903
4904 shell_fade(shell, FADE_OUT);
4905 /* lock() is called from shell_fade_done() */
4906}
4907
4908static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004909wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004910{
4911 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004912 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004913
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004914 unlock(shell);
4915}
4916
4917static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004918center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004919{
Giulio Camuffob8366642013-04-25 13:57:46 +03004920 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004921 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004922
Jason Ekstranda7af7042013-10-12 22:38:11 -05004923 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004924
4925 x = output->x + (output->width - width) / 2 - surf_x / 2;
4926 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004927
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004928 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004929}
4930
4931static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004932weston_view_set_initial_position(struct weston_view *view,
4933 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004934{
4935 struct weston_compositor *compositor = shell->compositor;
4936 int ix = 0, iy = 0;
4937 int range_x, range_y;
4938 int dx, dy, x, y, panel_height;
4939 struct weston_output *output, *target_output = NULL;
4940 struct weston_seat *seat;
4941
4942 /* As a heuristic place the new window on the same output as the
4943 * pointer. Falling back to the output containing 0, 0.
4944 *
4945 * TODO: Do something clever for touch too?
4946 */
4947 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004948 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004949 ix = wl_fixed_to_int(seat->pointer->x);
4950 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004951 break;
4952 }
4953 }
4954
4955 wl_list_for_each(output, &compositor->output_list, link) {
4956 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4957 target_output = output;
4958 break;
4959 }
4960 }
4961
4962 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004963 weston_view_set_position(view, 10 + random() % 400,
4964 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004965 return;
4966 }
4967
4968 /* Valid range within output where the surface will still be onscreen.
4969 * If this is negative it means that the surface is bigger than
4970 * output.
4971 */
4972 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004973 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004974 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004975 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004976
Rob Bradford4cb88c72012-08-13 15:18:44 +01004977 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004978 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004979 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004980 dx = 0;
4981
4982 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004983 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004984 else
4985 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004986
4987 x = target_output->x + dx;
4988 y = target_output->y + dy;
4989
Jason Ekstranda7af7042013-10-12 22:38:11 -05004990 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004991}
4992
4993static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004994map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004995 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004996{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004997 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01004998 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08004999 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03005000 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005001
Pekka Paalanen77346a62011-11-30 16:26:35 +02005002 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005003 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005004 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005005 if (shsurf->state.fullscreen) {
5006 center_on_output(shsurf->view, shsurf->fullscreen_output);
5007 shell_map_fullscreen(shsurf);
5008 } else if (shsurf->state.maximized) {
5009 /* use surface configure to set the geometry */
5010 panel_height = get_output_panel_height(shell, shsurf->output);
5011 surface_subsurfaces_boundingbox(shsurf->surface,
5012 &surf_x, &surf_y, NULL, NULL);
5013 weston_view_set_position(shsurf->view,
5014 shsurf->output->x - surf_x,
5015 shsurf->output->y +
5016 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02005017 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005018 weston_view_set_initial_position(shsurf->view, shell);
5019 }
Juan Zhao96879df2012-02-07 08:45:41 +08005020 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005021 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04005022 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00005023 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005024 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005025 weston_view_set_position(shsurf->view,
5026 shsurf->view->geometry.x + sx,
5027 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005028 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005029 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005030 default:
5031 ;
5032 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005033
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005034 /* Surface stacking order, see also activate(). */
5035 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005036
Jason Ekstranda7af7042013-10-12 22:38:11 -05005037 if (shsurf->type != SHELL_SURFACE_NONE) {
5038 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005039 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005040 shsurf->surface->output = shsurf->output;
5041 shsurf->view->output = shsurf->output;
5042 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005043 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005044
Jason Ekstranda7af7042013-10-12 22:38:11 -05005045 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005046 /* XXX: xwayland's using the same fields for transient type */
5047 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005048 if (shsurf->transient.flags ==
5049 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5050 break;
5051 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005052 if (shsurf->state.relative &&
5053 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5054 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005055 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005056 break;
5057 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005058 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005059 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005060 case SHELL_SURFACE_POPUP:
5061 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005062 default:
5063 break;
5064 }
5065
Rafael Antognolli03b16592013-12-03 15:35:42 -02005066 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5067 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005068 {
5069 switch (shell->win_animation_type) {
5070 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005071 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005072 break;
5073 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005074 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005075 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005076 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005077 default:
5078 break;
5079 }
5080 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005081}
5082
5083static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005084configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005085 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005086{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005087 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005088 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005089 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005090
Pekka Paalanen77346a62011-11-30 16:26:35 +02005091 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005092
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005093 assert(shsurf);
5094
Rafael Antognolli03b16592013-12-03 15:35:42 -02005095 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005096 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005097 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08005098 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03005099 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
5100 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005101 mx = shsurf->output->x - surf_x;
5102 my = shsurf->output->y +
5103 get_output_panel_height(shell,shsurf->output) - surf_y;
5104 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005105 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005106 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005107 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005108
Alex Wu4539b082012-03-01 12:57:46 +08005109 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005110 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005111 wl_list_for_each(view, &surface->views, surface_link)
5112 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005113
Rafael Antognolli03b16592013-12-03 15:35:42 -02005114 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005115 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005116 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005117}
5118
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005119static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005120shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005121{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005122 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005123 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005124 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005125
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005126 assert(shsurf);
5127
5128 shell = shsurf->shell;
5129
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005130 if (!weston_surface_is_mapped(es) &&
5131 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005132 remove_popup_grab(shsurf);
5133 }
5134
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005135 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005136 return;
5137
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005138 if (shsurf->has_next_geometry) {
5139 shsurf->geometry = shsurf->next_geometry;
5140 shsurf->has_next_geometry = false;
5141 shsurf->has_set_geometry = true;
5142 } else if (!shsurf->has_set_geometry) {
5143 surface_subsurfaces_boundingbox(shsurf->surface,
5144 &shsurf->geometry.x,
5145 &shsurf->geometry.y,
5146 &shsurf->geometry.width,
5147 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005148 }
5149
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005150 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005151 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005152 type_changed = 1;
5153 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005154
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005155 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005156 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005157 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005158 shsurf->last_width != es->width ||
5159 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005160 float from_x, from_y;
5161 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005162
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005163 if (shsurf->resize_edges) {
5164 sx = 0;
5165 sy = 0;
5166 }
5167
5168 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5169 sx = shsurf->last_width - es->width;
5170 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5171 sy = shsurf->last_height - es->height;
5172
5173 shsurf->last_width = es->width;
5174 shsurf->last_height = es->height;
5175
Jason Ekstranda7af7042013-10-12 22:38:11 -05005176 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5177 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005178 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005179 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005180 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005181 }
5182}
5183
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005184static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005185
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005186static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005187desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005188{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005189 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005190 struct desktop_shell *shell =
5191 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005192
5193 shell->child.process.pid = 0;
5194 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005195
5196 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5197 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005198 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005199 shell->child.deathstamp = time;
5200 shell->child.deathcount = 0;
5201 }
5202
5203 shell->child.deathcount++;
5204 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005205 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005206 return;
5207 }
5208
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005209 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005210 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005211 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005212}
5213
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005214static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005215desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5216{
5217 struct desktop_shell *shell;
5218
5219 shell = container_of(listener, struct desktop_shell,
5220 child.client_destroy_listener);
5221
5222 shell->child.client = NULL;
5223}
5224
5225static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005226launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005227{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005228 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005229
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005230 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005231 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005232 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005233 desktop_shell_sigchld);
5234
5235 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005236 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005237
5238 shell->child.client_destroy_listener.notify =
5239 desktop_shell_client_destroy;
5240 wl_client_add_destroy_listener(shell->child.client,
5241 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005242}
5243
5244static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005245handle_shell_client_destroy(struct wl_listener *listener, void *data)
5246{
5247 struct shell_client *sc =
5248 container_of(listener, struct shell_client, destroy_listener);
5249
5250 if (sc->ping_timer)
5251 wl_event_source_remove(sc->ping_timer);
5252 free(sc);
5253}
5254
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005255static struct shell_client *
5256shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5257 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005258{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005259 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005260
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005261 sc = zalloc(sizeof *sc);
5262 if (sc == NULL) {
5263 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005264 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005265 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005266
5267 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005268 if (sc->resource == NULL) {
5269 free(sc);
5270 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005271 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005272 }
5273
5274 sc->client = client;
5275 sc->shell = shell;
5276 sc->destroy_listener.notify = handle_shell_client_destroy;
5277 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5278
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005279 return sc;
5280}
5281
5282static void
5283bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5284{
5285 struct desktop_shell *shell = data;
5286 struct shell_client *sc;
5287
5288 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5289 if (sc)
5290 wl_resource_set_implementation(sc->resource,
5291 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005292 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005293}
5294
5295static void
5296bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5297{
5298 struct desktop_shell *shell = data;
5299 struct shell_client *sc;
5300
5301 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5302 if (sc)
5303 wl_resource_set_dispatcher(sc->resource,
5304 xdg_shell_unversioned_dispatch,
5305 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005306}
5307
5308static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005309unbind_desktop_shell(struct wl_resource *resource)
5310{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005311 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005312
5313 if (shell->locked)
5314 resume_desktop(shell);
5315
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005316 shell->child.desktop_shell = NULL;
5317 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005318}
5319
5320static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005321bind_desktop_shell(struct wl_client *client,
5322 void *data, uint32_t version, uint32_t id)
5323{
Tiago Vignattibe143262012-04-16 17:31:41 +03005324 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005325 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005326
Jason Ekstranda85118c2013-06-27 20:17:02 -05005327 resource = wl_resource_create(client, &desktop_shell_interface,
5328 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005329
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005330 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005331 wl_resource_set_implementation(resource,
5332 &desktop_shell_implementation,
5333 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005334 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005335
5336 if (version < 2)
5337 shell_fade_startup(shell);
5338
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005339 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005340 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005341
5342 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5343 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005344 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005345}
5346
Pekka Paalanen6e168112011-11-24 11:34:05 +02005347static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005348screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005349{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005350 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005351 struct weston_view *view;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005352
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005353 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005354 return;
5355
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005356 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005357 if (!shell->locked)
5358 return;
5359
Jason Ekstranda7af7042013-10-12 22:38:11 -05005360 view = container_of(surface->views.next, struct weston_view, surface_link);
5361 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005362
Jason Ekstranda7af7042013-10-12 22:38:11 -05005363 if (wl_list_empty(&view->layer_link)) {
5364 wl_list_insert(shell->lock_layer.view_list.prev,
5365 &view->layer_link);
5366 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005367 wl_event_source_timer_update(shell->screensaver.timer,
5368 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005369 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005370 }
5371}
5372
5373static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005374screensaver_set_surface(struct wl_client *client,
5375 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005376 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005377 struct wl_resource *output_resource)
5378{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005379 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005380 struct weston_surface *surface =
5381 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005382 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005383 struct weston_view *view, *next;
5384
5385 /* Make sure we only have one view */
5386 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5387 weston_view_destroy(view);
5388 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005389
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005390 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005391 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005392 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005393}
5394
5395static const struct screensaver_interface screensaver_implementation = {
5396 screensaver_set_surface
5397};
5398
5399static void
5400unbind_screensaver(struct wl_resource *resource)
5401{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005402 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005403
Pekka Paalanen77346a62011-11-30 16:26:35 +02005404 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005405}
5406
5407static void
5408bind_screensaver(struct wl_client *client,
5409 void *data, uint32_t version, uint32_t id)
5410{
Tiago Vignattibe143262012-04-16 17:31:41 +03005411 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005412 struct wl_resource *resource;
5413
Jason Ekstranda85118c2013-06-27 20:17:02 -05005414 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005415
Pekka Paalanen77346a62011-11-30 16:26:35 +02005416 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005417 wl_resource_set_implementation(resource,
5418 &screensaver_implementation,
5419 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005420 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005421 return;
5422 }
5423
5424 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5425 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005426 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005427}
5428
Kristian Høgsberg07045392012-02-19 18:52:44 -05005429struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005430 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005431 struct weston_surface *current;
5432 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005433 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005434 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005435};
5436
5437static void
5438switcher_next(struct switcher *switcher)
5439{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005440 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005441 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005442 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005443 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005444
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005445 /* temporary re-display minimized surfaces */
5446 struct weston_view *tmp;
5447 struct weston_view **minimized;
5448 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5449 wl_list_remove(&view->layer_link);
5450 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5451 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5452 *minimized = view;
5453 }
5454
Jason Ekstranda7af7042013-10-12 22:38:11 -05005455 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005456 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005457 if (shsurf &&
5458 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5459 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005460 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005461 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005462 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005463 next = view->surface;
5464 prev = view->surface;
5465 view->alpha = 0.25;
5466 weston_view_geometry_dirty(view);
5467 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005468 }
Alex Wu1659daa2012-04-01 20:13:09 +08005469
Jason Ekstranda7af7042013-10-12 22:38:11 -05005470 if (is_black_surface(view->surface, NULL)) {
5471 view->alpha = 0.25;
5472 weston_view_geometry_dirty(view);
5473 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005474 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005475 }
5476
5477 if (next == NULL)
5478 next = first;
5479
Alex Wu07b26062012-03-12 16:06:01 +08005480 if (next == NULL)
5481 return;
5482
Kristian Høgsberg07045392012-02-19 18:52:44 -05005483 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005484 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005485
5486 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005487 wl_list_for_each(view, &next->views, surface_link)
5488 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005489
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005490 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005491 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005492 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005493}
5494
5495static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005496switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005497{
5498 struct switcher *switcher =
5499 container_of(listener, struct switcher, listener);
5500
5501 switcher_next(switcher);
5502}
5503
5504static void
Daniel Stone351eb612012-05-31 15:27:47 -04005505switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005506{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005507 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005508 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005509 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005510
Jason Ekstranda7af7042013-10-12 22:38:11 -05005511 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005512 if (is_focus_view(view))
5513 continue;
5514
Jason Ekstranda7af7042013-10-12 22:38:11 -05005515 view->alpha = 1.0;
5516 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005517 }
5518
Alex Wu07b26062012-03-12 16:06:01 +08005519 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005520 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005521 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005522 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005523 weston_keyboard_end_grab(keyboard);
5524 if (keyboard->input_method_resource)
5525 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005526
5527 /* re-hide surfaces that were temporary shown during the switch */
5528 struct weston_view **minimized;
5529 wl_array_for_each(minimized, &switcher->minimized_array) {
5530 /* with the exception of the current selected */
5531 if ((*minimized)->surface != switcher->current) {
5532 wl_list_remove(&(*minimized)->layer_link);
5533 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5534 weston_view_damage_below(*minimized);
5535 }
5536 }
5537 wl_array_release(&switcher->minimized_array);
5538
Kristian Høgsberg07045392012-02-19 18:52:44 -05005539 free(switcher);
5540}
5541
5542static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005543switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005544 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005545{
5546 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005547 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005548
Daniel Stonec9785ea2012-05-30 16:31:52 +01005549 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005550 switcher_next(switcher);
5551}
5552
5553static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005554switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005555 uint32_t mods_depressed, uint32_t mods_latched,
5556 uint32_t mods_locked, uint32_t group)
5557{
5558 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005559 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005560
Daniel Stone351eb612012-05-31 15:27:47 -04005561 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5562 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005563}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005564
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005565static void
5566switcher_cancel(struct weston_keyboard_grab *grab)
5567{
5568 struct switcher *switcher = container_of(grab, struct switcher, grab);
5569
5570 switcher_destroy(switcher);
5571}
5572
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005573static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005574 switcher_key,
5575 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005576 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005577};
5578
5579static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005580switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005581 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005582{
Tiago Vignattibe143262012-04-16 17:31:41 +03005583 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005584 struct switcher *switcher;
5585
5586 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005587 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005588 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005589 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005590 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005591 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005592
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005593 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005594 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005595 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005596 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5597 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005598 switcher_next(switcher);
5599}
5600
Pekka Paalanen3c647232011-12-22 13:43:43 +02005601static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005602backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005603 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005604{
5605 struct weston_compositor *compositor = data;
5606 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005607 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005608
5609 /* TODO: we're limiting to simple use cases, where we assume just
5610 * control on the primary display. We'd have to extend later if we
5611 * ever get support for setting backlights on random desktop LCD
5612 * panels though */
5613 output = get_default_output(compositor);
5614 if (!output)
5615 return;
5616
5617 if (!output->set_backlight)
5618 return;
5619
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005620 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5621 backlight_new = output->backlight_current - 25;
5622 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5623 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005624
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005625 if (backlight_new < 5)
5626 backlight_new = 5;
5627 if (backlight_new > 255)
5628 backlight_new = 255;
5629
5630 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005631 output->set_backlight(output, output->backlight_current);
5632}
5633
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005634struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005635 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005636 struct weston_seat *seat;
5637 uint32_t key[2];
5638 int key_released[2];
5639};
5640
5641static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005642debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005643 uint32_t key, uint32_t state)
5644{
5645 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005646 struct weston_compositor *ec = db->seat->compositor;
5647 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005648 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005649 uint32_t serial;
5650 int send = 0, terminate = 0;
5651 int check_binding = 1;
5652 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005653 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005654
5655 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5656 /* Do not run bindings on key releases */
5657 check_binding = 0;
5658
5659 for (i = 0; i < 2; i++)
5660 if (key == db->key[i])
5661 db->key_released[i] = 1;
5662
5663 if (db->key_released[0] && db->key_released[1]) {
5664 /* All key releases been swalled so end the grab */
5665 terminate = 1;
5666 } else if (key != db->key[0] && key != db->key[1]) {
5667 /* Should not swallow release of other keys */
5668 send = 1;
5669 }
5670 } else if (key == db->key[0] && !db->key_released[0]) {
5671 /* Do not check bindings for the first press of the binding
5672 * key. This allows it to be used as a debug shortcut.
5673 * We still need to swallow this event. */
5674 check_binding = 0;
5675 } else if (db->key[1]) {
5676 /* If we already ran a binding don't process another one since
5677 * we can't keep track of all the binding keys that were
5678 * pressed in order to swallow the release events. */
5679 send = 1;
5680 check_binding = 0;
5681 }
5682
5683 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005684 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5685 key, state)) {
5686 /* We ran a binding so swallow the press and keep the
5687 * grab to swallow the released too. */
5688 send = 0;
5689 terminate = 0;
5690 db->key[1] = key;
5691 } else {
5692 /* Terminate the grab since the key pressed is not a
5693 * debug binding key. */
5694 send = 1;
5695 terminate = 1;
5696 }
5697 }
5698
5699 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005700 serial = wl_display_next_serial(display);
5701 resource_list = &grab->keyboard->focus_resource_list;
5702 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005703 wl_keyboard_send_key(resource, serial, time, key, state);
5704 }
5705 }
5706
5707 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005708 weston_keyboard_end_grab(grab->keyboard);
5709 if (grab->keyboard->input_method_resource)
5710 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005711 free(db);
5712 }
5713}
5714
5715static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005716debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005717 uint32_t mods_depressed, uint32_t mods_latched,
5718 uint32_t mods_locked, uint32_t group)
5719{
5720 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005721 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005722
Neil Roberts96d790e2013-09-19 17:32:00 +01005723 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005724
Neil Roberts96d790e2013-09-19 17:32:00 +01005725 wl_resource_for_each(resource, resource_list) {
5726 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5727 mods_latched, mods_locked, group);
5728 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005729}
5730
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005731static void
5732debug_binding_cancel(struct weston_keyboard_grab *grab)
5733{
5734 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5735
5736 weston_keyboard_end_grab(grab->keyboard);
5737 free(db);
5738}
5739
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005740struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005741 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005742 debug_binding_modifiers,
5743 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005744};
5745
5746static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005747debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005748{
5749 struct debug_binding_grab *grab;
5750
5751 grab = calloc(1, sizeof *grab);
5752 if (!grab)
5753 return;
5754
5755 grab->seat = (struct weston_seat *) seat;
5756 grab->key[0] = key;
5757 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005758 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005759}
5760
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005761static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005762force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005763 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005764{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005765 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005766 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005767 struct desktop_shell *shell = data;
5768 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005769 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005770
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005771 focus_surface = seat->keyboard->focus;
5772 if (!focus_surface)
5773 return;
5774
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005775 wl_signal_emit(&compositor->kill_signal, focus_surface);
5776
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005777 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005778 wl_client_get_credentials(client, &pid, NULL, NULL);
5779
5780 /* Skip clients that we launched ourselves (the credentials of
5781 * the socketpair is ours) */
5782 if (pid == getpid())
5783 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005784
Daniel Stone325fc2d2012-05-30 16:31:58 +01005785 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005786}
5787
5788static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005789workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005790 uint32_t key, void *data)
5791{
5792 struct desktop_shell *shell = data;
5793 unsigned int new_index = shell->workspaces.current;
5794
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005795 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005796 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005797 if (new_index != 0)
5798 new_index--;
5799
5800 change_workspace(shell, new_index);
5801}
5802
5803static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005804workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005805 uint32_t key, void *data)
5806{
5807 struct desktop_shell *shell = data;
5808 unsigned int new_index = shell->workspaces.current;
5809
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005810 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005811 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005812 if (new_index < shell->workspaces.num - 1)
5813 new_index++;
5814
5815 change_workspace(shell, new_index);
5816}
5817
5818static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005819workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005820 uint32_t key, void *data)
5821{
5822 struct desktop_shell *shell = data;
5823 unsigned int new_index;
5824
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005825 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005826 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005827 new_index = key - KEY_F1;
5828 if (new_index >= shell->workspaces.num)
5829 new_index = shell->workspaces.num - 1;
5830
5831 change_workspace(shell, new_index);
5832}
5833
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005834static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005835workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005836 uint32_t key, void *data)
5837{
5838 struct desktop_shell *shell = data;
5839 unsigned int new_index = shell->workspaces.current;
5840
5841 if (shell->locked)
5842 return;
5843
5844 if (new_index != 0)
5845 new_index--;
5846
5847 take_surface_to_workspace_by_seat(shell, seat, new_index);
5848}
5849
5850static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005851workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005852 uint32_t key, void *data)
5853{
5854 struct desktop_shell *shell = data;
5855 unsigned int new_index = shell->workspaces.current;
5856
5857 if (shell->locked)
5858 return;
5859
5860 if (new_index < shell->workspaces.num - 1)
5861 new_index++;
5862
5863 take_surface_to_workspace_by_seat(shell, seat, new_index);
5864}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005865
5866static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005867shell_reposition_view_on_output_destroy(struct weston_view *view)
5868{
5869 struct weston_output *output, *first_output;
5870 struct weston_compositor *ec = view->surface->compositor;
5871 struct shell_surface *shsurf;
5872 float x, y;
5873 int visible;
5874
5875 x = view->geometry.x;
5876 y = view->geometry.y;
5877
5878 /* At this point the destroyed output is not in the list anymore.
5879 * If the view is still visible somewhere, we leave where it is,
5880 * otherwise, move it to the first output. */
5881 visible = 0;
5882 wl_list_for_each(output, &ec->output_list, link) {
5883 if (pixman_region32_contains_point(&output->region,
5884 x, y, NULL)) {
5885 visible = 1;
5886 break;
5887 }
5888 }
5889
5890 if (!visible) {
5891 first_output = container_of(ec->output_list.next,
5892 struct weston_output, link);
5893
5894 x = first_output->x + first_output->width / 4;
5895 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08005896
5897 weston_view_set_position(view, x, y);
5898 } else {
5899 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005900 }
5901
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005902
5903 shsurf = get_shell_surface(view->surface);
5904
5905 if (shsurf) {
5906 shsurf->saved_position_valid = false;
5907 shsurf->next_state.maximized = false;
5908 shsurf->next_state.fullscreen = false;
5909 shsurf->state_changed = true;
5910 }
5911}
5912
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005913void
5914shell_for_each_layer(struct desktop_shell *shell,
5915 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005916{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005917 struct workspace **ws;
5918
5919 func(shell, &shell->fullscreen_layer, data);
5920 func(shell, &shell->panel_layer, data);
5921 func(shell, &shell->background_layer, data);
5922 func(shell, &shell->lock_layer, data);
5923 func(shell, &shell->input_panel_layer, data);
5924
5925 wl_array_for_each(ws, &shell->workspaces.array)
5926 func(shell, &(*ws)->layer, data);
5927}
5928
5929static void
5930shell_output_destroy_move_layer(struct desktop_shell *shell,
5931 struct weston_layer *layer,
5932 void *data)
5933{
5934 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005935 struct weston_view *view;
5936
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005937 wl_list_for_each(view, &layer->view_list, layer_link) {
5938 if (view->output != output)
5939 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005940
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005941 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005942 }
5943}
5944
5945static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005946handle_output_destroy(struct wl_listener *listener, void *data)
5947{
5948 struct shell_output *output_listener =
5949 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005950 struct weston_output *output = output_listener->output;
5951 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005952
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005953 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005954
Xiong Zhang6b481422013-10-23 13:58:32 +08005955 wl_list_remove(&output_listener->destroy_listener.link);
5956 wl_list_remove(&output_listener->link);
5957 free(output_listener);
5958}
5959
5960static void
5961create_shell_output(struct desktop_shell *shell,
5962 struct weston_output *output)
5963{
5964 struct shell_output *shell_output;
5965
5966 shell_output = zalloc(sizeof *shell_output);
5967 if (shell_output == NULL)
5968 return;
5969
5970 shell_output->output = output;
5971 shell_output->shell = shell;
5972 shell_output->destroy_listener.notify = handle_output_destroy;
5973 wl_signal_add(&output->destroy_signal,
5974 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005975 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005976}
5977
5978static void
5979handle_output_create(struct wl_listener *listener, void *data)
5980{
5981 struct desktop_shell *shell =
5982 container_of(listener, struct desktop_shell, output_create_listener);
5983 struct weston_output *output = (struct weston_output *)data;
5984
5985 create_shell_output(shell, output);
5986}
5987
5988static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005989handle_output_move_layer(struct desktop_shell *shell,
5990 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005991{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005992 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005993 struct weston_view *view;
5994 float x, y;
5995
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005996 wl_list_for_each(view, &layer->view_list, layer_link) {
5997 if (view->output != output)
5998 continue;
5999
6000 x = view->geometry.x + output->move_x;
6001 y = view->geometry.y + output->move_y;
6002 weston_view_set_position(view, x, y);
6003 }
6004}
6005
6006static void
6007handle_output_move(struct wl_listener *listener, void *data)
6008{
6009 struct desktop_shell *shell;
6010
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006011 shell = container_of(listener, struct desktop_shell,
6012 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006013
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006014 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006015}
6016
6017static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006018setup_output_destroy_handler(struct weston_compositor *ec,
6019 struct desktop_shell *shell)
6020{
6021 struct weston_output *output;
6022
6023 wl_list_init(&shell->output_list);
6024 wl_list_for_each(output, &ec->output_list, link)
6025 create_shell_output(shell, output);
6026
6027 shell->output_create_listener.notify = handle_output_create;
6028 wl_signal_add(&ec->output_created_signal,
6029 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006030
6031 shell->output_move_listener.notify = handle_output_move;
6032 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006033}
6034
6035static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006036shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006037{
Tiago Vignattibe143262012-04-16 17:31:41 +03006038 struct desktop_shell *shell =
6039 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006040 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006041 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006042
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006043 /* Force state to unlocked so we don't try to fade */
6044 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006045 if (shell->child.client)
6046 wl_client_destroy(shell->child.client);
6047
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006048 wl_list_remove(&shell->idle_listener.link);
6049 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006050
6051 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006052
Xiong Zhang6b481422013-10-23 13:58:32 +08006053 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6054 wl_list_remove(&shell_output->destroy_listener.link);
6055 wl_list_remove(&shell_output->link);
6056 free(shell_output);
6057 }
6058
6059 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006060 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006061
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006062 wl_array_for_each(ws, &shell->workspaces.array)
6063 workspace_destroy(*ws);
6064 wl_array_release(&shell->workspaces.array);
6065
Pekka Paalanen3c647232011-12-22 13:43:43 +02006066 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006067 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006068 free(shell);
6069}
6070
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006071static void
6072shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6073{
6074 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006075 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006076
6077 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006078 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6079 MODIFIER_CTRL | MODIFIER_ALT,
6080 terminate_binding, ec);
6081 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6082 click_to_activate_binding,
6083 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006084 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6085 click_to_activate_binding,
6086 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006087 weston_compositor_add_touch_binding(ec, 0,
6088 touch_to_activate_binding,
6089 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006090 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6091 MODIFIER_SUPER | MODIFIER_ALT,
6092 surface_opacity_binding, NULL);
6093 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6094 MODIFIER_SUPER, zoom_axis_binding,
6095 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006096
6097 /* configurable bindings */
6098 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006099 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6100 zoom_key_binding, NULL);
6101 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6102 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006103 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6104 maximize_binding, NULL);
6105 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6106 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006107 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6108 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006109 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006110 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6111 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006112 weston_compositor_add_button_binding(ec, BTN_LEFT,
6113 mod | MODIFIER_SHIFT,
6114 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006115
6116 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6117 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6118 rotate_binding, NULL);
6119
Daniel Stone325fc2d2012-05-30 16:31:58 +01006120 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6121 shell);
6122 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6123 ec);
6124 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6125 backlight_binding, ec);
6126 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6127 ec);
6128 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6129 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006130 weston_compositor_add_key_binding(ec, KEY_K, mod,
6131 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006132 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6133 workspace_up_binding, shell);
6134 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6135 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006136 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6137 workspace_move_surface_up_binding,
6138 shell);
6139 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6140 workspace_move_surface_down_binding,
6141 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006142
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006143 if (shell->exposay_modifier)
6144 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6145 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006146
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006147 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6148 if (shell->workspaces.num > 1) {
6149 num_workspace_bindings = shell->workspaces.num;
6150 if (num_workspace_bindings > 6)
6151 num_workspace_bindings = 6;
6152 for (i = 0; i < num_workspace_bindings; i++)
6153 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6154 workspace_f_binding,
6155 shell);
6156 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006157
6158 /* Debug bindings */
6159 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6160 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006161}
6162
Jason Ekstrand024177c2014-04-21 19:42:58 -05006163static void
6164handle_seat_created(struct wl_listener *listener, void *data)
6165{
6166 struct weston_seat *seat = data;
6167
6168 create_shell_seat(seat);
6169}
6170
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006171WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006172module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006173 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006174{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006175 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006176 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006177 struct workspace **pws;
6178 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006179 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006180
Peter Huttererf3d62272013-08-08 11:57:05 +10006181 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006182 if (shell == NULL)
6183 return -1;
6184
Kristian Høgsberg75840622011-09-06 13:48:16 -04006185 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006186
6187 shell->destroy_listener.notify = shell_destroy;
6188 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006189 shell->idle_listener.notify = idle_handler;
6190 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6191 shell->wake_listener.notify = wake_handler;
6192 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006193
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006194 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006195 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006196 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006197 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006198 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006199 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006200 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006201 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006202 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006203 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006204 ec->shell_interface.set_window_geometry = set_window_geometry;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006205
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006206 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6207 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006208 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6209 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006210 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006211
6212 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006213 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006214
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006215 if (input_panel_setup(shell) < 0)
6216 return -1;
6217
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006218 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006219
Daniel Stonedf8133b2013-11-19 11:37:14 +01006220 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6221 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6222
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006223 for (i = 0; i < shell->workspaces.num; i++) {
6224 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6225 if (pws == NULL)
6226 return -1;
6227
6228 *pws = workspace_create();
6229 if (*pws == NULL)
6230 return -1;
6231 }
6232 activate_workspace(shell, 0);
6233
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006234 weston_layer_init(&shell->minimized_layer, NULL);
6235
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006236 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006237 wl_list_init(&shell->workspaces.animation.link);
6238 shell->workspaces.animation.frame = animate_workspace_change_frame;
6239
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006240 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006241 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006242 return -1;
6243
Rafael Antognollie2a34552013-12-03 15:35:45 -02006244 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6245 shell, bind_xdg_shell) == NULL)
6246 return -1;
6247
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006248 if (wl_global_create(ec->wl_display,
6249 &desktop_shell_interface, 2,
6250 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006251 return -1;
6252
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006253 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6254 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006255 return -1;
6256
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006257 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6258 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006259 return -1;
6260
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006261 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006262
Xiong Zhang6b481422013-10-23 13:58:32 +08006263 setup_output_destroy_handler(ec, shell);
6264
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006265 loop = wl_display_get_event_loop(ec->wl_display);
6266 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006267
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006268 shell->screensaver.timer =
6269 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6270
Jason Ekstrand024177c2014-04-21 19:42:58 -05006271 wl_list_for_each(seat, &ec->seat_list, link)
6272 handle_seat_created(NULL, seat);
6273 shell->seat_create_listener.notify = handle_seat_created;
6274 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006275
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006276 screenshooter_create(ec);
6277
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006278 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006279
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006280 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006281
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006282 return 0;
6283}