blob: ec72287446c06662e8cf94cb1ba395cd9ddb26ba [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
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300282 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
Alex Wubd3354b2012-04-17 17:20:49 +0800283 return false;
284
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300285 top_fs_ev = container_of(shell->fullscreen_layer.view_list.link.next,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500286 struct weston_view,
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300287 layer_link.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
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300364 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700365 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);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200538 weston_config_section_get_string(section, "close-animation", &s, "fade");
539 shell->win_close_animation_type = get_animation_type(s);
540 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700541 weston_config_section_get_string(section,
542 "startup-animation", &s, "fade");
543 shell->startup_animation_type = get_animation_type(s);
544 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700545 if (shell->startup_animation_type == ANIMATION_ZOOM)
546 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100547 weston_config_section_get_string(section, "focus-animation", &s, "none");
548 shell->focus_animation_type = get_animation_type(s);
549 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400550 weston_config_section_get_uint(section, "num-workspaces",
551 &shell->workspaces.num,
552 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200553}
554
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800555struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100556get_default_output(struct weston_compositor *compositor)
557{
558 return container_of(compositor->output_list.next,
559 struct weston_output, link);
560}
561
562
563/* no-op func for checking focus surface */
564static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600565focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100566{
567}
568
569static struct focus_surface *
570get_focus_surface(struct weston_surface *surface)
571{
572 if (surface->configure == focus_surface_configure)
573 return surface->configure_private;
574 else
575 return NULL;
576}
577
578static bool
579is_focus_surface (struct weston_surface *es)
580{
581 return (es->configure == focus_surface_configure);
582}
583
584static bool
585is_focus_view (struct weston_view *view)
586{
587 return is_focus_surface (view->surface);
588}
589
590static struct focus_surface *
591create_focus_surface(struct weston_compositor *ec,
592 struct weston_output *output)
593{
594 struct focus_surface *fsurf = NULL;
595 struct weston_surface *surface = NULL;
596
597 fsurf = malloc(sizeof *fsurf);
598 if (!fsurf)
599 return NULL;
600
601 fsurf->surface = weston_surface_create(ec);
602 surface = fsurf->surface;
603 if (surface == NULL) {
604 free(fsurf);
605 return NULL;
606 }
607
608 surface->configure = focus_surface_configure;
609 surface->output = output;
610 surface->configure_private = fsurf;
611
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800612 fsurf->view = weston_view_create(surface);
613 if (fsurf->view == NULL) {
614 weston_surface_destroy(surface);
615 free(fsurf);
616 return NULL;
617 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100618 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100619
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600620 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600621 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100622 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
623 pixman_region32_fini(&surface->opaque);
624 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
625 output->width, output->height);
626 pixman_region32_fini(&surface->input);
627 pixman_region32_init(&surface->input);
628
629 wl_list_init(&fsurf->workspace_transform.link);
630
631 return fsurf;
632}
633
634static void
635focus_surface_destroy(struct focus_surface *fsurf)
636{
637 weston_surface_destroy(fsurf->surface);
638 free(fsurf);
639}
640
641static void
642focus_animation_done(struct weston_view_animation *animation, void *data)
643{
644 struct workspace *ws = data;
645
646 ws->focus_animation = NULL;
647}
648
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200649static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200650focus_state_destroy(struct focus_state *state)
651{
652 wl_list_remove(&state->seat_destroy_listener.link);
653 wl_list_remove(&state->surface_destroy_listener.link);
654 free(state);
655}
656
657static void
658focus_state_seat_destroy(struct wl_listener *listener, void *data)
659{
660 struct focus_state *state = container_of(listener,
661 struct focus_state,
662 seat_destroy_listener);
663
664 wl_list_remove(&state->link);
665 focus_state_destroy(state);
666}
667
668static void
669focus_state_surface_destroy(struct wl_listener *listener, void *data)
670{
671 struct focus_state *state = container_of(listener,
672 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400673 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400674 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500675 struct weston_surface *main_surface, *next;
676 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200677
Pekka Paalanen01388e22013-04-25 13:57:44 +0300678 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
679
Kristian Høgsberge3778222012-07-31 17:29:30 -0400680 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300681 wl_list_for_each(view,
682 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500683 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400684 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100685 if (is_focus_view(view))
686 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400687
Jason Ekstranda7af7042013-10-12 22:38:11 -0500688 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400689 break;
690 }
691
Pekka Paalanen01388e22013-04-25 13:57:44 +0300692 /* if the focus was a sub-surface, activate its main surface */
693 if (main_surface != state->keyboard_focus)
694 next = main_surface;
695
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100696 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400697 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100698 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100699 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400700 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100701 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
702 if (state->ws->focus_animation)
703 weston_view_animation_destroy(state->ws->focus_animation);
704
705 state->ws->focus_animation = weston_fade_run(
706 state->ws->fsurf_front->view,
707 state->ws->fsurf_front->view->alpha, 0.0, 300,
708 focus_animation_done, state->ws);
709 }
710
Kristian Høgsberge3778222012-07-31 17:29:30 -0400711 wl_list_remove(&state->link);
712 focus_state_destroy(state);
713 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200714}
715
716static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400717focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200718{
Jonas Ådahl04769742012-06-13 00:01:24 +0200719 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200720
721 state = malloc(sizeof *state);
722 if (state == NULL)
723 return NULL;
724
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100725 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400726 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200727 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400728 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200729
730 state->seat_destroy_listener.notify = focus_state_seat_destroy;
731 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400732 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200733 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400734 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200735
736 return state;
737}
738
Jonas Ådahl8538b222012-08-29 22:13:03 +0200739static struct focus_state *
740ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
741{
742 struct workspace *ws = get_current_workspace(shell);
743 struct focus_state *state;
744
745 wl_list_for_each(state, &ws->focus_list, link)
746 if (state->seat == seat)
747 break;
748
749 if (&state->link == &ws->focus_list)
750 state = focus_state_create(seat, ws);
751
752 return state;
753}
754
Jonas Ådahl04769742012-06-13 00:01:24 +0200755static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800756focus_state_set_focus(struct focus_state *state,
757 struct weston_surface *surface)
758{
759 if (state->keyboard_focus) {
760 wl_list_remove(&state->surface_destroy_listener.link);
761 wl_list_init(&state->surface_destroy_listener.link);
762 }
763
764 state->keyboard_focus = surface;
765 if (surface)
766 wl_signal_add(&surface->destroy_signal,
767 &state->surface_destroy_listener);
768}
769
770static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400771restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200772{
773 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400774 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100775 struct wl_list pending_seat_list;
776 struct weston_seat *seat, *next_seat;
777
778 /* Temporarily steal the list of seats so that we can keep
779 * track of the seats we've already processed */
780 wl_list_init(&pending_seat_list);
781 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
782 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200783
784 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100785 wl_list_remove(&state->seat->link);
786 wl_list_insert(&shell->compositor->seat_list,
787 &state->seat->link);
788
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800789 if (state->seat->keyboard == NULL)
790 continue;
791
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400792 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200793
Kristian Høgsberge3148752013-05-06 23:19:49 -0400794 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200795 }
Neil Roberts4237f502014-04-09 16:33:31 +0100796
797 /* For any remaining seats that we don't have a focus state
798 * for we'll reset the keyboard focus to NULL */
799 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
800 wl_list_insert(&shell->compositor->seat_list, &seat->link);
801
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300802 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100803 continue;
804
805 weston_keyboard_set_focus(seat->keyboard, NULL);
806 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200807}
808
809static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200810replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
811 struct weston_seat *seat)
812{
813 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200814
815 wl_list_for_each(state, &ws->focus_list, link) {
816 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800817 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200818 return;
819 }
820 }
821}
822
823static void
824drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
825 struct weston_surface *surface)
826{
827 struct focus_state *state;
828
829 wl_list_for_each(state, &ws->focus_list, link)
830 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800831 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200832}
833
834static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100835animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
836 struct weston_view *from, struct weston_view *to)
837{
838 struct weston_output *output;
839 bool focus_surface_created = false;
840
841 /* FIXME: Only support dim animation using two layers */
842 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
843 return;
844
845 output = get_default_output(shell->compositor);
846 if (ws->fsurf_front == NULL && (from || to)) {
847 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800848 if (ws->fsurf_front == NULL)
849 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100850 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800851
852 ws->fsurf_back = create_focus_surface(shell->compositor, output);
853 if (ws->fsurf_back == NULL) {
854 focus_surface_destroy(ws->fsurf_front);
855 return;
856 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100857 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800858
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100859 focus_surface_created = true;
860 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300861 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
862 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100863 }
864
865 if (ws->focus_animation) {
866 weston_view_animation_destroy(ws->focus_animation);
867 ws->focus_animation = NULL;
868 }
869
870 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300871 weston_layer_entry_insert(&to->layer_link,
872 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100873 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300874 weston_layer_entry_insert(&ws->layer.view_list,
875 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100876
877 if (focus_surface_created) {
878 ws->focus_animation = weston_fade_run(
879 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200880 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100881 focus_animation_done, ws);
882 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300883 weston_layer_entry_insert(&from->layer_link,
884 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100885 ws->focus_animation = weston_stable_fade_run(
886 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200887 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100888 focus_animation_done, ws);
889 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300890 weston_layer_entry_insert(&ws->layer.view_list,
891 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100892 ws->focus_animation = weston_stable_fade_run(
893 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200894 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100895 focus_animation_done, ws);
896 }
897}
898
899static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200900workspace_destroy(struct workspace *ws)
901{
Jonas Ådahl04769742012-06-13 00:01:24 +0200902 struct focus_state *state, *next;
903
904 wl_list_for_each_safe(state, next, &ws->focus_list, link)
905 focus_state_destroy(state);
906
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100907 if (ws->fsurf_front)
908 focus_surface_destroy(ws->fsurf_front);
909 if (ws->fsurf_back)
910 focus_surface_destroy(ws->fsurf_back);
911
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200912 free(ws);
913}
914
Jonas Ådahl04769742012-06-13 00:01:24 +0200915static void
916seat_destroyed(struct wl_listener *listener, void *data)
917{
918 struct weston_seat *seat = data;
919 struct focus_state *state, *next;
920 struct workspace *ws = container_of(listener,
921 struct workspace,
922 seat_destroyed_listener);
923
924 wl_list_for_each_safe(state, next, &ws->focus_list, link)
925 if (state->seat == seat)
926 wl_list_remove(&state->link);
927}
928
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200929static struct workspace *
930workspace_create(void)
931{
932 struct workspace *ws = malloc(sizeof *ws);
933 if (ws == NULL)
934 return NULL;
935
936 weston_layer_init(&ws->layer, NULL);
937
Jonas Ådahl04769742012-06-13 00:01:24 +0200938 wl_list_init(&ws->focus_list);
939 wl_list_init(&ws->seat_destroyed_listener.link);
940 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100941 ws->fsurf_front = NULL;
942 ws->fsurf_back = NULL;
943 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200944
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200945 return ws;
946}
947
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200948static int
949workspace_is_empty(struct workspace *ws)
950{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300951 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200952}
953
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200954static struct workspace *
955get_workspace(struct desktop_shell *shell, unsigned int index)
956{
957 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200958 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200959 pws += index;
960 return *pws;
961}
962
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800963struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200964get_current_workspace(struct desktop_shell *shell)
965{
966 return get_workspace(shell, shell->workspaces.current);
967}
968
969static void
970activate_workspace(struct desktop_shell *shell, unsigned int index)
971{
972 struct workspace *ws;
973
974 ws = get_workspace(shell, index);
975 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
976
977 shell->workspaces.current = index;
978}
979
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200980static unsigned int
981get_output_height(struct weston_output *output)
982{
983 return abs(output->region.extents.y1 - output->region.extents.y2);
984}
985
986static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100987view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200988{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200989 struct weston_transform *transform;
990
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100991 if (is_focus_view(view)) {
992 struct focus_surface *fsurf = get_focus_surface(view->surface);
993 transform = &fsurf->workspace_transform;
994 } else {
995 struct shell_surface *shsurf = get_shell_surface(view->surface);
996 transform = &shsurf->workspace_transform;
997 }
998
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200999 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001000 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001001 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001002
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001003 weston_matrix_init(&transform->matrix);
1004 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001005 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001006 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001007}
1008
1009static void
1010workspace_translate_out(struct workspace *ws, double fraction)
1011{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001013 unsigned int height;
1014 double d;
1015
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001016 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001017 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001018 d = height * fraction;
1019
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001020 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001021 }
1022}
1023
1024static void
1025workspace_translate_in(struct workspace *ws, double fraction)
1026{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001028 unsigned int height;
1029 double d;
1030
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001031 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001033
1034 if (fraction > 0)
1035 d = -(height - height * fraction);
1036 else
1037 d = height + height * fraction;
1038
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001039 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001040 }
1041}
1042
1043static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001044broadcast_current_workspace_state(struct desktop_shell *shell)
1045{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001046 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001047
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001048 wl_resource_for_each(resource, &shell->workspaces.client_list)
1049 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001050 shell->workspaces.current,
1051 shell->workspaces.num);
1052}
1053
1054static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001055reverse_workspace_change_animation(struct desktop_shell *shell,
1056 unsigned int index,
1057 struct workspace *from,
1058 struct workspace *to)
1059{
1060 shell->workspaces.current = index;
1061
1062 shell->workspaces.anim_to = to;
1063 shell->workspaces.anim_from = from;
1064 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1065 shell->workspaces.anim_timestamp = 0;
1066
Scott Moreau4272e632012-08-13 09:58:41 -06001067 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001068}
1069
1070static void
1071workspace_deactivate_transforms(struct workspace *ws)
1072{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001073 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001074 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001075
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001076 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001077 if (is_focus_view(view)) {
1078 struct focus_surface *fsurf = get_focus_surface(view->surface);
1079 transform = &fsurf->workspace_transform;
1080 } else {
1081 struct shell_surface *shsurf = get_shell_surface(view->surface);
1082 transform = &shsurf->workspace_transform;
1083 }
1084
1085 if (!wl_list_empty(&transform->link)) {
1086 wl_list_remove(&transform->link);
1087 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001088 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001090 }
1091}
1092
1093static void
1094finish_workspace_change_animation(struct desktop_shell *shell,
1095 struct workspace *from,
1096 struct workspace *to)
1097{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001098 struct weston_view *view;
1099
Scott Moreau4272e632012-08-13 09:58:41 -06001100 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001101
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001102 /* Views that extend past the bottom of the output are still
1103 * visible after the workspace animation ends but before its layer
1104 * is hidden. In that case, we need to damage below those views so
1105 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001106 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001107 weston_view_damage_below(view);
1108
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001109 wl_list_remove(&shell->workspaces.animation.link);
1110 workspace_deactivate_transforms(from);
1111 workspace_deactivate_transforms(to);
1112 shell->workspaces.anim_to = NULL;
1113
1114 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1115}
1116
1117static void
1118animate_workspace_change_frame(struct weston_animation *animation,
1119 struct weston_output *output, uint32_t msecs)
1120{
1121 struct desktop_shell *shell =
1122 container_of(animation, struct desktop_shell,
1123 workspaces.animation);
1124 struct workspace *from = shell->workspaces.anim_from;
1125 struct workspace *to = shell->workspaces.anim_to;
1126 uint32_t t;
1127 double x, y;
1128
1129 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1130 finish_workspace_change_animation(shell, from, to);
1131 return;
1132 }
1133
1134 if (shell->workspaces.anim_timestamp == 0) {
1135 if (shell->workspaces.anim_current == 0.0)
1136 shell->workspaces.anim_timestamp = msecs;
1137 else
1138 shell->workspaces.anim_timestamp =
1139 msecs -
1140 /* Invers of movement function 'y' below. */
1141 (asin(1.0 - shell->workspaces.anim_current) *
1142 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1143 M_2_PI);
1144 }
1145
1146 t = msecs - shell->workspaces.anim_timestamp;
1147
1148 /*
1149 * x = [0, π/2]
1150 * y(x) = sin(x)
1151 */
1152 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1153 y = sin(x);
1154
1155 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001156 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001157
1158 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1159 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1160 shell->workspaces.anim_current = y;
1161
Scott Moreau4272e632012-08-13 09:58:41 -06001162 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001163 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001164 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001165 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001166}
1167
1168static void
1169animate_workspace_change(struct desktop_shell *shell,
1170 unsigned int index,
1171 struct workspace *from,
1172 struct workspace *to)
1173{
1174 struct weston_output *output;
1175
1176 int dir;
1177
1178 if (index > shell->workspaces.current)
1179 dir = -1;
1180 else
1181 dir = 1;
1182
1183 shell->workspaces.current = index;
1184
1185 shell->workspaces.anim_dir = dir;
1186 shell->workspaces.anim_from = from;
1187 shell->workspaces.anim_to = to;
1188 shell->workspaces.anim_current = 0.0;
1189 shell->workspaces.anim_timestamp = 0;
1190
1191 output = container_of(shell->compositor->output_list.next,
1192 struct weston_output, link);
1193 wl_list_insert(&output->animation_list,
1194 &shell->workspaces.animation.link);
1195
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001196 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001197
1198 workspace_translate_in(to, 0);
1199
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001200 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001201
Scott Moreau4272e632012-08-13 09:58:41 -06001202 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001203}
1204
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001205static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001206update_workspace(struct desktop_shell *shell, unsigned int index,
1207 struct workspace *from, struct workspace *to)
1208{
1209 shell->workspaces.current = index;
1210 wl_list_insert(&from->layer.link, &to->layer.link);
1211 wl_list_remove(&from->layer.link);
1212}
1213
1214static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001215change_workspace(struct desktop_shell *shell, unsigned int index)
1216{
1217 struct workspace *from;
1218 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001219 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001220
1221 if (index == shell->workspaces.current)
1222 return;
1223
1224 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001225 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001226 return;
1227
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001228 from = get_current_workspace(shell);
1229 to = get_workspace(shell, index);
1230
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001231 if (shell->workspaces.anim_from == to &&
1232 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001233 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001234 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001235 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001236 return;
1237 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001238
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001239 if (shell->workspaces.anim_to != NULL)
1240 finish_workspace_change_animation(shell,
1241 shell->workspaces.anim_from,
1242 shell->workspaces.anim_to);
1243
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001244 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001245
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001246 if (shell->focus_animation_type != ANIMATION_NONE) {
1247 wl_list_for_each(state, &from->focus_list, link)
1248 if (state->keyboard_focus)
1249 animate_focus_change(shell, from,
1250 get_default_view(state->keyboard_focus), NULL);
1251
1252 wl_list_for_each(state, &to->focus_list, link)
1253 if (state->keyboard_focus)
1254 animate_focus_change(shell, to,
1255 NULL, get_default_view(state->keyboard_focus));
1256 }
1257
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001258 if (workspace_is_empty(to) && workspace_is_empty(from))
1259 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001260 else
1261 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001262
1263 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001264}
1265
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001266static bool
1267workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1268{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001269 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001270 struct wl_list *e;
1271
1272 if (wl_list_empty(list))
1273 return false;
1274
1275 e = list->next;
1276
1277 if (e->next != list)
1278 return false;
1279
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001280 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001281}
1282
1283static void
Philip Withnall659163d2013-11-25 18:01:36 +00001284move_surface_to_workspace(struct desktop_shell *shell,
1285 struct shell_surface *shsurf,
1286 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001287{
1288 struct workspace *from;
1289 struct workspace *to;
1290 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001291 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001292 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001293
1294 if (workspace == shell->workspaces.current)
1295 return;
1296
Philip Withnall659163d2013-11-25 18:01:36 +00001297 view = get_default_view(shsurf->surface);
1298 if (!view)
1299 return;
1300
1301 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1302
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001303 if (workspace >= shell->workspaces.num)
1304 workspace = shell->workspaces.num - 1;
1305
Jonas Ådahle9d22502012-08-29 22:13:01 +02001306 from = get_current_workspace(shell);
1307 to = get_workspace(shell, workspace);
1308
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001309 weston_layer_entry_remove(&view->layer_link);
1310 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001311
Philip Withnall648a4dd2013-11-25 18:01:44 +00001312 shell_surface_update_child_surface_layers(shsurf);
1313
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001315 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1316 if (!seat->keyboard)
1317 continue;
1318
1319 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001321 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001322 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001323
Jason Ekstranda7af7042013-10-12 22:38:11 -05001324 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001325}
1326
1327static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001328take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001329 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001330 unsigned int index)
1331{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001332 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001334 struct shell_surface *shsurf;
1335 struct workspace *from;
1336 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001337 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001338
Pekka Paalanen01388e22013-04-25 13:57:44 +03001339 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 view = get_default_view(surface);
1341 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001342 index == shell->workspaces.current ||
1343 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001344 return;
1345
1346 from = get_current_workspace(shell);
1347 to = get_workspace(shell, index);
1348
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001349 weston_layer_entry_remove(&view->layer_link);
1350 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001351
Philip Withnall659163d2013-11-25 18:01:36 +00001352 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001353 if (shsurf != NULL)
1354 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001355
Jonas Ådahle9d22502012-08-29 22:13:01 +02001356 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001357 drop_focus_state(shell, from, surface);
1358
1359 if (shell->workspaces.anim_from == to &&
1360 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001361 wl_list_remove(&to->layer.link);
1362 wl_list_insert(from->layer.link.prev, &to->layer.link);
1363
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001364 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001365 broadcast_current_workspace_state(shell);
1366
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001367 return;
1368 }
1369
1370 if (shell->workspaces.anim_to != NULL)
1371 finish_workspace_change_animation(shell,
1372 shell->workspaces.anim_from,
1373 shell->workspaces.anim_to);
1374
1375 if (workspace_is_empty(from) &&
1376 workspace_has_only(to, surface))
1377 update_workspace(shell, index, from, to);
1378 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001379 if (shsurf != NULL &&
1380 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001381 wl_list_insert(&shell->workspaces.anim_sticky_list,
1382 &shsurf->workspace_transform.link);
1383
1384 animate_workspace_change(shell, index, from, to);
1385 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001386
1387 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001388
1389 state = ensure_focus_state(shell, seat);
1390 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001391 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001392}
1393
1394static void
1395workspace_manager_move_surface(struct wl_client *client,
1396 struct wl_resource *resource,
1397 struct wl_resource *surface_resource,
1398 uint32_t workspace)
1399{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001400 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001401 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001402 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001403 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001404 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001405
Pekka Paalanen01388e22013-04-25 13:57:44 +03001406 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001407 shell_surface = get_shell_surface(main_surface);
1408 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001410
1411 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001412}
1413
1414static const struct workspace_manager_interface workspace_manager_implementation = {
1415 workspace_manager_move_surface,
1416};
1417
1418static void
1419unbind_resource(struct wl_resource *resource)
1420{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001421 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001422}
1423
1424static void
1425bind_workspace_manager(struct wl_client *client,
1426 void *data, uint32_t version, uint32_t id)
1427{
1428 struct desktop_shell *shell = data;
1429 struct wl_resource *resource;
1430
Jason Ekstranda85118c2013-06-27 20:17:02 -05001431 resource = wl_resource_create(client,
1432 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001433
1434 if (resource == NULL) {
1435 weston_log("couldn't add workspace manager object");
1436 return;
1437 }
1438
Jason Ekstranda85118c2013-06-27 20:17:02 -05001439 wl_resource_set_implementation(resource,
1440 &workspace_manager_implementation,
1441 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001442 wl_list_insert(&shell->workspaces.client_list,
1443 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001444
1445 workspace_manager_send_state(resource,
1446 shell->workspaces.current,
1447 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001448}
1449
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001450static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001451touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1452 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1453{
1454}
1455
1456static void
1457touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1458{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001459 struct weston_touch_move_grab *move =
1460 (struct weston_touch_move_grab *) container_of(
1461 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001462
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001463 if (touch_id == 0)
1464 move->active = 0;
1465
Jonas Ådahl9484b692013-12-02 22:05:03 +01001466 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001467 shell_touch_grab_end(&move->base);
1468 free(move);
1469 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001470}
1471
1472static void
1473touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1474 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1475{
1476 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1477 struct shell_surface *shsurf = move->base.shsurf;
1478 struct weston_surface *es;
1479 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1480 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1481
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001482 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001483 return;
1484
1485 es = shsurf->surface;
1486
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001487 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001488
1489 weston_compositor_schedule_repaint(es->compositor);
1490}
1491
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001492static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001493touch_move_grab_frame(struct weston_touch_grab *grab)
1494{
1495}
1496
1497static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001498touch_move_grab_cancel(struct weston_touch_grab *grab)
1499{
1500 struct weston_touch_move_grab *move =
1501 (struct weston_touch_move_grab *) container_of(
1502 grab, struct shell_touch_grab, grab);
1503
1504 shell_touch_grab_end(&move->base);
1505 free(move);
1506}
1507
Rusty Lynch1084da52013-08-15 09:10:08 -07001508static const struct weston_touch_grab_interface touch_move_grab_interface = {
1509 touch_move_grab_down,
1510 touch_move_grab_up,
1511 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001512 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001513 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001514};
1515
1516static int
1517surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1518{
1519 struct weston_touch_move_grab *move;
1520
1521 if (!shsurf)
1522 return -1;
1523
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001524 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001525 return 0;
1526
1527 move = malloc(sizeof *move);
1528 if (!move)
1529 return -1;
1530
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001531 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001532 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001533 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001535 seat->touch->grab_y;
1536
1537 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1538 seat->touch);
1539
1540 return 0;
1541}
1542
1543static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001544noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001545{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001546}
1547
1548static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001549constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1550{
1551 struct shell_surface *shsurf = move->base.shsurf;
1552 struct weston_pointer *pointer = move->base.grab.pointer;
Kristian Høgsberg3434b332014-04-29 16:38:23 -07001553 int x, y, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001554 const int safety = 50;
1555
1556 x = wl_fixed_to_int(pointer->x + move->dx);
1557 y = wl_fixed_to_int(pointer->y + move->dy);
1558
1559 panel_height = get_output_panel_height(shsurf->shell,
1560 shsurf->surface->output);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001561 bottom = y + shsurf->geometry.height;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001562 if (bottom - panel_height < safety)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001563 y = panel_height + safety - shsurf->geometry.height;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001564
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001565 if (move->client_initiated && y + shsurf->geometry.y < panel_height)
1566 y = panel_height - shsurf->geometry.y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001567
1568 *cx = x;
1569 *cy = y;
1570}
1571
1572static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001573move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1574 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001575{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001576 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001577 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001578 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001579 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001580
1581 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001582 if (!shsurf)
1583 return;
1584
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001585 constrain_position(move, &cx, &cy);
1586
1587 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001588
Jason Ekstranda7af7042013-10-12 22:38:11 -05001589 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001590}
1591
1592static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001593move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001594 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001595{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001596 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1597 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001598 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001599 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001600
Daniel Stone4dbadb12012-05-30 16:31:51 +01001601 if (pointer->button_count == 0 &&
1602 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001603 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001604 free(grab);
1605 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001606}
1607
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001608static void
1609move_grab_cancel(struct weston_pointer_grab *grab)
1610{
1611 struct shell_grab *shell_grab =
1612 container_of(grab, struct shell_grab, grab);
1613
1614 shell_grab_end(shell_grab);
1615 free(grab);
1616}
1617
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001618static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001619 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001620 move_grab_motion,
1621 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001622 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001623};
1624
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001625static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001626surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1627 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001628{
1629 struct weston_move_grab *move;
1630
1631 if (!shsurf)
1632 return -1;
1633
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001634 if (shsurf->grabbed ||
1635 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001636 return 0;
1637
1638 move = malloc(sizeof *move);
1639 if (!move)
1640 return -1;
1641
Jason Ekstranda7af7042013-10-12 22:38:11 -05001642 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001643 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001644 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001645 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001646 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001647
1648 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001649 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001650
1651 return 0;
1652}
1653
1654static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001655common_surface_move(struct wl_resource *resource,
1656 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001657{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001658 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001659 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001660 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001661
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001662 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001663 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001664 seat->pointer->button_count > 0 &&
1665 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001666 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001667 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001668 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001669 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001670 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001671 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001672 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001673 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001674 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001675 (surface_touch_move(shsurf, seat) < 0))
1676 wl_resource_post_no_memory(resource);
1677 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001678}
1679
Rafael Antognollie2a34552013-12-03 15:35:45 -02001680static void
1681shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1682 struct wl_resource *seat_resource, uint32_t serial)
1683{
1684 common_surface_move(resource, seat_resource, serial);
1685}
1686
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001687struct weston_resize_grab {
1688 struct shell_grab base;
1689 uint32_t edges;
1690 int32_t width, height;
1691};
1692
1693static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001694resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1695 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001696{
1697 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001698 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001699 struct shell_surface *shsurf = resize->base.shsurf;
1700 int32_t width, height;
1701 wl_fixed_t from_x, from_y;
1702 wl_fixed_t to_x, to_y;
1703
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001704 weston_pointer_move(pointer, x, y);
1705
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001706 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001707 return;
1708
Jason Ekstranda7af7042013-10-12 22:38:11 -05001709 weston_view_from_global_fixed(shsurf->view,
1710 pointer->grab_x, pointer->grab_y,
1711 &from_x, &from_y);
1712 weston_view_from_global_fixed(shsurf->view,
1713 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001714
1715 width = resize->width;
1716 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1717 width += wl_fixed_to_int(from_x - to_x);
1718 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1719 width += wl_fixed_to_int(to_x - from_x);
1720 }
1721
1722 height = resize->height;
1723 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1724 height += wl_fixed_to_int(from_y - to_y);
1725 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1726 height += wl_fixed_to_int(to_y - from_y);
1727 }
1728
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001729 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001730}
1731
1732static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001733send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001734{
1735 struct shell_surface *shsurf = get_shell_surface(surface);
1736
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001737 assert(shsurf);
1738
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001739 if (shsurf->resource)
1740 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001741 shsurf->resize_edges,
1742 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001743}
1744
1745static const struct weston_shell_client shell_client = {
1746 send_configure
1747};
1748
1749static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001750resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001751 uint32_t time, uint32_t button, uint32_t state_w)
1752{
1753 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001754 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001755 enum wl_pointer_button_state state = state_w;
1756
1757 if (pointer->button_count == 0 &&
1758 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1759 shell_grab_end(&resize->base);
1760 free(grab);
1761 }
1762}
1763
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001764static void
1765resize_grab_cancel(struct weston_pointer_grab *grab)
1766{
1767 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1768
1769 shell_grab_end(&resize->base);
1770 free(grab);
1771}
1772
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001773static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001774 noop_grab_focus,
1775 resize_grab_motion,
1776 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001777 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001778};
1779
Giulio Camuffob8366642013-04-25 13:57:46 +03001780/*
1781 * Returns the bounding box of a surface and all its sub-surfaces,
1782 * in the surface coordinates system. */
1783static void
1784surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1785 int32_t *y, int32_t *w, int32_t *h) {
1786 pixman_region32_t region;
1787 pixman_box32_t *box;
1788 struct weston_subsurface *subsurface;
1789
1790 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001791 surface->width,
1792 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001793
1794 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1795 pixman_region32_union_rect(&region, &region,
1796 subsurface->position.x,
1797 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001798 subsurface->surface->width,
1799 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001800 }
1801
1802 box = pixman_region32_extents(&region);
1803 if (x)
1804 *x = box->x1;
1805 if (y)
1806 *y = box->y1;
1807 if (w)
1808 *w = box->x2 - box->x1;
1809 if (h)
1810 *h = box->y2 - box->y1;
1811
1812 pixman_region32_fini(&region);
1813}
1814
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001815static int
1816surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001817 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001818{
1819 struct weston_resize_grab *resize;
1820
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001821 if (shsurf->grabbed ||
1822 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001823 return 0;
1824
1825 if (edges == 0 || edges > 15 ||
1826 (edges & 3) == 3 || (edges & 12) == 12)
1827 return 0;
1828
1829 resize = malloc(sizeof *resize);
1830 if (!resize)
1831 return -1;
1832
1833 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001834
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001835 resize->width = shsurf->geometry.width;
1836 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001837
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001838 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001839 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001840 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001841 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001842
1843 return 0;
1844}
1845
1846static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001847common_surface_resize(struct wl_resource *resource,
1848 struct wl_resource *seat_resource, uint32_t serial,
1849 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001850{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001851 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001852 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001853 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001854
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001855 if (seat->pointer == NULL ||
1856 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001857 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001858 seat->pointer->focus == NULL)
1859 return;
1860
1861 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1862 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001863 return;
1864
Kristian Høgsberge3148752013-05-06 23:19:49 -04001865 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001866 wl_resource_post_no_memory(resource);
1867}
1868
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001869static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001870shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1871 struct wl_resource *seat_resource, uint32_t serial,
1872 uint32_t edges)
1873{
1874 common_surface_resize(resource, seat_resource, serial, edges);
1875}
1876
1877static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001878busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001879{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001880 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001881 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001882 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001883 wl_fixed_t sx, sy;
1884
Jason Ekstranda7af7042013-10-12 22:38:11 -05001885 view = weston_compositor_pick_view(pointer->seat->compositor,
1886 pointer->x, pointer->y,
1887 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001888
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001889 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1890 shell_grab_end(grab);
1891 free(grab);
1892 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001893}
1894
1895static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001896busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1897 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001898{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001899 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001900}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001901
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001902static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001903busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001904 uint32_t time, uint32_t button, uint32_t state)
1905{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001906 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001907 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001908 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001909
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001910 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001911 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001912 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001913 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001914 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001915 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001916 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001917}
1918
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001919static void
1920busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1921{
1922 struct shell_grab *grab = (struct shell_grab *) base;
1923
1924 shell_grab_end(grab);
1925 free(grab);
1926}
1927
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001928static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001929 busy_cursor_grab_focus,
1930 busy_cursor_grab_motion,
1931 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001932 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001933};
1934
1935static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001936set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001937{
1938 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001939
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001940 if (pointer->grab->interface == &busy_cursor_grab_interface)
1941 return;
1942
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001943 grab = malloc(sizeof *grab);
1944 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001945 return;
1946
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001947 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1948 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03001949 /* Mark the shsurf as ungrabbed so that button binding is able
1950 * to move it. */
1951 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001952}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001953
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001954static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001955end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001956{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001957 struct shell_grab *grab;
1958 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001959
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001960 wl_list_for_each(seat, &compositor->seat_list, link) {
1961 if (seat->pointer == NULL)
1962 continue;
1963
1964 grab = (struct shell_grab *) seat->pointer->grab;
1965 if (grab->grab.interface == &busy_cursor_grab_interface &&
1966 wl_resource_get_client(grab->shsurf->resource) == client) {
1967 shell_grab_end(grab);
1968 free(grab);
1969 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001970 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001971}
1972
Scott Moreau9521d5e2012-04-19 13:06:17 -06001973static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001974handle_shell_client_destroy(struct wl_listener *listener, void *data);
1975
1976static int
1977xdg_ping_timeout_handler(void *data)
1978{
1979 struct shell_client *sc = data;
1980 struct weston_seat *seat;
1981 struct shell_surface *shsurf;
1982
1983 /* Client is not responding */
1984 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001985 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1986 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1987 continue;
1988 if (seat->pointer->focus->surface->resource == NULL)
1989 continue;
1990
1991 shsurf = get_shell_surface(seat->pointer->focus->surface);
1992 if (shsurf &&
1993 wl_resource_get_client(shsurf->resource) == sc->client)
1994 set_busy_cursor(shsurf, seat->pointer);
1995 }
1996
1997 return 1;
1998}
1999
2000static void
2001handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2002{
2003 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002004 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002005 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002006 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002007
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002008 if (sc->unresponsive) {
2009 xdg_ping_timeout_handler(sc);
2010 return;
2011 }
2012
2013 sc->ping_serial = serial;
2014 loop = wl_display_get_event_loop(compositor->wl_display);
2015 if (sc->ping_timer == NULL)
2016 sc->ping_timer =
2017 wl_event_loop_add_timer(loop,
2018 xdg_ping_timeout_handler, sc);
2019 if (sc->ping_timer == NULL)
2020 return;
2021
2022 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2023
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002024 if (shell_surface_is_xdg_surface(shsurf) ||
2025 shell_surface_is_xdg_popup(shsurf))
2026 xdg_shell_send_ping(sc->resource, serial);
2027 else if (shell_surface_is_wl_shell_surface(shsurf))
2028 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002029}
2030
Scott Moreauff1db4a2012-04-17 19:06:18 -06002031static void
2032ping_handler(struct weston_surface *surface, uint32_t serial)
2033{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002034 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002035
2036 if (!shsurf)
2037 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002038 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002039 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002040 if (shsurf->surface == shsurf->shell->grab_surface)
2041 return;
2042
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002043 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002044}
2045
2046static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002047handle_pointer_focus(struct wl_listener *listener, void *data)
2048{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002049 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002050 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002051 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002052 uint32_t serial;
2053
Jason Ekstranda7af7042013-10-12 22:38:11 -05002054 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002055 return;
2056
Jason Ekstranda7af7042013-10-12 22:38:11 -05002057 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002058 serial = wl_display_next_serial(compositor->wl_display);
2059 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002060}
2061
2062static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002063shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2064{
2065 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002066 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002067}
2068
2069static void
2070shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2071{
2072 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002073 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002074}
2075
2076static void
2077handle_keyboard_focus(struct wl_listener *listener, void *data)
2078{
2079 struct weston_keyboard *keyboard = data;
2080 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2081
2082 if (seat->focused_surface) {
2083 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2084 if (shsurf)
2085 shell_surface_lose_keyboard_focus(shsurf);
2086 }
2087
2088 seat->focused_surface = keyboard->focus;
2089
2090 if (seat->focused_surface) {
2091 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2092 if (shsurf)
2093 shell_surface_gain_keyboard_focus(shsurf);
2094 }
2095}
2096
2097static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002098shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002099{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002100 if (sc->ping_serial != serial)
2101 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002102
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002103 sc->unresponsive = 0;
2104 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002105
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002106 if (sc->ping_timer) {
2107 wl_event_source_remove(sc->ping_timer);
2108 sc->ping_timer = NULL;
2109 }
2110
2111}
2112
2113static void
2114shell_surface_pong(struct wl_client *client,
2115 struct wl_resource *resource, uint32_t serial)
2116{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002117 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2118 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002119
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002120 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002121}
2122
2123static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002124set_title(struct shell_surface *shsurf, const char *title)
2125{
2126 free(shsurf->title);
2127 shsurf->title = strdup(title);
2128}
2129
2130static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002131set_window_geometry(struct shell_surface *shsurf,
2132 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002133{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002134 shsurf->next_geometry.x = x;
2135 shsurf->next_geometry.y = y;
2136 shsurf->next_geometry.width = width;
2137 shsurf->next_geometry.height = height;
2138 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002139}
2140
2141static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002142shell_surface_set_title(struct wl_client *client,
2143 struct wl_resource *resource, const char *title)
2144{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002145 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002146
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002147 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002148}
2149
2150static void
2151shell_surface_set_class(struct wl_client *client,
2152 struct wl_resource *resource, const char *class)
2153{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002154 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002155
2156 free(shsurf->class);
2157 shsurf->class = strdup(class);
2158}
2159
Alex Wu4539b082012-03-01 12:57:46 +08002160static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002161restore_output_mode(struct weston_output *output)
2162{
Hardening57388e42013-09-18 23:56:36 +02002163 if (output->current_mode != output->original_mode ||
2164 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002165 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002166 output->original_mode,
2167 output->original_scale,
2168 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002169}
2170
2171static void
2172restore_all_output_modes(struct weston_compositor *compositor)
2173{
2174 struct weston_output *output;
2175
2176 wl_list_for_each(output, &compositor->output_list, link)
2177 restore_output_mode(output);
2178}
2179
Philip Withnall07926d92013-11-25 18:01:40 +00002180/* The surface will be inserted into the list immediately after the link
2181 * returned by this function (i.e. will be stacked immediately above the
2182 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002183static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002184shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2185{
2186 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002187 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002188
2189 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002190 case SHELL_SURFACE_XWAYLAND:
2191 return &shsurf->shell->fullscreen_layer.view_list;
2192
2193 case SHELL_SURFACE_NONE:
2194 return NULL;
2195
Kristian Høgsbergead52422014-01-01 13:51:52 -08002196 case SHELL_SURFACE_POPUP:
2197 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002198 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002199 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002200 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002201 /* Move the surface to its parent layer so
2202 * that surfaces which are transient for
2203 * fullscreen surfaces don't get hidden by the
2204 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002205
2206 /* TODO: Handle a parent with multiple views */
2207 parent = get_default_view(shsurf->parent);
2208 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002209 return container_of(parent->layer_link.link.prev,
2210 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002211 }
Philip Withnall07926d92013-11-25 18:01:40 +00002212
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002213 /* Move the surface to a normal workspace layer so that surfaces
2214 * which were previously fullscreen or transient are no longer
2215 * rendered on top. */
2216 ws = get_current_workspace(shsurf->shell);
2217 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002218 }
2219
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002220 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002221}
2222
Philip Withnall648a4dd2013-11-25 18:01:44 +00002223static void
2224shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2225{
2226 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002227 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002228
2229 /* Move the child layers to the same workspace as shsurf. They will be
2230 * stacked above shsurf. */
2231 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002232 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002233 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002234 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002235 prev = container_of(shsurf->view->layer_link.link.prev,
2236 struct weston_layer_entry, link);
2237 weston_layer_entry_remove(&child->view->layer_link);
2238 weston_layer_entry_insert(prev,
2239 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002240 weston_view_geometry_dirty(child->view);
2241 weston_surface_damage(child->surface);
2242
2243 /* Recurse. We don’t expect this to recurse very far (if
2244 * at all) because that would imply we have transient
2245 * (or popup) children of transient surfaces, which
2246 * would be unusual. */
2247 shell_surface_update_child_surface_layers(child);
2248 }
2249 }
2250}
2251
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002252/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002253 * geometry to ensure the changes are redrawn.
2254 *
2255 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2256 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002257static void
2258shell_surface_update_layer(struct shell_surface *shsurf)
2259{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002260 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002261
2262 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2263
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002264 if (new_layer_link == NULL)
2265 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002266 if (new_layer_link == &shsurf->view->layer_link)
2267 return;
2268
2269 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002270 weston_layer_entry_remove(&shsurf->view->layer_link);
2271 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002272 weston_view_geometry_dirty(shsurf->view);
2273 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002274
2275 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002276}
2277
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002278static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002279shell_surface_set_parent(struct shell_surface *shsurf,
2280 struct weston_surface *parent)
2281{
2282 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002283
2284 wl_list_remove(&shsurf->children_link);
2285 wl_list_init(&shsurf->children_link);
2286
2287 /* Insert into the parent surface’s child list. */
2288 if (parent != NULL) {
2289 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2290 if (parent_shsurf != NULL)
2291 wl_list_insert(&parent_shsurf->children_list,
2292 &shsurf->children_link);
2293 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002294}
2295
2296static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002297shell_surface_set_output(struct shell_surface *shsurf,
2298 struct weston_output *output)
2299{
2300 struct weston_surface *es = shsurf->surface;
2301
2302 /* get the default output, if the client set it as NULL
2303 check whether the ouput is available */
2304 if (output)
2305 shsurf->output = output;
2306 else if (es->output)
2307 shsurf->output = es->output;
2308 else
2309 shsurf->output = get_default_output(es->compositor);
2310}
2311
2312static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002313surface_clear_next_states(struct shell_surface *shsurf)
2314{
2315 shsurf->next_state.maximized = false;
2316 shsurf->next_state.fullscreen = false;
2317
2318 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2319 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2320 shsurf->state_changed = true;
2321}
2322
2323static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002324set_toplevel(struct shell_surface *shsurf)
2325{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002326 shell_surface_set_parent(shsurf, NULL);
2327 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002328 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002329
2330 /* The layer_link is updated in set_surface_type(),
2331 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002332}
2333
2334static void
2335shell_surface_set_toplevel(struct wl_client *client,
2336 struct wl_resource *resource)
2337{
2338 struct shell_surface *surface = wl_resource_get_user_data(resource);
2339
2340 set_toplevel(surface);
2341}
2342
2343static void
2344set_transient(struct shell_surface *shsurf,
2345 struct weston_surface *parent, int x, int y, uint32_t flags)
2346{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002347 assert(parent != NULL);
2348
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002349 shell_surface_set_parent(shsurf, parent);
2350
2351 surface_clear_next_states(shsurf);
2352
Philip Withnallbecb77e2013-11-25 18:01:30 +00002353 shsurf->transient.x = x;
2354 shsurf->transient.y = y;
2355 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002356
Rafael Antognollied207b42013-12-03 15:35:43 -02002357 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002358 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002359 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002360
2361 /* The layer_link is updated in set_surface_type(),
2362 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002363}
2364
2365static void
2366shell_surface_set_transient(struct wl_client *client,
2367 struct wl_resource *resource,
2368 struct wl_resource *parent_resource,
2369 int x, int y, uint32_t flags)
2370{
2371 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2372 struct weston_surface *parent =
2373 wl_resource_get_user_data(parent_resource);
2374
2375 set_transient(shsurf, parent, x, y, flags);
2376}
2377
2378static void
2379set_fullscreen(struct shell_surface *shsurf,
2380 uint32_t method,
2381 uint32_t framerate,
2382 struct weston_output *output)
2383{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002384 shell_surface_set_output(shsurf, output);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002385 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002386
2387 shsurf->fullscreen_output = shsurf->output;
2388 shsurf->fullscreen.type = method;
2389 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002390
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002391 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002392}
2393
2394static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002395weston_view_set_initial_position(struct weston_view *view,
2396 struct desktop_shell *shell);
2397
2398static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002399unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002400{
Philip Withnallf85fe842013-11-25 18:01:37 +00002401 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002402 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2403 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002404 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002405 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002406
Alex Wu4539b082012-03-01 12:57:46 +08002407 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2408 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002409
Alex Wu4539b082012-03-01 12:57:46 +08002410 wl_list_remove(&shsurf->fullscreen.transform.link);
2411 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002412
Jason Ekstranda7af7042013-10-12 22:38:11 -05002413 if (shsurf->fullscreen.black_view)
2414 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2415 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002416
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002417 if (shsurf->saved_position_valid)
2418 weston_view_set_position(shsurf->view,
2419 shsurf->saved_x, shsurf->saved_y);
2420 else
2421 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2422
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002423 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002424 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002425 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002426 shsurf->saved_rotation_valid = false;
2427 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002428
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002429 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002430}
2431
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002432static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002433shell_surface_set_fullscreen(struct wl_client *client,
2434 struct wl_resource *resource,
2435 uint32_t method,
2436 uint32_t framerate,
2437 struct wl_resource *output_resource)
2438{
2439 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2440 struct weston_output *output;
2441
2442 if (output_resource)
2443 output = wl_resource_get_user_data(output_resource);
2444 else
2445 output = NULL;
2446
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002447 shell_surface_set_parent(shsurf, NULL);
2448
Rafael Antognolli03b16592013-12-03 15:35:42 -02002449 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002450 shsurf->next_state.fullscreen = true;
2451 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002452 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002453}
2454
2455static void
2456set_popup(struct shell_surface *shsurf,
2457 struct weston_surface *parent,
2458 struct weston_seat *seat,
2459 uint32_t serial,
2460 int32_t x,
2461 int32_t y)
2462{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002463 assert(parent != NULL);
2464
Philip Withnallbecb77e2013-11-25 18:01:30 +00002465 shsurf->popup.shseat = get_shell_seat(seat);
2466 shsurf->popup.serial = serial;
2467 shsurf->popup.x = x;
2468 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002469
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002470 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002471}
2472
2473static void
2474shell_surface_set_popup(struct wl_client *client,
2475 struct wl_resource *resource,
2476 struct wl_resource *seat_resource,
2477 uint32_t serial,
2478 struct wl_resource *parent_resource,
2479 int32_t x, int32_t y, uint32_t flags)
2480{
2481 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002482 struct weston_surface *parent =
2483 wl_resource_get_user_data(parent_resource);
2484
2485 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002486
Rafael Antognolli03b16592013-12-03 15:35:42 -02002487 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002488 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002489 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002490 wl_resource_get_user_data(seat_resource),
2491 serial, x, y);
2492}
2493
2494static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002495unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002496{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002497 /* undo all maximized things here */
2498 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002499
2500 if (shsurf->saved_position_valid)
2501 weston_view_set_position(shsurf->view,
2502 shsurf->saved_x, shsurf->saved_y);
2503 else
2504 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002505
2506 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002507 wl_list_insert(&shsurf->view->geometry.transformation_list,
2508 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002509 shsurf->saved_rotation_valid = false;
2510 }
2511
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002512 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002513}
2514
Philip Withnallbecb77e2013-11-25 18:01:30 +00002515static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002516set_minimized(struct weston_surface *surface, uint32_t is_true)
2517{
2518 struct shell_surface *shsurf;
2519 struct workspace *current_ws;
2520 struct weston_seat *seat;
2521 struct weston_surface *focus;
2522 struct weston_view *view;
2523
2524 view = get_default_view(surface);
2525 if (!view)
2526 return;
2527
2528 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2529
2530 shsurf = get_shell_surface(surface);
2531 current_ws = get_current_workspace(shsurf->shell);
2532
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002533 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002534 /* hide or show, depending on the state */
2535 if (is_true) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002536 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002537
2538 drop_focus_state(shsurf->shell, current_ws, view->surface);
2539 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2540 if (!seat->keyboard)
2541 continue;
2542 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2543 if (focus == view->surface)
2544 weston_keyboard_set_focus(seat->keyboard, NULL);
2545 }
2546 }
2547 else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002548 weston_layer_entry_insert(&current_ws->layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002549
2550 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2551 if (!seat->keyboard)
2552 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002553 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002554 }
2555 }
2556
2557 shell_surface_update_child_surface_layers(shsurf);
2558
2559 weston_view_damage_below(view);
2560}
2561
2562static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002563shell_surface_set_maximized(struct wl_client *client,
2564 struct wl_resource *resource,
2565 struct wl_resource *output_resource)
2566{
2567 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2568 struct weston_output *output;
2569
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002570 surface_clear_next_states(shsurf);
2571 shsurf->next_state.maximized = true;
2572 shsurf->state_changed = true;
2573
2574 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2575 shell_surface_set_parent(shsurf, NULL);
2576
Philip Withnallbecb77e2013-11-25 18:01:30 +00002577 if (output_resource)
2578 output = wl_resource_get_user_data(output_resource);
2579 else
2580 output = NULL;
2581
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002582 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002583
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002584 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002585}
2586
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002587/* This is only ever called from set_surface_type(), so there’s no need to
2588 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002589static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002590reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002591{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002592 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002593 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002594 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002595 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002596
Pekka Paalanen98262232011-12-01 10:42:22 +02002597 return 0;
2598}
2599
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002600static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002601set_full_output(struct shell_surface *shsurf)
2602{
2603 shsurf->saved_x = shsurf->view->geometry.x;
2604 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002605 shsurf->saved_width = shsurf->surface->width;
2606 shsurf->saved_height = shsurf->surface->height;
2607 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002608 shsurf->saved_position_valid = true;
2609
2610 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2611 wl_list_remove(&shsurf->rotation.transform.link);
2612 wl_list_init(&shsurf->rotation.transform.link);
2613 weston_view_geometry_dirty(shsurf->view);
2614 shsurf->saved_rotation_valid = true;
2615 }
2616}
2617
2618static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002619set_surface_type(struct shell_surface *shsurf)
2620{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002621 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002622 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002623
Philip Withnallbecb77e2013-11-25 18:01:30 +00002624 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002625
Rafael Antognolli03b16592013-12-03 15:35:42 -02002626 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002627 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002628
2629 switch (shsurf->type) {
2630 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002631 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002632 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002633 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002634 weston_view_set_position(shsurf->view,
2635 pev->geometry.x + shsurf->transient.x,
2636 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002637 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002638 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002639
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002640 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002641 weston_view_set_position(shsurf->view, shsurf->transient.x,
2642 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002643 break;
2644
Philip Withnall0f640e22013-11-25 18:01:31 +00002645 case SHELL_SURFACE_POPUP:
2646 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002647 default:
2648 break;
2649 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002650
2651 /* Update the surface’s layer. */
2652 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002653}
2654
Tiago Vignattibe143262012-04-16 17:31:41 +03002655static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002656shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002657{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002658 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002659}
2660
Alex Wu21858432012-04-01 20:13:08 +08002661static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002662black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002663
Jason Ekstranda7af7042013-10-12 22:38:11 -05002664static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002665create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002666 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002667 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002668{
2669 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002670 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002671
2672 surface = weston_surface_create(ec);
2673 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002674 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002675 return NULL;
2676 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002677 view = weston_view_create(surface);
2678 if (surface == NULL) {
2679 weston_log("no memory\n");
2680 weston_surface_destroy(surface);
2681 return NULL;
2682 }
Alex Wu4539b082012-03-01 12:57:46 +08002683
Alex Wu21858432012-04-01 20:13:08 +08002684 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002685 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002686 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002687 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002688 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002689 pixman_region32_fini(&surface->input);
2690 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002691
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002692 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002693 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002694
2695 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002696}
2697
Philip Withnalled8f1a92013-11-25 18:01:43 +00002698static void
2699shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2700{
2701 struct weston_output *output = shsurf->fullscreen_output;
2702
Rafael Antognolli03b16592013-12-03 15:35:42 -02002703 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002704
2705 if (!shsurf->fullscreen.black_view)
2706 shsurf->fullscreen.black_view =
2707 create_black_surface(shsurf->surface->compositor,
2708 shsurf->surface,
2709 output->x, output->y,
2710 output->width,
2711 output->height);
2712
2713 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002714 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2715 weston_layer_entry_insert(&shsurf->view->layer_link,
2716 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002717 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2718 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002719
2720 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002721}
2722
Alex Wu4539b082012-03-01 12:57:46 +08002723/* Create black surface and append it to the associated fullscreen surface.
2724 * Handle size dismatch and positioning according to the method. */
2725static void
2726shell_configure_fullscreen(struct shell_surface *shsurf)
2727{
2728 struct weston_output *output = shsurf->fullscreen_output;
2729 struct weston_surface *surface = shsurf->surface;
2730 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002731 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002732 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002733
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002734 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2735 restore_output_mode(output);
2736
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002737 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002738 weston_layer_entry_remove(&shsurf->view->layer_link);
2739 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002740
Philip Withnalled8f1a92013-11-25 18:01:43 +00002741 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002742
Jason Ekstranda7af7042013-10-12 22:38:11 -05002743 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002744 &surf_width, &surf_height);
2745
Alex Wu4539b082012-03-01 12:57:46 +08002746 switch (shsurf->fullscreen.type) {
2747 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002748 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002749 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002750 break;
2751 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002752 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002753 if (output->width == surf_width &&
2754 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002755 weston_view_set_position(shsurf->view,
2756 output->x - surf_x,
2757 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002758 break;
2759 }
2760
Alex Wu4539b082012-03-01 12:57:46 +08002761 matrix = &shsurf->fullscreen.transform.matrix;
2762 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002763
Scott Moreau1bad5db2012-08-18 01:04:05 -06002764 output_aspect = (float) output->width /
2765 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002766 /* XXX: Use surf_width and surf_height here? */
2767 surface_aspect = (float) surface->width /
2768 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002769 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002770 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002771 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002772 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002773 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002774 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002775
Alex Wu4539b082012-03-01 12:57:46 +08002776 weston_matrix_scale(matrix, scale, scale, 1);
2777 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002778 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002779 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002780 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2781 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002782 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002783
Alex Wu4539b082012-03-01 12:57:46 +08002784 break;
2785 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002786 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002787 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002788 surf_width * surface->buffer_viewport.buffer.scale,
2789 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002790 shsurf->fullscreen.framerate};
2791
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002792 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002793 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002794 weston_view_set_position(shsurf->view,
2795 output->x - surf_x,
2796 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002797 shsurf->fullscreen.black_view->surface->width = output->width;
2798 shsurf->fullscreen.black_view->surface->height = output->height;
2799 weston_view_set_position(shsurf->fullscreen.black_view,
2800 output->x - surf_x,
2801 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002802 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002803 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002804 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002805 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002806 }
Alex Wubd3354b2012-04-17 17:20:49 +08002807 }
Alex Wu4539b082012-03-01 12:57:46 +08002808 break;
2809 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002810 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002811 break;
2812 default:
2813 break;
2814 }
2815}
2816
Alex Wu4539b082012-03-01 12:57:46 +08002817static void
2818shell_map_fullscreen(struct shell_surface *shsurf)
2819{
Alex Wubd3354b2012-04-17 17:20:49 +08002820 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002821}
2822
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002823static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002824set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2825{
2826 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002827 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002828 shsurf->transient.x = x;
2829 shsurf->transient.y = y;
2830 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002831
2832 shell_surface_set_parent(shsurf, NULL);
2833
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002834 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002835 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002836}
2837
Kristian Høgsberg47792412014-05-04 13:47:06 -07002838static void
2839shell_interface_set_fullscreen(struct shell_surface *shsurf,
2840 uint32_t method,
2841 uint32_t framerate,
2842 struct weston_output *output)
2843{
2844 surface_clear_next_states(shsurf);
2845 set_fullscreen(shsurf, method, framerate, output);
2846
2847 shsurf->next_state.fullscreen = true;
2848 shsurf->state_changed = true;
2849}
2850
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002851static int
2852shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2853{
2854 return surface_move(shsurf, ws, 1);
2855}
2856
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002857static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002858
2859static void
2860destroy_shell_seat(struct wl_listener *listener, void *data)
2861{
2862 struct shell_seat *shseat =
2863 container_of(listener,
2864 struct shell_seat, seat_destroy_listener);
2865 struct shell_surface *shsurf, *prev = NULL;
2866
2867 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002868 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002869 shseat->popup_grab.client = NULL;
2870
2871 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2872 shsurf->popup.shseat = NULL;
2873 if (prev) {
2874 wl_list_init(&prev->popup.grab_link);
2875 }
2876 prev = shsurf;
2877 }
2878 wl_list_init(&prev->popup.grab_link);
2879 }
2880
2881 wl_list_remove(&shseat->seat_destroy_listener.link);
2882 free(shseat);
2883}
2884
Jason Ekstrand024177c2014-04-21 19:42:58 -05002885static void
2886shell_seat_caps_changed(struct wl_listener *listener, void *data)
2887{
2888 struct shell_seat *seat;
2889
2890 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2891
2892 if (seat->seat->keyboard &&
2893 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2894 wl_signal_add(&seat->seat->keyboard->focus_signal,
2895 &seat->keyboard_focus_listener);
2896 } else if (!seat->seat->keyboard) {
2897 wl_list_init(&seat->keyboard_focus_listener.link);
2898 }
2899
2900 if (seat->seat->pointer &&
2901 wl_list_empty(&seat->pointer_focus_listener.link)) {
2902 wl_signal_add(&seat->seat->pointer->focus_signal,
2903 &seat->pointer_focus_listener);
2904 } else if (!seat->seat->pointer) {
2905 wl_list_init(&seat->pointer_focus_listener.link);
2906 }
2907}
2908
Giulio Camuffo5085a752013-03-25 21:42:45 +01002909static struct shell_seat *
2910create_shell_seat(struct weston_seat *seat)
2911{
2912 struct shell_seat *shseat;
2913
2914 shseat = calloc(1, sizeof *shseat);
2915 if (!shseat) {
2916 weston_log("no memory to allocate shell seat\n");
2917 return NULL;
2918 }
2919
2920 shseat->seat = seat;
2921 wl_list_init(&shseat->popup_grab.surfaces_list);
2922
2923 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2924 wl_signal_add(&seat->destroy_signal,
2925 &shseat->seat_destroy_listener);
2926
Jason Ekstrand024177c2014-04-21 19:42:58 -05002927 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2928 wl_list_init(&shseat->keyboard_focus_listener.link);
2929
2930 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2931 wl_list_init(&shseat->pointer_focus_listener.link);
2932
2933 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2934 wl_signal_add(&seat->updated_caps_signal,
2935 &shseat->caps_changed_listener);
2936 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2937
Giulio Camuffo5085a752013-03-25 21:42:45 +01002938 return shseat;
2939}
2940
2941static struct shell_seat *
2942get_shell_seat(struct weston_seat *seat)
2943{
2944 struct wl_listener *listener;
2945
2946 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002947 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002948
2949 return container_of(listener,
2950 struct shell_seat, seat_destroy_listener);
2951}
2952
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002953static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002954popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002955{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002956 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002957 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002958 struct shell_seat *shseat =
2959 container_of(grab, struct shell_seat, popup_grab.grab);
2960 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002961 wl_fixed_t sx, sy;
2962
Jason Ekstranda7af7042013-10-12 22:38:11 -05002963 view = weston_compositor_pick_view(pointer->seat->compositor,
2964 pointer->x, pointer->y,
2965 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002966
Jason Ekstranda7af7042013-10-12 22:38:11 -05002967 if (view && view->surface->resource &&
2968 wl_resource_get_client(view->surface->resource) == client) {
2969 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002970 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002971 weston_pointer_set_focus(pointer, NULL,
2972 wl_fixed_from_int(0),
2973 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002974 }
2975}
2976
2977static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002978popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2979 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002980{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002981 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002982 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002983 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002984
Jonas Ådahl61917c82014-08-11 22:44:02 +02002985 if (pointer->focus) {
2986 weston_view_from_global_fixed(pointer->focus, x, y,
2987 &pointer->sx, &pointer->sy);
2988 }
2989
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002990 weston_pointer_move(pointer, x, y);
2991
Neil Roberts96d790e2013-09-19 17:32:00 +01002992 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002993 weston_view_from_global_fixed(pointer->focus,
2994 pointer->x, pointer->y,
2995 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002996 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002997 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002998}
2999
3000static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003001popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01003002 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003003{
3004 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003005 struct shell_seat *shseat =
3006 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003007 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003008 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003009 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003010 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003011
Neil Roberts96d790e2013-09-19 17:32:00 +01003012 resource_list = &grab->pointer->focus_resource_list;
3013 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003014 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003015 wl_resource_for_each(resource, resource_list) {
3016 wl_pointer_send_button(resource, serial,
3017 time, button, state);
3018 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003019 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003020 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003021 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003022 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003023 }
3024
Daniel Stone4dbadb12012-05-30 16:31:51 +01003025 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003026 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003027}
3028
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003029static void
3030popup_grab_cancel(struct weston_pointer_grab *grab)
3031{
3032 popup_grab_end(grab->pointer);
3033}
3034
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003035static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003036 popup_grab_focus,
3037 popup_grab_motion,
3038 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003039 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003040};
3041
3042static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003043shell_surface_send_popup_done(struct shell_surface *shsurf)
3044{
3045 if (shell_surface_is_wl_shell_surface(shsurf))
3046 wl_shell_surface_send_popup_done(shsurf->resource);
3047 else if (shell_surface_is_xdg_popup(shsurf))
3048 xdg_popup_send_popup_done(shsurf->resource,
3049 shsurf->popup.serial);
3050}
3051
3052static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003053popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003054{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003055 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003056 struct shell_seat *shseat =
3057 container_of(grab, struct shell_seat, popup_grab.grab);
3058 struct shell_surface *shsurf;
3059 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003060
3061 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003062 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003063 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003064 shseat->popup_grab.grab.interface = NULL;
3065 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003066 /* Send the popup_done event to all the popups open */
3067 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003068 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003069 shsurf->popup.shseat = NULL;
3070 if (prev) {
3071 wl_list_init(&prev->popup.grab_link);
3072 }
3073 prev = shsurf;
3074 }
3075 wl_list_init(&prev->popup.grab_link);
3076 wl_list_init(&shseat->popup_grab.surfaces_list);
3077 }
3078}
3079
3080static void
3081add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
3082{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003083 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003084
3085 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003086 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003087 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003088 /* We must make sure here that this popup was opened after
3089 * a mouse press, and not just by moving around with other
3090 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04003091 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003092 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003093
Rob Bradforddfe31052013-06-26 19:49:11 +01003094 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003095 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003096 } else {
3097 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003098 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003099}
3100
3101static void
3102remove_popup_grab(struct shell_surface *shsurf)
3103{
3104 struct shell_seat *shseat = shsurf->popup.shseat;
3105
3106 wl_list_remove(&shsurf->popup.grab_link);
3107 wl_list_init(&shsurf->popup.grab_link);
3108 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003109 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003110 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003111 }
3112}
3113
3114static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003115shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003116{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003117 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003118 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003119
Jason Ekstranda7af7042013-10-12 22:38:11 -05003120 shsurf->surface->output = parent_view->output;
3121 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003122
Jason Ekstranda7af7042013-10-12 22:38:11 -05003123 weston_view_set_transform_parent(shsurf->view, parent_view);
3124 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3125 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003126
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003127 if (shseat->seat->pointer &&
3128 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003129 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003130 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003131 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003132 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003133 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003134}
3135
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003136static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003137 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003138 shell_surface_move,
3139 shell_surface_resize,
3140 shell_surface_set_toplevel,
3141 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003142 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003143 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003144 shell_surface_set_maximized,
3145 shell_surface_set_title,
3146 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003147};
3148
3149static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003150destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003151{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003152 struct shell_surface *child, *next;
3153
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003154 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3155
Giulio Camuffo5085a752013-03-25 21:42:45 +01003156 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3157 remove_popup_grab(shsurf);
3158 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003159
Alex Wubd3354b2012-04-17 17:20:49 +08003160 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003161 shell_surface_is_top_fullscreen(shsurf))
3162 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003163
Jason Ekstranda7af7042013-10-12 22:38:11 -05003164 if (shsurf->fullscreen.black_view)
3165 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003166
Alex Wubd3354b2012-04-17 17:20:49 +08003167 /* As destroy_resource() use wl_list_for_each_safe(),
3168 * we can always remove the listener.
3169 */
3170 wl_list_remove(&shsurf->surface_destroy_listener.link);
3171 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003172 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003173
Jason Ekstranda7af7042013-10-12 22:38:11 -05003174 weston_view_destroy(shsurf->view);
3175
Philip Withnall648a4dd2013-11-25 18:01:44 +00003176 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003177 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3178 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003179
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003180 wl_list_remove(&shsurf->link);
3181 free(shsurf);
3182}
3183
3184static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003185shell_destroy_shell_surface(struct wl_resource *resource)
3186{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003187 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003188
Bryan Caina46b9462014-04-04 17:41:24 -05003189 if (!wl_list_empty(&shsurf->popup.grab_link))
3190 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003191 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003192}
3193
3194static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003195shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003196{
3197 struct shell_surface *shsurf = container_of(listener,
3198 struct shell_surface,
3199 surface_destroy_listener);
3200
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003201 if (shsurf->resource)
3202 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003203
3204 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003205}
3206
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003207static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003208fade_out_done(struct weston_view_animation *animation, void *data)
3209{
3210 struct shell_surface *shsurf = data;
3211
3212 weston_surface_destroy(shsurf->surface);
3213}
3214
3215static void
3216handle_resource_destroy(struct wl_listener *listener, void *data)
3217{
3218 struct shell_surface *shsurf =
3219 container_of(listener, struct shell_surface,
3220 resource_destroy_listener);
3221
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003222 if (!weston_surface_is_mapped(shsurf->surface))
3223 return;
3224
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003225 shsurf->surface->ref_count++;
3226
3227 pixman_region32_fini(&shsurf->surface->pending.input);
3228 pixman_region32_init(&shsurf->surface->pending.input);
3229 pixman_region32_fini(&shsurf->surface->input);
3230 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003231 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3232 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3233 fade_out_done, shsurf);
3234 } else {
3235 weston_surface_destroy(shsurf->surface);
3236 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003237}
3238
3239static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003240shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003241
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003242struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003243get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003244{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003245 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003246 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003247 else
3248 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003249}
3250
Rafael Antognollie2a34552013-12-03 15:35:45 -02003251static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003252create_common_surface(struct shell_client *owner, void *shell,
3253 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003254 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003255{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003256 struct shell_surface *shsurf;
3257
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003258 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003259 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003260 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003261 }
3262
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003263 shsurf = calloc(1, sizeof *shsurf);
3264 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003265 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003266 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003267 }
3268
Jason Ekstranda7af7042013-10-12 22:38:11 -05003269 shsurf->view = weston_view_create(surface);
3270 if (!shsurf->view) {
3271 weston_log("no memory to allocate shell surface\n");
3272 free(shsurf);
3273 return NULL;
3274 }
3275
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003276 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003277 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003278
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003279 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3280 wl_resource_add_destroy_listener(surface->resource,
3281 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003282 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003283
Tiago Vignattibc052c92012-04-19 16:18:18 +03003284 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003285 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003286 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003287 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003288 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003289 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003290 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3291 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003292 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003293 wl_list_init(&shsurf->fullscreen.transform.link);
3294
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003295 shsurf->output = get_default_output(shsurf->shell->compositor);
3296
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003297 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003298 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003299 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003300 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003301
3302 /* init link so its safe to always remove it in destroy_shell_surface */
3303 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003304 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003305
Pekka Paalanen460099f2012-01-20 16:48:25 +02003306 /* empty when not in use */
3307 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003308 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003309
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003310 wl_list_init(&shsurf->workspace_transform.link);
3311
Philip Withnall648a4dd2013-11-25 18:01:44 +00003312 wl_list_init(&shsurf->children_link);
3313 wl_list_init(&shsurf->children_list);
3314 shsurf->parent = NULL;
3315
Pekka Paalanen98262232011-12-01 10:42:22 +02003316 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003317
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003318 shsurf->client = client;
3319
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003320 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003321}
3322
Rafael Antognollie2a34552013-12-03 15:35:45 -02003323static struct shell_surface *
3324create_shell_surface(void *shell, struct weston_surface *surface,
3325 const struct weston_shell_client *client)
3326{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003327 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003328}
3329
Jason Ekstranda7af7042013-10-12 22:38:11 -05003330static struct weston_view *
3331get_primary_view(void *shell, struct shell_surface *shsurf)
3332{
3333 return shsurf->view;
3334}
3335
Tiago Vignattibc052c92012-04-19 16:18:18 +03003336static void
3337shell_get_shell_surface(struct wl_client *client,
3338 struct wl_resource *resource,
3339 uint32_t id,
3340 struct wl_resource *surface_resource)
3341{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003342 struct weston_surface *surface =
3343 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003344 struct shell_client *sc = wl_resource_get_user_data(resource);
3345 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003346 struct shell_surface *shsurf;
3347
3348 if (get_shell_surface(surface)) {
3349 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003350 WL_DISPLAY_ERROR_INVALID_OBJECT,
3351 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003352 return;
3353 }
3354
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003355 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003356 if (!shsurf) {
3357 wl_resource_post_error(surface_resource,
3358 WL_DISPLAY_ERROR_INVALID_OBJECT,
3359 "surface->configure already set");
3360 return;
3361 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003362
Jason Ekstranda85118c2013-06-27 20:17:02 -05003363 shsurf->resource =
3364 wl_resource_create(client,
3365 &wl_shell_surface_interface, 1, id);
3366 wl_resource_set_implementation(shsurf->resource,
3367 &shell_surface_implementation,
3368 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003369}
3370
Rafael Antognollie2a34552013-12-03 15:35:45 -02003371static bool
3372shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3373{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003374 /* A shell surface without a resource is created from xwayland
3375 * and is considered a wl_shell surface for now. */
3376
3377 return shsurf->resource == NULL ||
3378 wl_resource_instance_of(shsurf->resource,
3379 &wl_shell_surface_interface,
3380 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003381}
3382
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003383static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003384 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003385};
3386
Rafael Antognollie2a34552013-12-03 15:35:45 -02003387/****************************
3388 * xdg-shell implementation */
3389
3390static void
3391xdg_surface_destroy(struct wl_client *client,
3392 struct wl_resource *resource)
3393{
3394 wl_resource_destroy(resource);
3395}
3396
3397static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003398xdg_surface_set_parent(struct wl_client *client,
3399 struct wl_resource *resource,
3400 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003401{
3402 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3403 struct weston_surface *parent;
3404
3405 if (parent_resource)
3406 parent = wl_resource_get_user_data(parent_resource);
3407 else
3408 parent = NULL;
3409
3410 shell_surface_set_parent(shsurf, parent);
3411}
3412
3413static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003414xdg_surface_set_app_id(struct wl_client *client,
3415 struct wl_resource *resource,
3416 const char *app_id)
3417{
3418 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3419
3420 free(shsurf->class);
3421 shsurf->class = strdup(app_id);
3422}
3423
3424static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003425xdg_surface_show_window_menu(struct wl_client *client,
3426 struct wl_resource *surface_resource,
3427 struct wl_resource *seat_resource,
3428 uint32_t serial,
3429 int32_t x,
3430 int32_t y)
3431{
3432 /* TODO */
3433}
3434
3435static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003436xdg_surface_set_title(struct wl_client *client,
3437 struct wl_resource *resource, const char *title)
3438{
3439 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3440
3441 set_title(shsurf, title);
3442}
3443
3444static void
3445xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3446 struct wl_resource *seat_resource, uint32_t serial)
3447{
3448 common_surface_move(resource, seat_resource, serial);
3449}
3450
3451static void
3452xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3453 struct wl_resource *seat_resource, uint32_t serial,
3454 uint32_t edges)
3455{
3456 common_surface_resize(resource, seat_resource, serial, edges);
3457}
3458
3459static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003460xdg_surface_ack_configure(struct wl_client *client,
3461 struct wl_resource *resource,
3462 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003463{
3464 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3465
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003466 if (shsurf->state_requested) {
3467 shsurf->next_state = shsurf->requested_state;
3468 shsurf->state_changed = true;
3469 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003470 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003471}
3472
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003473static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003474xdg_surface_set_window_geometry(struct wl_client *client,
3475 struct wl_resource *resource,
3476 int32_t x,
3477 int32_t y,
3478 int32_t width,
3479 int32_t height)
3480{
3481 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3482
3483 set_window_geometry(shsurf, x, y, width, height);
3484}
3485
3486static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003487xdg_surface_set_maximized(struct wl_client *client,
3488 struct wl_resource *resource)
3489{
3490 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3491
3492 shsurf->state_requested = true;
3493 shsurf->requested_state.maximized = true;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003494 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003495}
3496
3497static void
3498xdg_surface_unset_maximized(struct wl_client *client,
3499 struct wl_resource *resource)
3500{
3501 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3502
3503 shsurf->state_requested = true;
3504 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003505 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003506}
3507
3508static void
3509xdg_surface_set_fullscreen(struct wl_client *client,
3510 struct wl_resource *resource,
3511 struct wl_resource *output_resource)
3512{
3513 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3514 struct weston_output *output;
3515
3516 shsurf->state_requested = true;
3517 shsurf->requested_state.fullscreen = true;
3518
3519 if (output_resource)
3520 output = wl_resource_get_user_data(output_resource);
3521 else
3522 output = NULL;
3523
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003524 shell_surface_set_output(shsurf, output);
3525 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003526
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003527 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003528}
3529
3530static void
3531xdg_surface_unset_fullscreen(struct wl_client *client,
3532 struct wl_resource *resource)
3533{
3534 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3535
3536 shsurf->state_requested = true;
3537 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003538 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003539}
3540
3541static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003542xdg_surface_set_minimized(struct wl_client *client,
3543 struct wl_resource *resource)
3544{
3545 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3546
3547 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3548 return;
3549
3550 /* apply compositor's own minimization logic (hide) */
3551 set_minimized(shsurf->surface, 1);
3552}
3553
Rafael Antognollie2a34552013-12-03 15:35:45 -02003554static const struct xdg_surface_interface xdg_surface_implementation = {
3555 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003556 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003557 xdg_surface_set_title,
3558 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003559 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003560 xdg_surface_move,
3561 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003562 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003563 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003564 xdg_surface_set_maximized,
3565 xdg_surface_unset_maximized,
3566 xdg_surface_set_fullscreen,
3567 xdg_surface_unset_fullscreen,
3568 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003569};
3570
3571static void
3572xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003573 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003574{
3575 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003576 uint32_t *s;
3577 struct wl_array states;
3578 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003579
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003580 assert(shsurf);
3581
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003582 if (!shsurf->resource)
3583 return;
3584
3585 wl_array_init(&states);
3586 if (shsurf->requested_state.fullscreen) {
3587 s = wl_array_add(&states, sizeof *s);
3588 *s = XDG_SURFACE_STATE_FULLSCREEN;
3589 } else if (shsurf->requested_state.maximized) {
3590 s = wl_array_add(&states, sizeof *s);
3591 *s = XDG_SURFACE_STATE_MAXIMIZED;
3592 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003593 if (shsurf->resize_edges != 0) {
3594 s = wl_array_add(&states, sizeof *s);
3595 *s = XDG_SURFACE_STATE_RESIZING;
3596 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003597 if (shsurf->focus_count > 0) {
3598 s = wl_array_add(&states, sizeof *s);
3599 *s = XDG_SURFACE_STATE_ACTIVATED;
3600 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003601
3602 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3603 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3604
3605 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003606}
3607
3608static const struct weston_shell_client xdg_client = {
3609 xdg_send_configure
3610};
3611
3612static void
3613xdg_use_unstable_version(struct wl_client *client,
3614 struct wl_resource *resource,
3615 int32_t version)
3616{
3617 if (version > 1) {
3618 wl_resource_post_error(resource,
3619 1,
3620 "xdg-shell:: version not implemented yet.");
3621 return;
3622 }
3623}
3624
3625static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003626create_xdg_surface(struct shell_client *owner, void *shell,
3627 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003628 const struct weston_shell_client *client)
3629{
3630 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003631
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003632 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003633 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003634
3635 return shsurf;
3636}
3637
3638static void
3639xdg_get_xdg_surface(struct wl_client *client,
3640 struct wl_resource *resource,
3641 uint32_t id,
3642 struct wl_resource *surface_resource)
3643{
3644 struct weston_surface *surface =
3645 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003646 struct shell_client *sc = wl_resource_get_user_data(resource);
3647 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003648 struct shell_surface *shsurf;
3649
3650 if (get_shell_surface(surface)) {
3651 wl_resource_post_error(surface_resource,
3652 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003653 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003654 return;
3655 }
3656
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003657 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003658 if (!shsurf) {
3659 wl_resource_post_error(surface_resource,
3660 WL_DISPLAY_ERROR_INVALID_OBJECT,
3661 "surface->configure already set");
3662 return;
3663 }
3664
3665 shsurf->resource =
3666 wl_resource_create(client,
3667 &xdg_surface_interface, 1, id);
3668 wl_resource_set_implementation(shsurf->resource,
3669 &xdg_surface_implementation,
3670 shsurf, shell_destroy_shell_surface);
3671}
3672
3673static bool
3674shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3675{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003676 return shsurf->resource &&
3677 wl_resource_instance_of(shsurf->resource,
3678 &xdg_surface_interface,
3679 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003680}
3681
3682/* xdg-popup implementation */
3683
3684static void
3685xdg_popup_destroy(struct wl_client *client,
3686 struct wl_resource *resource)
3687{
3688 wl_resource_destroy(resource);
3689}
3690
Rafael Antognollie2a34552013-12-03 15:35:45 -02003691static const struct xdg_popup_interface xdg_popup_implementation = {
3692 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003693};
3694
3695static void
3696xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003697 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003698{
3699}
3700
3701static const struct weston_shell_client xdg_popup_client = {
3702 xdg_popup_send_configure
3703};
3704
3705static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003706create_xdg_popup(struct shell_client *owner, void *shell,
3707 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003708 const struct weston_shell_client *client,
3709 struct weston_surface *parent,
3710 struct shell_seat *seat,
3711 uint32_t serial,
3712 int32_t x, int32_t y)
3713{
3714 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003715
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003716 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003717 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003718 shsurf->popup.shseat = seat;
3719 shsurf->popup.serial = serial;
3720 shsurf->popup.x = x;
3721 shsurf->popup.y = y;
3722 shell_surface_set_parent(shsurf, parent);
3723
3724 return shsurf;
3725}
3726
3727static void
3728xdg_get_xdg_popup(struct wl_client *client,
3729 struct wl_resource *resource,
3730 uint32_t id,
3731 struct wl_resource *surface_resource,
3732 struct wl_resource *parent_resource,
3733 struct wl_resource *seat_resource,
3734 uint32_t serial,
3735 int32_t x, int32_t y, uint32_t flags)
3736{
3737 struct weston_surface *surface =
3738 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003739 struct shell_client *sc = wl_resource_get_user_data(resource);
3740 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003741 struct shell_surface *shsurf;
3742 struct weston_surface *parent;
3743 struct shell_seat *seat;
3744
3745 if (get_shell_surface(surface)) {
3746 wl_resource_post_error(surface_resource,
3747 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003748 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003749 return;
3750 }
3751
3752 if (!parent_resource) {
3753 wl_resource_post_error(surface_resource,
3754 WL_DISPLAY_ERROR_INVALID_OBJECT,
3755 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04003756 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003757 }
3758
3759 parent = wl_resource_get_user_data(parent_resource);
3760 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3761
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003762 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003763 parent, seat, serial, x, y);
3764 if (!shsurf) {
3765 wl_resource_post_error(surface_resource,
3766 WL_DISPLAY_ERROR_INVALID_OBJECT,
3767 "surface->configure already set");
3768 return;
3769 }
3770
3771 shsurf->resource =
3772 wl_resource_create(client,
3773 &xdg_popup_interface, 1, id);
3774 wl_resource_set_implementation(shsurf->resource,
3775 &xdg_popup_implementation,
3776 shsurf, shell_destroy_shell_surface);
3777}
3778
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003779static void
3780xdg_pong(struct wl_client *client,
3781 struct wl_resource *resource, uint32_t serial)
3782{
3783 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003784
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003785 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003786}
3787
Rafael Antognollie2a34552013-12-03 15:35:45 -02003788static bool
3789shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3790{
3791 return wl_resource_instance_of(shsurf->resource,
3792 &xdg_popup_interface,
3793 &xdg_popup_implementation);
3794}
3795
3796static const struct xdg_shell_interface xdg_implementation = {
3797 xdg_use_unstable_version,
3798 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003799 xdg_get_xdg_popup,
3800 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003801};
3802
3803static int
3804xdg_shell_unversioned_dispatch(const void *implementation,
3805 void *_target, uint32_t opcode,
3806 const struct wl_message *message,
3807 union wl_argument *args)
3808{
3809 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003810 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003811
3812 if (opcode != 0) {
3813 wl_resource_post_error(resource,
3814 WL_DISPLAY_ERROR_INVALID_OBJECT,
3815 "must call use_unstable_version first");
3816 return 0;
3817 }
3818
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003819#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003820
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003821 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3822 "shell implementation doesn't match protocol version");
3823
Rafael Antognollie2a34552013-12-03 15:35:45 -02003824 if (args[0].i != XDG_SERVER_VERSION) {
3825 wl_resource_post_error(resource,
3826 WL_DISPLAY_ERROR_INVALID_OBJECT,
3827 "incompatible version, server is %d "
3828 "client wants %d",
3829 XDG_SERVER_VERSION, args[0].i);
3830 return 0;
3831 }
3832
3833 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003834 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003835
3836 return 1;
3837}
3838
3839/* end of xdg-shell implementation */
3840/***********************************/
3841
Kristian Høgsberg07937562011-04-12 17:25:42 -04003842static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003843shell_fade(struct desktop_shell *shell, enum fade_type type);
3844
3845static int
3846screensaver_timeout(void *data)
3847{
3848 struct desktop_shell *shell = data;
3849
3850 shell_fade(shell, FADE_OUT);
3851
3852 return 1;
3853}
3854
3855static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003856handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003857{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003858 struct desktop_shell *shell =
3859 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003860
Pekka Paalanen18027e52011-12-02 16:31:49 +02003861 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003862
3863 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003864 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003865}
3866
3867static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003868launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003869{
3870 if (shell->screensaver.binding)
3871 return;
3872
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003873 if (!shell->screensaver.path) {
3874 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003875 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003876 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003877
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003878 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003879 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003880 return;
3881 }
3882
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003883 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003884 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003885 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003886 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003887}
3888
3889static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003890terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003891{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003892 if (shell->screensaver.process.pid == 0)
3893 return;
3894
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003895 /* Disarm the screensaver timer, otherwise it may fire when the
3896 * compositor is not in the idle state. In that case, the screen will
3897 * be locked, but the wake_signal won't fire on user input, making the
3898 * system unresponsive. */
3899 wl_event_source_timer_update(shell->screensaver.timer, 0);
3900
Pekka Paalanen18027e52011-12-02 16:31:49 +02003901 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003902}
3903
3904static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003905configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003906{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003907 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003908
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003909 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003910 if (v->output == ev->output && v != ev) {
3911 weston_view_unmap(v);
3912 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003913 }
3914 }
3915
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003916 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003917
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003918 if (wl_list_empty(&ev->layer_link.link)) {
3919 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003920 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003921 }
3922}
3923
3924static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003925background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003926{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003927 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003928 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003929
Jason Ekstranda7af7042013-10-12 22:38:11 -05003930 view = container_of(es->views.next, struct weston_view, surface_link);
3931
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003932 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003933}
3934
3935static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003936desktop_shell_set_background(struct wl_client *client,
3937 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003938 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003939 struct wl_resource *surface_resource)
3940{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003941 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003942 struct weston_surface *surface =
3943 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003944 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003945
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003946 if (surface->configure) {
3947 wl_resource_post_error(surface_resource,
3948 WL_DISPLAY_ERROR_INVALID_OBJECT,
3949 "surface role already assigned");
3950 return;
3951 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003952
Jason Ekstranda7af7042013-10-12 22:38:11 -05003953 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3954 weston_view_destroy(view);
3955 view = weston_view_create(surface);
3956
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003957 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003958 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003959 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003960 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003961 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003962 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003963 surface->output->width,
3964 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003965}
3966
3967static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003968panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003969{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003970 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003971 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003972
Jason Ekstranda7af7042013-10-12 22:38:11 -05003973 view = container_of(es->views.next, struct weston_view, surface_link);
3974
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003975 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003976}
3977
3978static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003979desktop_shell_set_panel(struct wl_client *client,
3980 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003981 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003982 struct wl_resource *surface_resource)
3983{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003984 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003985 struct weston_surface *surface =
3986 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003987 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003988
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003989 if (surface->configure) {
3990 wl_resource_post_error(surface_resource,
3991 WL_DISPLAY_ERROR_INVALID_OBJECT,
3992 "surface role already assigned");
3993 return;
3994 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003995
Jason Ekstranda7af7042013-10-12 22:38:11 -05003996 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3997 weston_view_destroy(view);
3998 view = weston_view_create(surface);
3999
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004000 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004001 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004002 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004003 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004004 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004005 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004006 surface->output->width,
4007 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004008}
4009
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004010static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004011lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004012{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004013 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004014 struct weston_view *view;
4015
4016 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004017
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004018 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004019 return;
4020
Jason Ekstranda7af7042013-10-12 22:38:11 -05004021 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004022
4023 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004024 weston_layer_entry_insert(&shell->lock_layer.view_list,
4025 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004026 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004027 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004028 }
4029}
4030
4031static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004032handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004033{
Tiago Vignattibe143262012-04-16 17:31:41 +03004034 struct desktop_shell *shell =
4035 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004036
Martin Minarik6d118362012-06-07 18:01:59 +02004037 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004038 shell->lock_surface = NULL;
4039}
4040
4041static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004042desktop_shell_set_lock_surface(struct wl_client *client,
4043 struct wl_resource *resource,
4044 struct wl_resource *surface_resource)
4045{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004046 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004047 struct weston_surface *surface =
4048 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004049
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004050 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004051
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004052 if (!shell->locked)
4053 return;
4054
Pekka Paalanen98262232011-12-01 10:42:22 +02004055 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004056
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004057 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004058 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004059 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004060
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004061 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004062 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004063 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004064}
4065
4066static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004067resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004068{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004069 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004070
Pekka Paalanen77346a62011-11-30 16:26:35 +02004071 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004072
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004073 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004074 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004075 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004076 &shell->input_panel_layer.link);
4077 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004078 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004079 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004080 wl_list_insert(&shell->compositor->cursor_layer.link,
4081 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004082 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004083 wl_list_insert(&shell->fullscreen_layer.link,
4084 &shell->panel_layer.link);
4085 wl_list_insert(&shell->panel_layer.link,
4086 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004087
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004088 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004089
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004090 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004091 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004092 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004093}
4094
4095static void
4096desktop_shell_unlock(struct wl_client *client,
4097 struct wl_resource *resource)
4098{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004099 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004100
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004101 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004102
4103 if (shell->locked)
4104 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004105}
4106
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004107static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004108desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004109 struct wl_resource *resource,
4110 struct wl_resource *surface_resource)
4111{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004112 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004113
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004114 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004115 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004116}
4117
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004118static void
4119desktop_shell_desktop_ready(struct wl_client *client,
4120 struct wl_resource *resource)
4121{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004122 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004123
4124 shell_fade_startup(shell);
4125}
4126
Kristian Høgsberg75840622011-09-06 13:48:16 -04004127static const struct desktop_shell_interface desktop_shell_implementation = {
4128 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004129 desktop_shell_set_panel,
4130 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004131 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004132 desktop_shell_set_grab_surface,
4133 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04004134};
4135
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004136static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004137get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004138{
4139 struct shell_surface *shsurf;
4140
4141 shsurf = get_shell_surface(surface);
4142 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004143 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004144 return shsurf->type;
4145}
4146
Kristian Høgsberg75840622011-09-06 13:48:16 -04004147static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004148move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004149{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004150 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004151 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004152 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004153
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004154 if (seat->pointer->focus == NULL)
4155 return;
4156
4157 focus = seat->pointer->focus->surface;
4158
Pekka Paalanen01388e22013-04-25 13:57:44 +03004159 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004160 if (surface == NULL)
4161 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004162
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004163 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004164 if (shsurf == NULL || shsurf->state.fullscreen ||
4165 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004166 return;
4167
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004168 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004169}
4170
4171static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004172maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4173{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004174 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004175 struct weston_surface *surface;
4176 struct shell_surface *shsurf;
4177
4178 surface = weston_surface_get_main_surface(focus);
4179 if (surface == NULL)
4180 return;
4181
4182 shsurf = get_shell_surface(surface);
4183 if (shsurf == NULL)
4184 return;
4185
4186 if (!shell_surface_is_xdg_surface(shsurf))
4187 return;
4188
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004189 shsurf->state_requested = true;
4190 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004191 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004192}
4193
4194static void
4195fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4196{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004197 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004198 struct weston_surface *surface;
4199 struct shell_surface *shsurf;
4200
4201 surface = weston_surface_get_main_surface(focus);
4202 if (surface == NULL)
4203 return;
4204
4205 shsurf = get_shell_surface(surface);
4206 if (shsurf == NULL)
4207 return;
4208
4209 if (!shell_surface_is_xdg_surface(shsurf))
4210 return;
4211
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004212 shsurf->state_requested = true;
4213 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004214 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004215 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004216}
4217
4218static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004219touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4220{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004221 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004222 struct weston_surface *surface;
4223 struct shell_surface *shsurf;
4224
4225 surface = weston_surface_get_main_surface(focus);
4226 if (surface == NULL)
4227 return;
4228
4229 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004230 if (shsurf == NULL || shsurf->state.fullscreen ||
4231 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004232 return;
4233
4234 surface_touch_move(shsurf, (struct weston_seat *) seat);
4235}
4236
4237static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004238resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004239{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004240 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004241 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004242 uint32_t edges = 0;
4243 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004244 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004245
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004246 if (seat->pointer->focus == NULL)
4247 return;
4248
4249 focus = seat->pointer->focus->surface;
4250
Pekka Paalanen01388e22013-04-25 13:57:44 +03004251 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004252 if (surface == NULL)
4253 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004254
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004255 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004256 if (shsurf == NULL || shsurf->state.fullscreen ||
4257 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004258 return;
4259
Jason Ekstranda7af7042013-10-12 22:38:11 -05004260 weston_view_from_global(shsurf->view,
4261 wl_fixed_to_int(seat->pointer->grab_x),
4262 wl_fixed_to_int(seat->pointer->grab_y),
4263 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004264
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004265 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004266 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004267 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004268 edges |= 0;
4269 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004270 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004271
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004272 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004273 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004274 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004275 edges |= 0;
4276 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004277 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004278
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004279 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004280}
4281
4282static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004283surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004284 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004285{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004286 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004287 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004288 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004289 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004290
Pekka Paalanen01388e22013-04-25 13:57:44 +03004291 /* XXX: broken for windows containing sub-surfaces */
4292 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004293 if (surface == NULL)
4294 return;
4295
4296 shsurf = get_shell_surface(surface);
4297 if (!shsurf)
4298 return;
4299
Jason Ekstranda7af7042013-10-12 22:38:11 -05004300 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004301
Jason Ekstranda7af7042013-10-12 22:38:11 -05004302 if (shsurf->view->alpha > 1.0)
4303 shsurf->view->alpha = 1.0;
4304 if (shsurf->view->alpha < step)
4305 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004306
Jason Ekstranda7af7042013-10-12 22:38:11 -05004307 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004308 weston_surface_damage(surface);
4309}
4310
4311static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004312do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004313 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004314{
Daniel Stone37816df2012-05-16 18:45:18 +01004315 struct weston_seat *ws = (struct weston_seat *) seat;
4316 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004317 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004318 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004319
4320 wl_list_for_each(output, &compositor->output_list, link) {
4321 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004322 wl_fixed_to_double(seat->pointer->x),
4323 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004324 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004325 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004326 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004327 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004328 increment = -output->zoom.increment;
4329 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004330 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004331 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004332 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004333 else
4334 increment = 0;
4335
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004336 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004337
Scott Moreaue6603982012-06-11 13:07:51 -06004338 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004339 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004340 else if (output->zoom.level > output->zoom.max_level)
4341 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004342 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004343 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004344 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004345
Scott Moreaue6603982012-06-11 13:07:51 -06004346 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004347
Jason Ekstranda7af7042013-10-12 22:38:11 -05004348 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004349 }
4350 }
4351}
4352
Scott Moreauccbf29d2012-02-22 14:21:41 -07004353static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004354zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004355 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004356{
4357 do_zoom(seat, time, 0, axis, value);
4358}
4359
4360static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004361zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004362 void *data)
4363{
4364 do_zoom(seat, time, key, 0, 0);
4365}
4366
4367static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004368terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004369 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004370{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004371 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004372
Daniel Stone325fc2d2012-05-30 16:31:58 +01004373 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004374}
4375
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004376static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004377rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4378 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004379{
4380 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004381 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004382 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004383 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004384 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004385
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004386 weston_pointer_move(pointer, x, y);
4387
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004388 if (!shsurf)
4389 return;
4390
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004391 cx = 0.5f * shsurf->surface->width;
4392 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004393
Daniel Stone37816df2012-05-16 18:45:18 +01004394 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4395 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004396 r = sqrtf(dx * dx + dy * dy);
4397
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004398 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004399 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004400
4401 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004402 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004403 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004404
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004405 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004406 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004407
4408 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004409 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004410 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004411 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004412 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004413
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004414 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004415 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004416 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004417 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004418 wl_list_init(&shsurf->rotation.transform.link);
4419 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004420 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004421 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004422
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004423 /* We need to adjust the position of the surface
4424 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004425 cposx = shsurf->view->geometry.x + cx;
4426 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004427 dposx = rotate->center.x - cposx;
4428 dposy = rotate->center.y - cposy;
4429 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004430 weston_view_set_position(shsurf->view,
4431 shsurf->view->geometry.x + dposx,
4432 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004433 }
4434
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004435 /* Repaint implies weston_surface_update_transform(), which
4436 * lazily applies the damage due to rotation update.
4437 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004438 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004439}
4440
4441static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004442rotate_grab_button(struct weston_pointer_grab *grab,
4443 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004444{
4445 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004446 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004447 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004448 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004449 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004450
Daniel Stone4dbadb12012-05-30 16:31:51 +01004451 if (pointer->button_count == 0 &&
4452 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004453 if (shsurf)
4454 weston_matrix_multiply(&shsurf->rotation.rotation,
4455 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004456 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004457 free(rotate);
4458 }
4459}
4460
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004461static void
4462rotate_grab_cancel(struct weston_pointer_grab *grab)
4463{
4464 struct rotate_grab *rotate =
4465 container_of(grab, struct rotate_grab, base.grab);
4466
4467 shell_grab_end(&rotate->base);
4468 free(rotate);
4469}
4470
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004471static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004472 noop_grab_focus,
4473 rotate_grab_motion,
4474 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004475 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004476};
4477
4478static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004479surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004480{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004481 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004482 float dx, dy;
4483 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004484
Pekka Paalanen460099f2012-01-20 16:48:25 +02004485 rotate = malloc(sizeof *rotate);
4486 if (!rotate)
4487 return;
4488
Jason Ekstranda7af7042013-10-12 22:38:11 -05004489 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004490 surface->surface->width * 0.5f,
4491 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004492 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004493
Daniel Stone37816df2012-05-16 18:45:18 +01004494 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4495 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004496 r = sqrtf(dx * dx + dy * dy);
4497 if (r > 20.0f) {
4498 struct weston_matrix inverse;
4499
4500 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004501 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004502 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004503
4504 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004505 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004506 } else {
4507 weston_matrix_init(&surface->rotation.rotation);
4508 weston_matrix_init(&rotate->rotation);
4509 }
4510
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004511 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4512 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004513}
4514
4515static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004516rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004517 void *data)
4518{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004519 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004520 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004521 struct shell_surface *surface;
4522
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004523 if (seat->pointer->focus == NULL)
4524 return;
4525
4526 focus = seat->pointer->focus->surface;
4527
Pekka Paalanen01388e22013-04-25 13:57:44 +03004528 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004529 if (base_surface == NULL)
4530 return;
4531
4532 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004533 if (surface == NULL || surface->state.fullscreen ||
4534 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004535 return;
4536
4537 surface_rotate(surface, seat);
4538}
4539
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004540/* Move all fullscreen layers down to the current workspace and hide their
4541 * black views. The surfaces' state is set to both fullscreen and lowered,
4542 * and this is reversed when such a surface is re-configured, see
4543 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4544 *
4545 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004546 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004547void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004548lower_fullscreen_layer(struct desktop_shell *shell)
4549{
4550 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004551 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004552
4553 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004554 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004555 &shell->fullscreen_layer.view_list.link,
4556 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004557 struct shell_surface *shsurf = get_shell_surface(view->surface);
4558
4559 if (!shsurf)
4560 continue;
4561
4562 /* We can have a non-fullscreen popup for a fullscreen surface
4563 * in the fullscreen layer. */
4564 if (shsurf->state.fullscreen) {
4565 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004566 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
4567 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07004568 weston_view_damage_below(shsurf->fullscreen.black_view);
4569
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004570 }
4571
4572 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004573 weston_layer_entry_remove(&view->layer_link);
4574 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004575 weston_view_damage_below(view);
4576 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004577
4578 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004579 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004580}
4581
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004582void
Tiago Vignattibe143262012-04-16 17:31:41 +03004583activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004584 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004585{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004586 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004587 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004588 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004589 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004590 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004591
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004592 lower_fullscreen_layer(shell);
4593
Pekka Paalanen01388e22013-04-25 13:57:44 +03004594 main_surface = weston_surface_get_main_surface(es);
4595
Daniel Stone37816df2012-05-16 18:45:18 +01004596 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004597
Jonas Ådahl8538b222012-08-29 22:13:03 +02004598 state = ensure_focus_state(shell, seat);
4599 if (state == NULL)
4600 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004601
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004602 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004603 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004604
Rafael Antognolli03b16592013-12-03 15:35:42 -02004605 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004606 assert(shsurf);
4607
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004608 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004609 shell_configure_fullscreen(shsurf);
4610 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004611 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004612
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004613 /* Update the surface’s layer. This brings it to the top of the stacking
4614 * order as appropriate. */
4615 shell_surface_update_layer(shsurf);
4616
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004617 if (shell->focus_animation_type != ANIMATION_NONE) {
4618 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004619 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004620 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004621}
4622
Alex Wu21858432012-04-01 20:13:08 +08004623/* no-op func for checking black surface */
4624static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004625black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004626{
4627}
4628
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004629static bool
Alex Wu21858432012-04-01 20:13:08 +08004630is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4631{
4632 if (es->configure == black_surface_configure) {
4633 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004634 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004635 return true;
4636 }
4637 return false;
4638}
4639
Kristian Høgsberg75840622011-09-06 13:48:16 -04004640static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004641activate_binding(struct weston_seat *seat,
4642 struct desktop_shell *shell,
4643 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004644{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004645 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004646
Alex Wu9c35e6b2012-03-05 14:13:13 +08004647 if (!focus)
4648 return;
4649
Pekka Paalanen01388e22013-04-25 13:57:44 +03004650 if (is_black_surface(focus, &main_surface))
4651 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004652
Pekka Paalanen01388e22013-04-25 13:57:44 +03004653 main_surface = weston_surface_get_main_surface(focus);
4654 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004655 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004656
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004657 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004658}
4659
4660static void
4661click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4662 void *data)
4663{
4664 if (seat->pointer->grab != &seat->pointer->default_grab)
4665 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004666 if (seat->pointer->focus == NULL)
4667 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004668
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004669 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004670}
4671
4672static void
4673touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4674{
4675 if (seat->touch->grab != &seat->touch->default_grab)
4676 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004677 if (seat->touch->focus == NULL)
4678 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004679
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004680 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004681}
4682
4683static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004684unfocus_all_seats(struct desktop_shell *shell)
4685{
4686 struct weston_seat *seat, *next;
4687
4688 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4689 if (seat->keyboard == NULL)
4690 continue;
4691
4692 weston_keyboard_set_focus(seat->keyboard, NULL);
4693 }
4694}
4695
4696static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004697lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004698{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004699 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004700
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004701 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004702 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004703 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004704 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004705
4706 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004707
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004708 /* Hide all surfaces by removing the fullscreen, panel and
4709 * toplevel layers. This way nothing else can show or receive
4710 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004711
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004712 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004713 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004714 if (shell->showing_input_panels)
4715 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004716 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004717 wl_list_insert(&shell->compositor->cursor_layer.link,
4718 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004719
Pekka Paalanen77346a62011-11-30 16:26:35 +02004720 launch_screensaver(shell);
4721
Neil Robertsb4a91702014-04-09 16:33:32 +01004722 /* Remove the keyboard focus on all seats. This will be
4723 * restored to the workspace's saved state via
4724 * restore_focus_state when the compositor is unlocked */
4725 unfocus_all_seats(shell);
4726
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004727 /* TODO: disable bindings that should not work while locked. */
4728
4729 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004730}
4731
4732static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004733unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004734{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004735 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004736 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004737 return;
4738 }
4739
4740 /* If desktop-shell client has gone away, unlock immediately. */
4741 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004742 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004743 return;
4744 }
4745
4746 if (shell->prepare_event_sent)
4747 return;
4748
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004749 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004750 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004751}
4752
4753static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004754shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004755{
4756 struct desktop_shell *shell = data;
4757
4758 shell->fade.animation = NULL;
4759
4760 switch (shell->fade.type) {
4761 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004762 weston_surface_destroy(shell->fade.view->surface);
4763 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004764 break;
4765 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004766 lock(shell);
4767 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004768 default:
4769 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004770 }
4771}
4772
Jason Ekstranda7af7042013-10-12 22:38:11 -05004773static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004774shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004775{
4776 struct weston_compositor *compositor = shell->compositor;
4777 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004778 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004779
4780 surface = weston_surface_create(compositor);
4781 if (!surface)
4782 return NULL;
4783
Jason Ekstranda7af7042013-10-12 22:38:11 -05004784 view = weston_view_create(surface);
4785 if (!view) {
4786 weston_surface_destroy(surface);
4787 return NULL;
4788 }
4789
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004790 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004791 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004792 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004793 weston_layer_entry_insert(&compositor->fade_layer.view_list,
4794 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004795 pixman_region32_init(&surface->input);
4796
Jason Ekstranda7af7042013-10-12 22:38:11 -05004797 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004798}
4799
4800static void
4801shell_fade(struct desktop_shell *shell, enum fade_type type)
4802{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004803 float tint;
4804
4805 switch (type) {
4806 case FADE_IN:
4807 tint = 0.0;
4808 break;
4809 case FADE_OUT:
4810 tint = 1.0;
4811 break;
4812 default:
4813 weston_log("shell: invalid fade type\n");
4814 return;
4815 }
4816
4817 shell->fade.type = type;
4818
Jason Ekstranda7af7042013-10-12 22:38:11 -05004819 if (shell->fade.view == NULL) {
4820 shell->fade.view = shell_fade_create_surface(shell);
4821 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004822 return;
4823
Jason Ekstranda7af7042013-10-12 22:38:11 -05004824 shell->fade.view->alpha = 1.0 - tint;
4825 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004826 }
4827
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004828 if (shell->fade.view->output == NULL) {
4829 /* If the black view gets a NULL output, we lost the
4830 * last output and we'll just cancel the fade. This
4831 * happens when you close the last window under the
4832 * X11 or Wayland backends. */
4833 shell->locked = false;
4834 weston_surface_destroy(shell->fade.view->surface);
4835 shell->fade.view = NULL;
4836 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004837 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004838 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004839 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004840 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004841 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004842 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004843 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004844}
4845
4846static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004847do_shell_fade_startup(void *data)
4848{
4849 struct desktop_shell *shell = data;
4850
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004851 if (shell->startup_animation_type == ANIMATION_FADE)
4852 shell_fade(shell, FADE_IN);
4853 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004854 weston_surface_destroy(shell->fade.view->surface);
4855 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004856 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004857}
4858
4859static void
4860shell_fade_startup(struct desktop_shell *shell)
4861{
4862 struct wl_event_loop *loop;
4863
4864 if (!shell->fade.startup_timer)
4865 return;
4866
4867 wl_event_source_remove(shell->fade.startup_timer);
4868 shell->fade.startup_timer = NULL;
4869
4870 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4871 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4872}
4873
4874static int
4875fade_startup_timeout(void *data)
4876{
4877 struct desktop_shell *shell = data;
4878
4879 shell_fade_startup(shell);
4880 return 0;
4881}
4882
4883static void
4884shell_fade_init(struct desktop_shell *shell)
4885{
4886 /* Make compositor output all black, and wait for the desktop-shell
4887 * client to signal it is ready, then fade in. The timer triggers a
4888 * fade-in, in case the desktop-shell client takes too long.
4889 */
4890
4891 struct wl_event_loop *loop;
4892
Jason Ekstranda7af7042013-10-12 22:38:11 -05004893 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004894 weston_log("%s: warning: fade surface already exists\n",
4895 __func__);
4896 return;
4897 }
4898
Jason Ekstranda7af7042013-10-12 22:38:11 -05004899 shell->fade.view = shell_fade_create_surface(shell);
4900 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004901 return;
4902
Jason Ekstranda7af7042013-10-12 22:38:11 -05004903 weston_view_update_transform(shell->fade.view);
4904 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004905
4906 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4907 shell->fade.startup_timer =
4908 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4909 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4910}
4911
4912static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004913idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004914{
4915 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004916 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004917 struct weston_seat *seat;
4918
4919 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4920 if (seat->pointer)
4921 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004922
4923 shell_fade(shell, FADE_OUT);
4924 /* lock() is called from shell_fade_done() */
4925}
4926
4927static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004928wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004929{
4930 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004931 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004932
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004933 unlock(shell);
4934}
4935
4936static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004937center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004938{
Giulio Camuffob8366642013-04-25 13:57:46 +03004939 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004940 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004941
Jason Ekstranda7af7042013-10-12 22:38:11 -05004942 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004943
4944 x = output->x + (output->width - width) / 2 - surf_x / 2;
4945 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004946
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004947 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004948}
4949
4950static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004951weston_view_set_initial_position(struct weston_view *view,
4952 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004953{
4954 struct weston_compositor *compositor = shell->compositor;
4955 int ix = 0, iy = 0;
4956 int range_x, range_y;
4957 int dx, dy, x, y, panel_height;
4958 struct weston_output *output, *target_output = NULL;
4959 struct weston_seat *seat;
4960
4961 /* As a heuristic place the new window on the same output as the
4962 * pointer. Falling back to the output containing 0, 0.
4963 *
4964 * TODO: Do something clever for touch too?
4965 */
4966 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004967 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004968 ix = wl_fixed_to_int(seat->pointer->x);
4969 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004970 break;
4971 }
4972 }
4973
4974 wl_list_for_each(output, &compositor->output_list, link) {
4975 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4976 target_output = output;
4977 break;
4978 }
4979 }
4980
4981 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004982 weston_view_set_position(view, 10 + random() % 400,
4983 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004984 return;
4985 }
4986
4987 /* Valid range within output where the surface will still be onscreen.
4988 * If this is negative it means that the surface is bigger than
4989 * output.
4990 */
4991 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004992 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004993 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004994 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004995
Rob Bradford4cb88c72012-08-13 15:18:44 +01004996 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004997 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004998 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004999 dx = 0;
5000
5001 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005002 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005003 else
5004 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005005
5006 x = target_output->x + dx;
5007 y = target_output->y + dy;
5008
Jason Ekstranda7af7042013-10-12 22:38:11 -05005009 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005010}
5011
5012static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005013map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005014 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005015{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005016 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005017 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08005018 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03005019 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005020
Pekka Paalanen77346a62011-11-30 16:26:35 +02005021 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005022 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005023 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005024 if (shsurf->state.fullscreen) {
5025 center_on_output(shsurf->view, shsurf->fullscreen_output);
5026 shell_map_fullscreen(shsurf);
5027 } else if (shsurf->state.maximized) {
5028 /* use surface configure to set the geometry */
5029 panel_height = get_output_panel_height(shell, shsurf->output);
5030 surface_subsurfaces_boundingbox(shsurf->surface,
5031 &surf_x, &surf_y, NULL, NULL);
5032 weston_view_set_position(shsurf->view,
5033 shsurf->output->x - surf_x,
5034 shsurf->output->y +
5035 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02005036 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005037 weston_view_set_initial_position(shsurf->view, shell);
5038 }
Juan Zhao96879df2012-02-07 08:45:41 +08005039 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005040 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04005041 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00005042 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005043 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005044 weston_view_set_position(shsurf->view,
5045 shsurf->view->geometry.x + sx,
5046 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005047 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005048 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005049 default:
5050 ;
5051 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005052
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005053 /* Surface stacking order, see also activate(). */
5054 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005055
Jason Ekstranda7af7042013-10-12 22:38:11 -05005056 if (shsurf->type != SHELL_SURFACE_NONE) {
5057 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005058 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005059 shsurf->surface->output = shsurf->output;
5060 shsurf->view->output = shsurf->output;
5061 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005062 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005063
Jason Ekstranda7af7042013-10-12 22:38:11 -05005064 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005065 /* XXX: xwayland's using the same fields for transient type */
5066 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005067 if (shsurf->transient.flags ==
5068 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5069 break;
5070 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005071 if (shsurf->state.relative &&
5072 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5073 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005074 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005075 break;
5076 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005077 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005078 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005079 case SHELL_SURFACE_POPUP:
5080 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005081 default:
5082 break;
5083 }
5084
Rafael Antognolli03b16592013-12-03 15:35:42 -02005085 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5086 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005087 {
5088 switch (shell->win_animation_type) {
5089 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005090 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005091 break;
5092 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005093 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005094 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005095 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005096 default:
5097 break;
5098 }
5099 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005100}
5101
5102static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005103configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005104 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005105{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005106 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005107 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005108 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005109
Pekka Paalanen77346a62011-11-30 16:26:35 +02005110 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005111
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005112 assert(shsurf);
5113
Rafael Antognolli03b16592013-12-03 15:35:42 -02005114 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005115 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005116 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08005117 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03005118 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
5119 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005120 mx = shsurf->output->x - surf_x;
5121 my = shsurf->output->y +
5122 get_output_panel_height(shell,shsurf->output) - surf_y;
5123 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005124 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005125 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005126 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005127
Alex Wu4539b082012-03-01 12:57:46 +08005128 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005129 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005130 wl_list_for_each(view, &surface->views, surface_link)
5131 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005132
Rafael Antognolli03b16592013-12-03 15:35:42 -02005133 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005134 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005135 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005136}
5137
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005138static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005139shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005140{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005141 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005142 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005143 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005144
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005145 assert(shsurf);
5146
5147 shell = shsurf->shell;
5148
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005149 if (!weston_surface_is_mapped(es) &&
5150 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005151 remove_popup_grab(shsurf);
5152 }
5153
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005154 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005155 return;
5156
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005157 if (shsurf->has_next_geometry) {
5158 shsurf->geometry = shsurf->next_geometry;
5159 shsurf->has_next_geometry = false;
5160 shsurf->has_set_geometry = true;
5161 } else if (!shsurf->has_set_geometry) {
5162 surface_subsurfaces_boundingbox(shsurf->surface,
5163 &shsurf->geometry.x,
5164 &shsurf->geometry.y,
5165 &shsurf->geometry.width,
5166 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005167 }
5168
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005169 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005170 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005171 type_changed = 1;
5172 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005173
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005174 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005175 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005176 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005177 shsurf->last_width != es->width ||
5178 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005179 float from_x, from_y;
5180 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005181
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005182 if (shsurf->resize_edges) {
5183 sx = 0;
5184 sy = 0;
5185 }
5186
5187 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5188 sx = shsurf->last_width - es->width;
5189 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5190 sy = shsurf->last_height - es->height;
5191
5192 shsurf->last_width = es->width;
5193 shsurf->last_height = es->height;
5194
Jason Ekstranda7af7042013-10-12 22:38:11 -05005195 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5196 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005197 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005198 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005199 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005200 }
5201}
5202
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005203static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005204
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005205static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005206desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005207{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005208 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005209 struct desktop_shell *shell =
5210 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005211
5212 shell->child.process.pid = 0;
5213 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005214
5215 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5216 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005217 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005218 shell->child.deathstamp = time;
5219 shell->child.deathcount = 0;
5220 }
5221
5222 shell->child.deathcount++;
5223 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005224 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005225 return;
5226 }
5227
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005228 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005229 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005230 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005231}
5232
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005233static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005234desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5235{
5236 struct desktop_shell *shell;
5237
5238 shell = container_of(listener, struct desktop_shell,
5239 child.client_destroy_listener);
5240
5241 shell->child.client = NULL;
5242}
5243
5244static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005245launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005246{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005247 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005248
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005249 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005250 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005251 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005252 desktop_shell_sigchld);
5253
5254 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005255 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005256
5257 shell->child.client_destroy_listener.notify =
5258 desktop_shell_client_destroy;
5259 wl_client_add_destroy_listener(shell->child.client,
5260 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005261}
5262
5263static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005264handle_shell_client_destroy(struct wl_listener *listener, void *data)
5265{
5266 struct shell_client *sc =
5267 container_of(listener, struct shell_client, destroy_listener);
5268
5269 if (sc->ping_timer)
5270 wl_event_source_remove(sc->ping_timer);
5271 free(sc);
5272}
5273
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005274static struct shell_client *
5275shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5276 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005277{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005278 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005279
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005280 sc = zalloc(sizeof *sc);
5281 if (sc == NULL) {
5282 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005283 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005284 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005285
5286 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005287 if (sc->resource == NULL) {
5288 free(sc);
5289 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005290 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005291 }
5292
5293 sc->client = client;
5294 sc->shell = shell;
5295 sc->destroy_listener.notify = handle_shell_client_destroy;
5296 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5297
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005298 return sc;
5299}
5300
5301static void
5302bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5303{
5304 struct desktop_shell *shell = data;
5305 struct shell_client *sc;
5306
5307 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5308 if (sc)
5309 wl_resource_set_implementation(sc->resource,
5310 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005311 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005312}
5313
5314static void
5315bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5316{
5317 struct desktop_shell *shell = data;
5318 struct shell_client *sc;
5319
5320 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5321 if (sc)
5322 wl_resource_set_dispatcher(sc->resource,
5323 xdg_shell_unversioned_dispatch,
5324 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005325}
5326
5327static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005328unbind_desktop_shell(struct wl_resource *resource)
5329{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005330 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005331
5332 if (shell->locked)
5333 resume_desktop(shell);
5334
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005335 shell->child.desktop_shell = NULL;
5336 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005337}
5338
5339static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005340bind_desktop_shell(struct wl_client *client,
5341 void *data, uint32_t version, uint32_t id)
5342{
Tiago Vignattibe143262012-04-16 17:31:41 +03005343 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005344 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005345
Jason Ekstranda85118c2013-06-27 20:17:02 -05005346 resource = wl_resource_create(client, &desktop_shell_interface,
5347 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005348
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005349 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005350 wl_resource_set_implementation(resource,
5351 &desktop_shell_implementation,
5352 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005353 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005354
5355 if (version < 2)
5356 shell_fade_startup(shell);
5357
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005358 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005359 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005360
5361 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5362 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005363}
5364
Pekka Paalanen6e168112011-11-24 11:34:05 +02005365static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005366screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005367{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005368 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005369 struct weston_view *view;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005370 struct weston_layer_entry *prev;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005371
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005372 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005373 return;
5374
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005375 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005376 if (!shell->locked)
5377 return;
5378
Jason Ekstranda7af7042013-10-12 22:38:11 -05005379 view = container_of(surface->views.next, struct weston_view, surface_link);
5380 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005381
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005382 if (wl_list_empty(&view->layer_link.link)) {
5383 prev = container_of(shell->lock_layer.view_list.link.prev,
5384 struct weston_layer_entry, link);
5385 weston_layer_entry_insert(prev, &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005386 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005387 wl_event_source_timer_update(shell->screensaver.timer,
5388 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005389 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005390 }
5391}
5392
5393static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005394screensaver_set_surface(struct wl_client *client,
5395 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005396 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005397 struct wl_resource *output_resource)
5398{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005399 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005400 struct weston_surface *surface =
5401 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005402 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005403 struct weston_view *view, *next;
5404
5405 /* Make sure we only have one view */
5406 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5407 weston_view_destroy(view);
5408 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005409
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005410 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005411 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005412 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005413}
5414
5415static const struct screensaver_interface screensaver_implementation = {
5416 screensaver_set_surface
5417};
5418
5419static void
5420unbind_screensaver(struct wl_resource *resource)
5421{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005422 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005423
Pekka Paalanen77346a62011-11-30 16:26:35 +02005424 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005425}
5426
5427static void
5428bind_screensaver(struct wl_client *client,
5429 void *data, uint32_t version, uint32_t id)
5430{
Tiago Vignattibe143262012-04-16 17:31:41 +03005431 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005432 struct wl_resource *resource;
5433
Jason Ekstranda85118c2013-06-27 20:17:02 -05005434 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005435
Pekka Paalanen77346a62011-11-30 16:26:35 +02005436 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005437 wl_resource_set_implementation(resource,
5438 &screensaver_implementation,
5439 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005440 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005441 return;
5442 }
5443
5444 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5445 "interface object already bound");
Pekka Paalanen6e168112011-11-24 11:34:05 +02005446}
5447
Kristian Høgsberg07045392012-02-19 18:52:44 -05005448struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005449 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005450 struct weston_surface *current;
5451 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005452 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005453 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005454};
5455
5456static void
5457switcher_next(struct switcher *switcher)
5458{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005459 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005460 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005461 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005462 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005463
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005464 /* temporary re-display minimized surfaces */
5465 struct weston_view *tmp;
5466 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005467 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5468 weston_layer_entry_remove(&view->layer_link);
5469 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005470 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5471 *minimized = view;
5472 }
5473
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005474 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005475 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005476 if (shsurf &&
5477 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5478 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005479 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005480 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005481 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005482 next = view->surface;
5483 prev = view->surface;
5484 view->alpha = 0.25;
5485 weston_view_geometry_dirty(view);
5486 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005487 }
Alex Wu1659daa2012-04-01 20:13:09 +08005488
Jason Ekstranda7af7042013-10-12 22:38:11 -05005489 if (is_black_surface(view->surface, NULL)) {
5490 view->alpha = 0.25;
5491 weston_view_geometry_dirty(view);
5492 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005493 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005494 }
5495
5496 if (next == NULL)
5497 next = first;
5498
Alex Wu07b26062012-03-12 16:06:01 +08005499 if (next == NULL)
5500 return;
5501
Kristian Høgsberg07045392012-02-19 18:52:44 -05005502 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005503 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005504
5505 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005506 wl_list_for_each(view, &next->views, surface_link)
5507 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005508
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005509 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005510 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005511 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005512}
5513
5514static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005515switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005516{
5517 struct switcher *switcher =
5518 container_of(listener, struct switcher, listener);
5519
5520 switcher_next(switcher);
5521}
5522
5523static void
Daniel Stone351eb612012-05-31 15:27:47 -04005524switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005525{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005526 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005527 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005528 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005529
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005530 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005531 if (is_focus_view(view))
5532 continue;
5533
Jason Ekstranda7af7042013-10-12 22:38:11 -05005534 view->alpha = 1.0;
5535 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005536 }
5537
Alex Wu07b26062012-03-12 16:06:01 +08005538 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005539 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005540 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005541 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005542 weston_keyboard_end_grab(keyboard);
5543 if (keyboard->input_method_resource)
5544 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005545
5546 /* re-hide surfaces that were temporary shown during the switch */
5547 struct weston_view **minimized;
5548 wl_array_for_each(minimized, &switcher->minimized_array) {
5549 /* with the exception of the current selected */
5550 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005551 weston_layer_entry_remove(&(*minimized)->layer_link);
5552 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005553 weston_view_damage_below(*minimized);
5554 }
5555 }
5556 wl_array_release(&switcher->minimized_array);
5557
Kristian Høgsberg07045392012-02-19 18:52:44 -05005558 free(switcher);
5559}
5560
5561static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005562switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005563 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005564{
5565 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005566 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005567
Daniel Stonec9785ea2012-05-30 16:31:52 +01005568 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005569 switcher_next(switcher);
5570}
5571
5572static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005573switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005574 uint32_t mods_depressed, uint32_t mods_latched,
5575 uint32_t mods_locked, uint32_t group)
5576{
5577 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005578 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005579
Daniel Stone351eb612012-05-31 15:27:47 -04005580 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5581 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005582}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005583
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005584static void
5585switcher_cancel(struct weston_keyboard_grab *grab)
5586{
5587 struct switcher *switcher = container_of(grab, struct switcher, grab);
5588
5589 switcher_destroy(switcher);
5590}
5591
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005592static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005593 switcher_key,
5594 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005595 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005596};
5597
5598static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005599switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005600 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005601{
Tiago Vignattibe143262012-04-16 17:31:41 +03005602 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005603 struct switcher *switcher;
5604
5605 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005606 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005607 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005608 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005609 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005610 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005611
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005612 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005613 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005614 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005615 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5616 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005617 switcher_next(switcher);
5618}
5619
Pekka Paalanen3c647232011-12-22 13:43:43 +02005620static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005621backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005622 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005623{
5624 struct weston_compositor *compositor = data;
5625 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005626 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005627
5628 /* TODO: we're limiting to simple use cases, where we assume just
5629 * control on the primary display. We'd have to extend later if we
5630 * ever get support for setting backlights on random desktop LCD
5631 * panels though */
5632 output = get_default_output(compositor);
5633 if (!output)
5634 return;
5635
5636 if (!output->set_backlight)
5637 return;
5638
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005639 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5640 backlight_new = output->backlight_current - 25;
5641 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5642 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005643
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005644 if (backlight_new < 5)
5645 backlight_new = 5;
5646 if (backlight_new > 255)
5647 backlight_new = 255;
5648
5649 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005650 output->set_backlight(output, output->backlight_current);
5651}
5652
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005653struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005654 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005655 struct weston_seat *seat;
5656 uint32_t key[2];
5657 int key_released[2];
5658};
5659
5660static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005661debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005662 uint32_t key, uint32_t state)
5663{
5664 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005665 struct weston_compositor *ec = db->seat->compositor;
5666 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005667 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005668 uint32_t serial;
5669 int send = 0, terminate = 0;
5670 int check_binding = 1;
5671 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005672 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005673
5674 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5675 /* Do not run bindings on key releases */
5676 check_binding = 0;
5677
5678 for (i = 0; i < 2; i++)
5679 if (key == db->key[i])
5680 db->key_released[i] = 1;
5681
5682 if (db->key_released[0] && db->key_released[1]) {
5683 /* All key releases been swalled so end the grab */
5684 terminate = 1;
5685 } else if (key != db->key[0] && key != db->key[1]) {
5686 /* Should not swallow release of other keys */
5687 send = 1;
5688 }
5689 } else if (key == db->key[0] && !db->key_released[0]) {
5690 /* Do not check bindings for the first press of the binding
5691 * key. This allows it to be used as a debug shortcut.
5692 * We still need to swallow this event. */
5693 check_binding = 0;
5694 } else if (db->key[1]) {
5695 /* If we already ran a binding don't process another one since
5696 * we can't keep track of all the binding keys that were
5697 * pressed in order to swallow the release events. */
5698 send = 1;
5699 check_binding = 0;
5700 }
5701
5702 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005703 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5704 key, state)) {
5705 /* We ran a binding so swallow the press and keep the
5706 * grab to swallow the released too. */
5707 send = 0;
5708 terminate = 0;
5709 db->key[1] = key;
5710 } else {
5711 /* Terminate the grab since the key pressed is not a
5712 * debug binding key. */
5713 send = 1;
5714 terminate = 1;
5715 }
5716 }
5717
5718 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005719 serial = wl_display_next_serial(display);
5720 resource_list = &grab->keyboard->focus_resource_list;
5721 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005722 wl_keyboard_send_key(resource, serial, time, key, state);
5723 }
5724 }
5725
5726 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005727 weston_keyboard_end_grab(grab->keyboard);
5728 if (grab->keyboard->input_method_resource)
5729 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005730 free(db);
5731 }
5732}
5733
5734static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005735debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005736 uint32_t mods_depressed, uint32_t mods_latched,
5737 uint32_t mods_locked, uint32_t group)
5738{
5739 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005740 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005741
Neil Roberts96d790e2013-09-19 17:32:00 +01005742 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005743
Neil Roberts96d790e2013-09-19 17:32:00 +01005744 wl_resource_for_each(resource, resource_list) {
5745 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5746 mods_latched, mods_locked, group);
5747 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005748}
5749
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005750static void
5751debug_binding_cancel(struct weston_keyboard_grab *grab)
5752{
5753 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5754
5755 weston_keyboard_end_grab(grab->keyboard);
5756 free(db);
5757}
5758
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005759struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005760 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005761 debug_binding_modifiers,
5762 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005763};
5764
5765static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005766debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005767{
5768 struct debug_binding_grab *grab;
5769
5770 grab = calloc(1, sizeof *grab);
5771 if (!grab)
5772 return;
5773
5774 grab->seat = (struct weston_seat *) seat;
5775 grab->key[0] = key;
5776 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005777 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005778}
5779
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005780static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005781force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005782 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005783{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005784 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005785 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005786 struct desktop_shell *shell = data;
5787 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005788 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005789
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005790 focus_surface = seat->keyboard->focus;
5791 if (!focus_surface)
5792 return;
5793
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005794 wl_signal_emit(&compositor->kill_signal, focus_surface);
5795
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005796 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005797 wl_client_get_credentials(client, &pid, NULL, NULL);
5798
5799 /* Skip clients that we launched ourselves (the credentials of
5800 * the socketpair is ours) */
5801 if (pid == getpid())
5802 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005803
Daniel Stone325fc2d2012-05-30 16:31:58 +01005804 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005805}
5806
5807static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005808workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005809 uint32_t key, void *data)
5810{
5811 struct desktop_shell *shell = data;
5812 unsigned int new_index = shell->workspaces.current;
5813
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005814 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005815 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005816 if (new_index != 0)
5817 new_index--;
5818
5819 change_workspace(shell, new_index);
5820}
5821
5822static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005823workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005824 uint32_t key, void *data)
5825{
5826 struct desktop_shell *shell = data;
5827 unsigned int new_index = shell->workspaces.current;
5828
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005829 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005830 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005831 if (new_index < shell->workspaces.num - 1)
5832 new_index++;
5833
5834 change_workspace(shell, new_index);
5835}
5836
5837static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005838workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005839 uint32_t key, void *data)
5840{
5841 struct desktop_shell *shell = data;
5842 unsigned int new_index;
5843
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005844 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005845 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005846 new_index = key - KEY_F1;
5847 if (new_index >= shell->workspaces.num)
5848 new_index = shell->workspaces.num - 1;
5849
5850 change_workspace(shell, new_index);
5851}
5852
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005853static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005854workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005855 uint32_t key, void *data)
5856{
5857 struct desktop_shell *shell = data;
5858 unsigned int new_index = shell->workspaces.current;
5859
5860 if (shell->locked)
5861 return;
5862
5863 if (new_index != 0)
5864 new_index--;
5865
5866 take_surface_to_workspace_by_seat(shell, seat, new_index);
5867}
5868
5869static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005870workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005871 uint32_t key, void *data)
5872{
5873 struct desktop_shell *shell = data;
5874 unsigned int new_index = shell->workspaces.current;
5875
5876 if (shell->locked)
5877 return;
5878
5879 if (new_index < shell->workspaces.num - 1)
5880 new_index++;
5881
5882 take_surface_to_workspace_by_seat(shell, seat, new_index);
5883}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005884
5885static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005886shell_reposition_view_on_output_destroy(struct weston_view *view)
5887{
5888 struct weston_output *output, *first_output;
5889 struct weston_compositor *ec = view->surface->compositor;
5890 struct shell_surface *shsurf;
5891 float x, y;
5892 int visible;
5893
5894 x = view->geometry.x;
5895 y = view->geometry.y;
5896
5897 /* At this point the destroyed output is not in the list anymore.
5898 * If the view is still visible somewhere, we leave where it is,
5899 * otherwise, move it to the first output. */
5900 visible = 0;
5901 wl_list_for_each(output, &ec->output_list, link) {
5902 if (pixman_region32_contains_point(&output->region,
5903 x, y, NULL)) {
5904 visible = 1;
5905 break;
5906 }
5907 }
5908
5909 if (!visible) {
5910 first_output = container_of(ec->output_list.next,
5911 struct weston_output, link);
5912
5913 x = first_output->x + first_output->width / 4;
5914 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08005915
5916 weston_view_set_position(view, x, y);
5917 } else {
5918 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005919 }
5920
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005921
5922 shsurf = get_shell_surface(view->surface);
5923
5924 if (shsurf) {
5925 shsurf->saved_position_valid = false;
5926 shsurf->next_state.maximized = false;
5927 shsurf->next_state.fullscreen = false;
5928 shsurf->state_changed = true;
5929 }
5930}
5931
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005932void
5933shell_for_each_layer(struct desktop_shell *shell,
5934 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005935{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005936 struct workspace **ws;
5937
5938 func(shell, &shell->fullscreen_layer, data);
5939 func(shell, &shell->panel_layer, data);
5940 func(shell, &shell->background_layer, data);
5941 func(shell, &shell->lock_layer, data);
5942 func(shell, &shell->input_panel_layer, data);
5943
5944 wl_array_for_each(ws, &shell->workspaces.array)
5945 func(shell, &(*ws)->layer, data);
5946}
5947
5948static void
5949shell_output_destroy_move_layer(struct desktop_shell *shell,
5950 struct weston_layer *layer,
5951 void *data)
5952{
5953 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005954 struct weston_view *view;
5955
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005956 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005957 if (view->output != output)
5958 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005959
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005960 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005961 }
5962}
5963
5964static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005965handle_output_destroy(struct wl_listener *listener, void *data)
5966{
5967 struct shell_output *output_listener =
5968 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005969 struct weston_output *output = output_listener->output;
5970 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005971
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005972 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005973
Xiong Zhang6b481422013-10-23 13:58:32 +08005974 wl_list_remove(&output_listener->destroy_listener.link);
5975 wl_list_remove(&output_listener->link);
5976 free(output_listener);
5977}
5978
5979static void
5980create_shell_output(struct desktop_shell *shell,
5981 struct weston_output *output)
5982{
5983 struct shell_output *shell_output;
5984
5985 shell_output = zalloc(sizeof *shell_output);
5986 if (shell_output == NULL)
5987 return;
5988
5989 shell_output->output = output;
5990 shell_output->shell = shell;
5991 shell_output->destroy_listener.notify = handle_output_destroy;
5992 wl_signal_add(&output->destroy_signal,
5993 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005994 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005995}
5996
5997static void
5998handle_output_create(struct wl_listener *listener, void *data)
5999{
6000 struct desktop_shell *shell =
6001 container_of(listener, struct desktop_shell, output_create_listener);
6002 struct weston_output *output = (struct weston_output *)data;
6003
6004 create_shell_output(shell, output);
6005}
6006
6007static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006008handle_output_move_layer(struct desktop_shell *shell,
6009 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006010{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006011 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006012 struct weston_view *view;
6013 float x, y;
6014
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006015 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006016 if (view->output != output)
6017 continue;
6018
6019 x = view->geometry.x + output->move_x;
6020 y = view->geometry.y + output->move_y;
6021 weston_view_set_position(view, x, y);
6022 }
6023}
6024
6025static void
6026handle_output_move(struct wl_listener *listener, void *data)
6027{
6028 struct desktop_shell *shell;
6029
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006030 shell = container_of(listener, struct desktop_shell,
6031 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006032
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006033 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006034}
6035
6036static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006037setup_output_destroy_handler(struct weston_compositor *ec,
6038 struct desktop_shell *shell)
6039{
6040 struct weston_output *output;
6041
6042 wl_list_init(&shell->output_list);
6043 wl_list_for_each(output, &ec->output_list, link)
6044 create_shell_output(shell, output);
6045
6046 shell->output_create_listener.notify = handle_output_create;
6047 wl_signal_add(&ec->output_created_signal,
6048 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006049
6050 shell->output_move_listener.notify = handle_output_move;
6051 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006052}
6053
6054static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006055shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006056{
Tiago Vignattibe143262012-04-16 17:31:41 +03006057 struct desktop_shell *shell =
6058 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006059 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006060 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006061
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006062 /* Force state to unlocked so we don't try to fade */
6063 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006064 if (shell->child.client)
6065 wl_client_destroy(shell->child.client);
6066
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006067 wl_list_remove(&shell->idle_listener.link);
6068 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006069
6070 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006071
Xiong Zhang6b481422013-10-23 13:58:32 +08006072 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6073 wl_list_remove(&shell_output->destroy_listener.link);
6074 wl_list_remove(&shell_output->link);
6075 free(shell_output);
6076 }
6077
6078 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006079 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006080
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006081 wl_array_for_each(ws, &shell->workspaces.array)
6082 workspace_destroy(*ws);
6083 wl_array_release(&shell->workspaces.array);
6084
Pekka Paalanen3c647232011-12-22 13:43:43 +02006085 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006086 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006087 free(shell);
6088}
6089
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006090static void
6091shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6092{
6093 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006094 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006095
6096 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006097 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6098 MODIFIER_CTRL | MODIFIER_ALT,
6099 terminate_binding, ec);
6100 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6101 click_to_activate_binding,
6102 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006103 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6104 click_to_activate_binding,
6105 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006106 weston_compositor_add_touch_binding(ec, 0,
6107 touch_to_activate_binding,
6108 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006109 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6110 MODIFIER_SUPER | MODIFIER_ALT,
6111 surface_opacity_binding, NULL);
6112 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6113 MODIFIER_SUPER, zoom_axis_binding,
6114 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006115
6116 /* configurable bindings */
6117 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006118 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6119 zoom_key_binding, NULL);
6120 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6121 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006122 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6123 maximize_binding, NULL);
6124 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6125 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006126 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6127 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006128 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006129 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6130 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006131 weston_compositor_add_button_binding(ec, BTN_LEFT,
6132 mod | MODIFIER_SHIFT,
6133 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006134
6135 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6136 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6137 rotate_binding, NULL);
6138
Daniel Stone325fc2d2012-05-30 16:31:58 +01006139 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6140 shell);
6141 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6142 ec);
6143 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6144 backlight_binding, ec);
6145 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6146 ec);
6147 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6148 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006149 weston_compositor_add_key_binding(ec, KEY_K, mod,
6150 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006151 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6152 workspace_up_binding, shell);
6153 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6154 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006155 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6156 workspace_move_surface_up_binding,
6157 shell);
6158 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6159 workspace_move_surface_down_binding,
6160 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006161
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006162 if (shell->exposay_modifier)
6163 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6164 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006165
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006166 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6167 if (shell->workspaces.num > 1) {
6168 num_workspace_bindings = shell->workspaces.num;
6169 if (num_workspace_bindings > 6)
6170 num_workspace_bindings = 6;
6171 for (i = 0; i < num_workspace_bindings; i++)
6172 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6173 workspace_f_binding,
6174 shell);
6175 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006176
6177 /* Debug bindings */
6178 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6179 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006180}
6181
Jason Ekstrand024177c2014-04-21 19:42:58 -05006182static void
6183handle_seat_created(struct wl_listener *listener, void *data)
6184{
6185 struct weston_seat *seat = data;
6186
6187 create_shell_seat(seat);
6188}
6189
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006190WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006191module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006192 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006193{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006194 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006195 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006196 struct workspace **pws;
6197 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006198 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006199
Peter Huttererf3d62272013-08-08 11:57:05 +10006200 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006201 if (shell == NULL)
6202 return -1;
6203
Kristian Høgsberg75840622011-09-06 13:48:16 -04006204 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006205
6206 shell->destroy_listener.notify = shell_destroy;
6207 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006208 shell->idle_listener.notify = idle_handler;
6209 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6210 shell->wake_listener.notify = wake_handler;
6211 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006212
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006213 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006214 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006215 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006216 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006217 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006218 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006219 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006220 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006221 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006222 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006223 ec->shell_interface.set_window_geometry = set_window_geometry;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006224
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006225 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6226 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006227 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6228 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006229 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006230
6231 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006232 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006233
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006234 if (input_panel_setup(shell) < 0)
6235 return -1;
6236
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006237 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006238
Daniel Stonedf8133b2013-11-19 11:37:14 +01006239 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6240 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6241
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006242 for (i = 0; i < shell->workspaces.num; i++) {
6243 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6244 if (pws == NULL)
6245 return -1;
6246
6247 *pws = workspace_create();
6248 if (*pws == NULL)
6249 return -1;
6250 }
6251 activate_workspace(shell, 0);
6252
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006253 weston_layer_init(&shell->minimized_layer, NULL);
6254
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006255 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006256 wl_list_init(&shell->workspaces.animation.link);
6257 shell->workspaces.animation.frame = animate_workspace_change_frame;
6258
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006259 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006260 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006261 return -1;
6262
Rafael Antognollie2a34552013-12-03 15:35:45 -02006263 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6264 shell, bind_xdg_shell) == NULL)
6265 return -1;
6266
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006267 if (wl_global_create(ec->wl_display,
6268 &desktop_shell_interface, 2,
6269 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006270 return -1;
6271
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006272 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6273 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006274 return -1;
6275
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006276 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6277 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006278 return -1;
6279
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006280 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006281
Xiong Zhang6b481422013-10-23 13:58:32 +08006282 setup_output_destroy_handler(ec, shell);
6283
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006284 loop = wl_display_get_event_loop(ec->wl_display);
6285 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006286
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006287 shell->screensaver.timer =
6288 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6289
Jason Ekstrand024177c2014-04-21 19:42:58 -05006290 wl_list_for_each(seat, &ec->seat_list, link)
6291 handle_seat_created(NULL, seat);
6292 shell->seat_create_listener.notify = handle_seat_created;
6293 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006294
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006295 screenshooter_create(ec);
6296
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006297 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006298
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006299 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006300
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006301 return 0;
6302}