blob: d307e480b2af1c07b65098d137776ab6aef3bf72 [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
Jonny Lamb765760d2014-08-20 15:53:19 +02004127static void
4128desktop_shell_set_panel_position(struct wl_client *client,
4129 struct wl_resource *resource,
4130 uint32_t position)
4131{
4132 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4133
4134 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4135 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4136 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4137 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4138 wl_resource_post_error(resource,
4139 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4140 "bad position argument");
4141 return;
4142 }
4143
4144 shell->panel_position = position;
4145}
4146
Kristian Høgsberg75840622011-09-06 13:48:16 -04004147static const struct desktop_shell_interface desktop_shell_implementation = {
4148 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004149 desktop_shell_set_panel,
4150 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004151 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004152 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02004153 desktop_shell_desktop_ready,
4154 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04004155};
4156
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004157static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004158get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004159{
4160 struct shell_surface *shsurf;
4161
4162 shsurf = get_shell_surface(surface);
4163 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004164 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004165 return shsurf->type;
4166}
4167
Kristian Høgsberg75840622011-09-06 13:48:16 -04004168static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004169move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004170{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004171 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004172 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004173 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004174
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004175 if (seat->pointer->focus == NULL)
4176 return;
4177
4178 focus = seat->pointer->focus->surface;
4179
Pekka Paalanen01388e22013-04-25 13:57:44 +03004180 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004181 if (surface == NULL)
4182 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004183
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004184 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004185 if (shsurf == NULL || shsurf->state.fullscreen ||
4186 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004187 return;
4188
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004189 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004190}
4191
4192static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004193maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4194{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004195 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004196 struct weston_surface *surface;
4197 struct shell_surface *shsurf;
4198
4199 surface = weston_surface_get_main_surface(focus);
4200 if (surface == NULL)
4201 return;
4202
4203 shsurf = get_shell_surface(surface);
4204 if (shsurf == NULL)
4205 return;
4206
4207 if (!shell_surface_is_xdg_surface(shsurf))
4208 return;
4209
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004210 shsurf->state_requested = true;
4211 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004212 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004213}
4214
4215static void
4216fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4217{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004218 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004219 struct weston_surface *surface;
4220 struct shell_surface *shsurf;
4221
4222 surface = weston_surface_get_main_surface(focus);
4223 if (surface == NULL)
4224 return;
4225
4226 shsurf = get_shell_surface(surface);
4227 if (shsurf == NULL)
4228 return;
4229
4230 if (!shell_surface_is_xdg_surface(shsurf))
4231 return;
4232
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004233 shsurf->state_requested = true;
4234 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004235 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004236 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004237}
4238
4239static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004240touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4241{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004242 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004243 struct weston_surface *surface;
4244 struct shell_surface *shsurf;
4245
4246 surface = weston_surface_get_main_surface(focus);
4247 if (surface == NULL)
4248 return;
4249
4250 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004251 if (shsurf == NULL || shsurf->state.fullscreen ||
4252 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004253 return;
4254
4255 surface_touch_move(shsurf, (struct weston_seat *) seat);
4256}
4257
4258static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004259resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004260{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004261 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004262 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004263 uint32_t edges = 0;
4264 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004265 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004266
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004267 if (seat->pointer->focus == NULL)
4268 return;
4269
4270 focus = seat->pointer->focus->surface;
4271
Pekka Paalanen01388e22013-04-25 13:57:44 +03004272 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004273 if (surface == NULL)
4274 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004275
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004276 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004277 if (shsurf == NULL || shsurf->state.fullscreen ||
4278 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004279 return;
4280
Jason Ekstranda7af7042013-10-12 22:38:11 -05004281 weston_view_from_global(shsurf->view,
4282 wl_fixed_to_int(seat->pointer->grab_x),
4283 wl_fixed_to_int(seat->pointer->grab_y),
4284 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004285
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004286 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004287 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004288 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004289 edges |= 0;
4290 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004291 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004292
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004293 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004294 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004295 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004296 edges |= 0;
4297 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004298 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004299
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004300 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004301}
4302
4303static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004304surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004305 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004306{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004307 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004308 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004309 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004310 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004311
Pekka Paalanen01388e22013-04-25 13:57:44 +03004312 /* XXX: broken for windows containing sub-surfaces */
4313 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004314 if (surface == NULL)
4315 return;
4316
4317 shsurf = get_shell_surface(surface);
4318 if (!shsurf)
4319 return;
4320
Jason Ekstranda7af7042013-10-12 22:38:11 -05004321 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004322
Jason Ekstranda7af7042013-10-12 22:38:11 -05004323 if (shsurf->view->alpha > 1.0)
4324 shsurf->view->alpha = 1.0;
4325 if (shsurf->view->alpha < step)
4326 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004327
Jason Ekstranda7af7042013-10-12 22:38:11 -05004328 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004329 weston_surface_damage(surface);
4330}
4331
4332static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004333do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004334 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004335{
Daniel Stone37816df2012-05-16 18:45:18 +01004336 struct weston_seat *ws = (struct weston_seat *) seat;
4337 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004338 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004339 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004340
4341 wl_list_for_each(output, &compositor->output_list, link) {
4342 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004343 wl_fixed_to_double(seat->pointer->x),
4344 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004345 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004346 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004347 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004348 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004349 increment = -output->zoom.increment;
4350 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004351 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004352 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004353 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004354 else
4355 increment = 0;
4356
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004357 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004358
Scott Moreaue6603982012-06-11 13:07:51 -06004359 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004360 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004361 else if (output->zoom.level > output->zoom.max_level)
4362 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004363 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004364 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004365 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004366
Scott Moreaue6603982012-06-11 13:07:51 -06004367 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004368
Jason Ekstranda7af7042013-10-12 22:38:11 -05004369 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004370 }
4371 }
4372}
4373
Scott Moreauccbf29d2012-02-22 14:21:41 -07004374static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004375zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004376 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004377{
4378 do_zoom(seat, time, 0, axis, value);
4379}
4380
4381static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004382zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004383 void *data)
4384{
4385 do_zoom(seat, time, key, 0, 0);
4386}
4387
4388static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004389terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004390 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004391{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004392 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004393
Daniel Stone325fc2d2012-05-30 16:31:58 +01004394 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004395}
4396
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004397static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004398rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4399 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004400{
4401 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004402 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004403 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004404 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004405 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004406
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004407 weston_pointer_move(pointer, x, y);
4408
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004409 if (!shsurf)
4410 return;
4411
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004412 cx = 0.5f * shsurf->surface->width;
4413 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004414
Daniel Stone37816df2012-05-16 18:45:18 +01004415 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4416 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004417 r = sqrtf(dx * dx + dy * dy);
4418
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004419 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004420 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004421
4422 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004423 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004424 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004425
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004426 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004427 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004428
4429 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004430 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004431 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004432 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004433 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004434
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004435 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004436 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004437 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004438 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004439 wl_list_init(&shsurf->rotation.transform.link);
4440 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004441 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004442 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004443
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004444 /* We need to adjust the position of the surface
4445 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004446 cposx = shsurf->view->geometry.x + cx;
4447 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004448 dposx = rotate->center.x - cposx;
4449 dposy = rotate->center.y - cposy;
4450 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004451 weston_view_set_position(shsurf->view,
4452 shsurf->view->geometry.x + dposx,
4453 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004454 }
4455
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004456 /* Repaint implies weston_surface_update_transform(), which
4457 * lazily applies the damage due to rotation update.
4458 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004459 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004460}
4461
4462static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004463rotate_grab_button(struct weston_pointer_grab *grab,
4464 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004465{
4466 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004467 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004468 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004469 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004470 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004471
Daniel Stone4dbadb12012-05-30 16:31:51 +01004472 if (pointer->button_count == 0 &&
4473 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004474 if (shsurf)
4475 weston_matrix_multiply(&shsurf->rotation.rotation,
4476 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004477 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004478 free(rotate);
4479 }
4480}
4481
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004482static void
4483rotate_grab_cancel(struct weston_pointer_grab *grab)
4484{
4485 struct rotate_grab *rotate =
4486 container_of(grab, struct rotate_grab, base.grab);
4487
4488 shell_grab_end(&rotate->base);
4489 free(rotate);
4490}
4491
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004492static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004493 noop_grab_focus,
4494 rotate_grab_motion,
4495 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004496 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004497};
4498
4499static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004500surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004501{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004502 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004503 float dx, dy;
4504 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004505
Pekka Paalanen460099f2012-01-20 16:48:25 +02004506 rotate = malloc(sizeof *rotate);
4507 if (!rotate)
4508 return;
4509
Jason Ekstranda7af7042013-10-12 22:38:11 -05004510 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004511 surface->surface->width * 0.5f,
4512 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004513 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004514
Daniel Stone37816df2012-05-16 18:45:18 +01004515 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4516 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004517 r = sqrtf(dx * dx + dy * dy);
4518 if (r > 20.0f) {
4519 struct weston_matrix inverse;
4520
4521 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004522 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004523 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004524
4525 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004526 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004527 } else {
4528 weston_matrix_init(&surface->rotation.rotation);
4529 weston_matrix_init(&rotate->rotation);
4530 }
4531
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004532 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4533 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004534}
4535
4536static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004537rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004538 void *data)
4539{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004540 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004541 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004542 struct shell_surface *surface;
4543
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004544 if (seat->pointer->focus == NULL)
4545 return;
4546
4547 focus = seat->pointer->focus->surface;
4548
Pekka Paalanen01388e22013-04-25 13:57:44 +03004549 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004550 if (base_surface == NULL)
4551 return;
4552
4553 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004554 if (surface == NULL || surface->state.fullscreen ||
4555 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004556 return;
4557
4558 surface_rotate(surface, seat);
4559}
4560
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004561/* Move all fullscreen layers down to the current workspace and hide their
4562 * black views. The surfaces' state is set to both fullscreen and lowered,
4563 * and this is reversed when such a surface is re-configured, see
4564 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4565 *
4566 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004567 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004568void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004569lower_fullscreen_layer(struct desktop_shell *shell)
4570{
4571 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004572 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004573
4574 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004575 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004576 &shell->fullscreen_layer.view_list.link,
4577 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004578 struct shell_surface *shsurf = get_shell_surface(view->surface);
4579
4580 if (!shsurf)
4581 continue;
4582
4583 /* We can have a non-fullscreen popup for a fullscreen surface
4584 * in the fullscreen layer. */
4585 if (shsurf->state.fullscreen) {
4586 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004587 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
4588 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07004589 weston_view_damage_below(shsurf->fullscreen.black_view);
4590
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004591 }
4592
4593 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004594 weston_layer_entry_remove(&view->layer_link);
4595 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004596 weston_view_damage_below(view);
4597 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004598
4599 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004600 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004601}
4602
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004603void
Tiago Vignattibe143262012-04-16 17:31:41 +03004604activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004605 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004606{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004607 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004608 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004609 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004610 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004611 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004612
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004613 lower_fullscreen_layer(shell);
4614
Pekka Paalanen01388e22013-04-25 13:57:44 +03004615 main_surface = weston_surface_get_main_surface(es);
4616
Daniel Stone37816df2012-05-16 18:45:18 +01004617 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004618
Jonas Ådahl8538b222012-08-29 22:13:03 +02004619 state = ensure_focus_state(shell, seat);
4620 if (state == NULL)
4621 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004622
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004623 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004624 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004625
Rafael Antognolli03b16592013-12-03 15:35:42 -02004626 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004627 assert(shsurf);
4628
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004629 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004630 shell_configure_fullscreen(shsurf);
4631 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004632 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004633
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004634 /* Update the surface’s layer. This brings it to the top of the stacking
4635 * order as appropriate. */
4636 shell_surface_update_layer(shsurf);
4637
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004638 if (shell->focus_animation_type != ANIMATION_NONE) {
4639 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004640 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004641 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004642}
4643
Alex Wu21858432012-04-01 20:13:08 +08004644/* no-op func for checking black surface */
4645static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004646black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004647{
4648}
4649
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004650static bool
Alex Wu21858432012-04-01 20:13:08 +08004651is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4652{
4653 if (es->configure == black_surface_configure) {
4654 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004655 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004656 return true;
4657 }
4658 return false;
4659}
4660
Kristian Høgsberg75840622011-09-06 13:48:16 -04004661static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004662activate_binding(struct weston_seat *seat,
4663 struct desktop_shell *shell,
4664 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004665{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004666 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004667
Alex Wu9c35e6b2012-03-05 14:13:13 +08004668 if (!focus)
4669 return;
4670
Pekka Paalanen01388e22013-04-25 13:57:44 +03004671 if (is_black_surface(focus, &main_surface))
4672 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004673
Pekka Paalanen01388e22013-04-25 13:57:44 +03004674 main_surface = weston_surface_get_main_surface(focus);
4675 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004676 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004677
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004678 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004679}
4680
4681static void
4682click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4683 void *data)
4684{
4685 if (seat->pointer->grab != &seat->pointer->default_grab)
4686 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004687 if (seat->pointer->focus == NULL)
4688 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004689
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004690 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004691}
4692
4693static void
4694touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4695{
4696 if (seat->touch->grab != &seat->touch->default_grab)
4697 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004698 if (seat->touch->focus == NULL)
4699 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004700
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004701 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004702}
4703
4704static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004705unfocus_all_seats(struct desktop_shell *shell)
4706{
4707 struct weston_seat *seat, *next;
4708
4709 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4710 if (seat->keyboard == NULL)
4711 continue;
4712
4713 weston_keyboard_set_focus(seat->keyboard, NULL);
4714 }
4715}
4716
4717static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004718lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004719{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004720 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004721
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004722 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004723 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004724 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004725 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004726
4727 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004728
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004729 /* Hide all surfaces by removing the fullscreen, panel and
4730 * toplevel layers. This way nothing else can show or receive
4731 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004732
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004733 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004734 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004735 if (shell->showing_input_panels)
4736 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004737 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004738 wl_list_insert(&shell->compositor->cursor_layer.link,
4739 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004740
Pekka Paalanen77346a62011-11-30 16:26:35 +02004741 launch_screensaver(shell);
4742
Neil Robertsb4a91702014-04-09 16:33:32 +01004743 /* Remove the keyboard focus on all seats. This will be
4744 * restored to the workspace's saved state via
4745 * restore_focus_state when the compositor is unlocked */
4746 unfocus_all_seats(shell);
4747
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004748 /* TODO: disable bindings that should not work while locked. */
4749
4750 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004751}
4752
4753static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004754unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004755{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004756 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004757 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004758 return;
4759 }
4760
4761 /* If desktop-shell client has gone away, unlock immediately. */
4762 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004763 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004764 return;
4765 }
4766
4767 if (shell->prepare_event_sent)
4768 return;
4769
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004770 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004771 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004772}
4773
4774static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004775shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004776{
4777 struct desktop_shell *shell = data;
4778
4779 shell->fade.animation = NULL;
4780
4781 switch (shell->fade.type) {
4782 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004783 weston_surface_destroy(shell->fade.view->surface);
4784 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004785 break;
4786 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004787 lock(shell);
4788 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004789 default:
4790 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004791 }
4792}
4793
Jason Ekstranda7af7042013-10-12 22:38:11 -05004794static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004795shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004796{
4797 struct weston_compositor *compositor = shell->compositor;
4798 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004799 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004800
4801 surface = weston_surface_create(compositor);
4802 if (!surface)
4803 return NULL;
4804
Jason Ekstranda7af7042013-10-12 22:38:11 -05004805 view = weston_view_create(surface);
4806 if (!view) {
4807 weston_surface_destroy(surface);
4808 return NULL;
4809 }
4810
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004811 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004812 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004813 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004814 weston_layer_entry_insert(&compositor->fade_layer.view_list,
4815 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004816 pixman_region32_init(&surface->input);
4817
Jason Ekstranda7af7042013-10-12 22:38:11 -05004818 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004819}
4820
4821static void
4822shell_fade(struct desktop_shell *shell, enum fade_type type)
4823{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004824 float tint;
4825
4826 switch (type) {
4827 case FADE_IN:
4828 tint = 0.0;
4829 break;
4830 case FADE_OUT:
4831 tint = 1.0;
4832 break;
4833 default:
4834 weston_log("shell: invalid fade type\n");
4835 return;
4836 }
4837
4838 shell->fade.type = type;
4839
Jason Ekstranda7af7042013-10-12 22:38:11 -05004840 if (shell->fade.view == NULL) {
4841 shell->fade.view = shell_fade_create_surface(shell);
4842 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004843 return;
4844
Jason Ekstranda7af7042013-10-12 22:38:11 -05004845 shell->fade.view->alpha = 1.0 - tint;
4846 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004847 }
4848
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004849 if (shell->fade.view->output == NULL) {
4850 /* If the black view gets a NULL output, we lost the
4851 * last output and we'll just cancel the fade. This
4852 * happens when you close the last window under the
4853 * X11 or Wayland backends. */
4854 shell->locked = false;
4855 weston_surface_destroy(shell->fade.view->surface);
4856 shell->fade.view = NULL;
4857 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004858 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004859 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004860 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004861 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004862 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004863 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004864 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004865}
4866
4867static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004868do_shell_fade_startup(void *data)
4869{
4870 struct desktop_shell *shell = data;
4871
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004872 if (shell->startup_animation_type == ANIMATION_FADE)
4873 shell_fade(shell, FADE_IN);
4874 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004875 weston_surface_destroy(shell->fade.view->surface);
4876 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004877 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004878}
4879
4880static void
4881shell_fade_startup(struct desktop_shell *shell)
4882{
4883 struct wl_event_loop *loop;
4884
4885 if (!shell->fade.startup_timer)
4886 return;
4887
4888 wl_event_source_remove(shell->fade.startup_timer);
4889 shell->fade.startup_timer = NULL;
4890
4891 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4892 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4893}
4894
4895static int
4896fade_startup_timeout(void *data)
4897{
4898 struct desktop_shell *shell = data;
4899
4900 shell_fade_startup(shell);
4901 return 0;
4902}
4903
4904static void
4905shell_fade_init(struct desktop_shell *shell)
4906{
4907 /* Make compositor output all black, and wait for the desktop-shell
4908 * client to signal it is ready, then fade in. The timer triggers a
4909 * fade-in, in case the desktop-shell client takes too long.
4910 */
4911
4912 struct wl_event_loop *loop;
4913
Jason Ekstranda7af7042013-10-12 22:38:11 -05004914 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004915 weston_log("%s: warning: fade surface already exists\n",
4916 __func__);
4917 return;
4918 }
4919
Jason Ekstranda7af7042013-10-12 22:38:11 -05004920 shell->fade.view = shell_fade_create_surface(shell);
4921 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004922 return;
4923
Jason Ekstranda7af7042013-10-12 22:38:11 -05004924 weston_view_update_transform(shell->fade.view);
4925 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004926
4927 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4928 shell->fade.startup_timer =
4929 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4930 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4931}
4932
4933static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004934idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004935{
4936 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004937 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004938 struct weston_seat *seat;
4939
4940 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4941 if (seat->pointer)
4942 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004943
4944 shell_fade(shell, FADE_OUT);
4945 /* lock() is called from shell_fade_done() */
4946}
4947
4948static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004949wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004950{
4951 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004952 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004953
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004954 unlock(shell);
4955}
4956
4957static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004958center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004959{
Giulio Camuffob8366642013-04-25 13:57:46 +03004960 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004961 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004962
Jason Ekstranda7af7042013-10-12 22:38:11 -05004963 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004964
4965 x = output->x + (output->width - width) / 2 - surf_x / 2;
4966 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004967
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004968 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004969}
4970
4971static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004972weston_view_set_initial_position(struct weston_view *view,
4973 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004974{
4975 struct weston_compositor *compositor = shell->compositor;
4976 int ix = 0, iy = 0;
4977 int range_x, range_y;
4978 int dx, dy, x, y, panel_height;
4979 struct weston_output *output, *target_output = NULL;
4980 struct weston_seat *seat;
4981
4982 /* As a heuristic place the new window on the same output as the
4983 * pointer. Falling back to the output containing 0, 0.
4984 *
4985 * TODO: Do something clever for touch too?
4986 */
4987 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004988 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004989 ix = wl_fixed_to_int(seat->pointer->x);
4990 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004991 break;
4992 }
4993 }
4994
4995 wl_list_for_each(output, &compositor->output_list, link) {
4996 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4997 target_output = output;
4998 break;
4999 }
5000 }
5001
5002 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005003 weston_view_set_position(view, 10 + random() % 400,
5004 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005005 return;
5006 }
5007
5008 /* Valid range within output where the surface will still be onscreen.
5009 * If this is negative it means that the surface is bigger than
5010 * output.
5011 */
5012 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005013 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06005014 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005015 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005016
Rob Bradford4cb88c72012-08-13 15:18:44 +01005017 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005018 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005019 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01005020 dx = 0;
5021
5022 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005023 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01005024 else
5025 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005026
5027 x = target_output->x + dx;
5028 y = target_output->y + dy;
5029
Jason Ekstranda7af7042013-10-12 22:38:11 -05005030 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005031}
5032
5033static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005034map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005035 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005036{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005037 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005038 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08005039 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03005040 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005041
Pekka Paalanen77346a62011-11-30 16:26:35 +02005042 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005043 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005044 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005045 if (shsurf->state.fullscreen) {
5046 center_on_output(shsurf->view, shsurf->fullscreen_output);
5047 shell_map_fullscreen(shsurf);
5048 } else if (shsurf->state.maximized) {
5049 /* use surface configure to set the geometry */
5050 panel_height = get_output_panel_height(shell, shsurf->output);
5051 surface_subsurfaces_boundingbox(shsurf->surface,
5052 &surf_x, &surf_y, NULL, NULL);
5053 weston_view_set_position(shsurf->view,
5054 shsurf->output->x - surf_x,
5055 shsurf->output->y +
5056 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02005057 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005058 weston_view_set_initial_position(shsurf->view, shell);
5059 }
Juan Zhao96879df2012-02-07 08:45:41 +08005060 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005061 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04005062 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00005063 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005064 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005065 weston_view_set_position(shsurf->view,
5066 shsurf->view->geometry.x + sx,
5067 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005068 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005069 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005070 default:
5071 ;
5072 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005073
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005074 /* Surface stacking order, see also activate(). */
5075 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005076
Jason Ekstranda7af7042013-10-12 22:38:11 -05005077 if (shsurf->type != SHELL_SURFACE_NONE) {
5078 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005079 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005080 shsurf->surface->output = shsurf->output;
5081 shsurf->view->output = shsurf->output;
5082 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005083 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005084
Jason Ekstranda7af7042013-10-12 22:38:11 -05005085 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005086 /* XXX: xwayland's using the same fields for transient type */
5087 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005088 if (shsurf->transient.flags ==
5089 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5090 break;
5091 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005092 if (shsurf->state.relative &&
5093 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5094 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005095 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005096 break;
5097 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005098 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005099 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005100 case SHELL_SURFACE_POPUP:
5101 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005102 default:
5103 break;
5104 }
5105
Rafael Antognolli03b16592013-12-03 15:35:42 -02005106 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5107 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005108 {
5109 switch (shell->win_animation_type) {
5110 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005111 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005112 break;
5113 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005114 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005115 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005116 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005117 default:
5118 break;
5119 }
5120 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005121}
5122
5123static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005124configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005125 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005126{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005127 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005128 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005129 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005130
Pekka Paalanen77346a62011-11-30 16:26:35 +02005131 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005132
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005133 assert(shsurf);
5134
Rafael Antognolli03b16592013-12-03 15:35:42 -02005135 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005136 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005137 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08005138 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03005139 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
5140 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005141 mx = shsurf->output->x - surf_x;
5142 my = shsurf->output->y +
5143 get_output_panel_height(shell,shsurf->output) - surf_y;
5144 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005145 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005146 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005147 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005148
Alex Wu4539b082012-03-01 12:57:46 +08005149 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005150 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005151 wl_list_for_each(view, &surface->views, surface_link)
5152 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005153
Rafael Antognolli03b16592013-12-03 15:35:42 -02005154 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005155 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005156 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005157}
5158
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005159static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005160shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005161{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005162 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005163 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005164 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005165
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005166 assert(shsurf);
5167
5168 shell = shsurf->shell;
5169
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005170 if (!weston_surface_is_mapped(es) &&
5171 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005172 remove_popup_grab(shsurf);
5173 }
5174
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005175 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005176 return;
5177
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005178 if (shsurf->has_next_geometry) {
5179 shsurf->geometry = shsurf->next_geometry;
5180 shsurf->has_next_geometry = false;
5181 shsurf->has_set_geometry = true;
5182 } else if (!shsurf->has_set_geometry) {
5183 surface_subsurfaces_boundingbox(shsurf->surface,
5184 &shsurf->geometry.x,
5185 &shsurf->geometry.y,
5186 &shsurf->geometry.width,
5187 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005188 }
5189
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005190 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005191 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005192 type_changed = 1;
5193 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005194
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005195 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005196 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005197 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005198 shsurf->last_width != es->width ||
5199 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005200 float from_x, from_y;
5201 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005202
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005203 if (shsurf->resize_edges) {
5204 sx = 0;
5205 sy = 0;
5206 }
5207
5208 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5209 sx = shsurf->last_width - es->width;
5210 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5211 sy = shsurf->last_height - es->height;
5212
5213 shsurf->last_width = es->width;
5214 shsurf->last_height = es->height;
5215
Jason Ekstranda7af7042013-10-12 22:38:11 -05005216 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5217 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005218 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005219 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005220 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005221 }
5222}
5223
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005224static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005225
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005226static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005227desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005228{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005229 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005230 struct desktop_shell *shell =
5231 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005232
5233 shell->child.process.pid = 0;
5234 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005235
5236 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5237 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005238 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005239 shell->child.deathstamp = time;
5240 shell->child.deathcount = 0;
5241 }
5242
5243 shell->child.deathcount++;
5244 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005245 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005246 return;
5247 }
5248
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005249 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005250 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005251 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005252}
5253
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005254static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005255desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5256{
5257 struct desktop_shell *shell;
5258
5259 shell = container_of(listener, struct desktop_shell,
5260 child.client_destroy_listener);
5261
5262 shell->child.client = NULL;
5263}
5264
5265static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005266launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005267{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005268 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005269
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005270 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005271 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005272 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005273 desktop_shell_sigchld);
5274
5275 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005276 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005277
5278 shell->child.client_destroy_listener.notify =
5279 desktop_shell_client_destroy;
5280 wl_client_add_destroy_listener(shell->child.client,
5281 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005282}
5283
5284static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005285handle_shell_client_destroy(struct wl_listener *listener, void *data)
5286{
5287 struct shell_client *sc =
5288 container_of(listener, struct shell_client, destroy_listener);
5289
5290 if (sc->ping_timer)
5291 wl_event_source_remove(sc->ping_timer);
5292 free(sc);
5293}
5294
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005295static struct shell_client *
5296shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5297 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005298{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005299 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005300
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005301 sc = zalloc(sizeof *sc);
5302 if (sc == NULL) {
5303 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005304 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005305 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005306
5307 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005308 if (sc->resource == NULL) {
5309 free(sc);
5310 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005311 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005312 }
5313
5314 sc->client = client;
5315 sc->shell = shell;
5316 sc->destroy_listener.notify = handle_shell_client_destroy;
5317 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5318
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005319 return sc;
5320}
5321
5322static void
5323bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5324{
5325 struct desktop_shell *shell = data;
5326 struct shell_client *sc;
5327
5328 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5329 if (sc)
5330 wl_resource_set_implementation(sc->resource,
5331 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005332 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005333}
5334
5335static void
5336bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5337{
5338 struct desktop_shell *shell = data;
5339 struct shell_client *sc;
5340
5341 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5342 if (sc)
5343 wl_resource_set_dispatcher(sc->resource,
5344 xdg_shell_unversioned_dispatch,
5345 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005346}
5347
5348static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005349unbind_desktop_shell(struct wl_resource *resource)
5350{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005351 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005352
5353 if (shell->locked)
5354 resume_desktop(shell);
5355
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005356 shell->child.desktop_shell = NULL;
5357 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005358}
5359
5360static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005361bind_desktop_shell(struct wl_client *client,
5362 void *data, uint32_t version, uint32_t id)
5363{
Tiago Vignattibe143262012-04-16 17:31:41 +03005364 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005365 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005366
Jason Ekstranda85118c2013-06-27 20:17:02 -05005367 resource = wl_resource_create(client, &desktop_shell_interface,
Jonny Lamb765760d2014-08-20 15:53:19 +02005368 MIN(version, 3), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005369
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005370 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005371 wl_resource_set_implementation(resource,
5372 &desktop_shell_implementation,
5373 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005374 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005375
5376 if (version < 2)
5377 shell_fade_startup(shell);
5378
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005379 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005380 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005381
5382 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5383 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005384}
5385
Pekka Paalanen6e168112011-11-24 11:34:05 +02005386static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005387screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005388{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005389 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005390 struct weston_view *view;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005391 struct weston_layer_entry *prev;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005392
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005393 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005394 return;
5395
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005396 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005397 if (!shell->locked)
5398 return;
5399
Jason Ekstranda7af7042013-10-12 22:38:11 -05005400 view = container_of(surface->views.next, struct weston_view, surface_link);
5401 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005402
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005403 if (wl_list_empty(&view->layer_link.link)) {
5404 prev = container_of(shell->lock_layer.view_list.link.prev,
5405 struct weston_layer_entry, link);
5406 weston_layer_entry_insert(prev, &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005407 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005408 wl_event_source_timer_update(shell->screensaver.timer,
5409 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005410 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005411 }
5412}
5413
5414static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005415screensaver_set_surface(struct wl_client *client,
5416 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005417 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005418 struct wl_resource *output_resource)
5419{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005420 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005421 struct weston_surface *surface =
5422 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005423 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005424 struct weston_view *view, *next;
5425
5426 /* Make sure we only have one view */
5427 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5428 weston_view_destroy(view);
5429 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005430
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005431 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005432 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005433 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005434}
5435
5436static const struct screensaver_interface screensaver_implementation = {
5437 screensaver_set_surface
5438};
5439
5440static void
5441unbind_screensaver(struct wl_resource *resource)
5442{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005443 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005444
Pekka Paalanen77346a62011-11-30 16:26:35 +02005445 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005446}
5447
5448static void
5449bind_screensaver(struct wl_client *client,
5450 void *data, uint32_t version, uint32_t id)
5451{
Tiago Vignattibe143262012-04-16 17:31:41 +03005452 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005453 struct wl_resource *resource;
5454
Jason Ekstranda85118c2013-06-27 20:17:02 -05005455 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005456
Pekka Paalanen77346a62011-11-30 16:26:35 +02005457 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005458 wl_resource_set_implementation(resource,
5459 &screensaver_implementation,
5460 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005461 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005462 return;
5463 }
5464
5465 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5466 "interface object already bound");
Pekka Paalanen6e168112011-11-24 11:34:05 +02005467}
5468
Kristian Høgsberg07045392012-02-19 18:52:44 -05005469struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005470 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005471 struct weston_surface *current;
5472 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005473 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005474 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005475};
5476
5477static void
5478switcher_next(struct switcher *switcher)
5479{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005480 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005481 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005482 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005483 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005484
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005485 /* temporary re-display minimized surfaces */
5486 struct weston_view *tmp;
5487 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005488 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5489 weston_layer_entry_remove(&view->layer_link);
5490 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005491 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5492 *minimized = view;
5493 }
5494
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005495 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005496 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005497 if (shsurf &&
5498 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5499 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005500 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005501 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005502 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005503 next = view->surface;
5504 prev = view->surface;
5505 view->alpha = 0.25;
5506 weston_view_geometry_dirty(view);
5507 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005508 }
Alex Wu1659daa2012-04-01 20:13:09 +08005509
Jason Ekstranda7af7042013-10-12 22:38:11 -05005510 if (is_black_surface(view->surface, NULL)) {
5511 view->alpha = 0.25;
5512 weston_view_geometry_dirty(view);
5513 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005514 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005515 }
5516
5517 if (next == NULL)
5518 next = first;
5519
Alex Wu07b26062012-03-12 16:06:01 +08005520 if (next == NULL)
5521 return;
5522
Kristian Høgsberg07045392012-02-19 18:52:44 -05005523 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005524 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005525
5526 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005527 wl_list_for_each(view, &next->views, surface_link)
5528 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005529
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005530 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005531 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005532 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005533}
5534
5535static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005536switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005537{
5538 struct switcher *switcher =
5539 container_of(listener, struct switcher, listener);
5540
5541 switcher_next(switcher);
5542}
5543
5544static void
Daniel Stone351eb612012-05-31 15:27:47 -04005545switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005546{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005547 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005548 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005549 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005550
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005551 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005552 if (is_focus_view(view))
5553 continue;
5554
Jason Ekstranda7af7042013-10-12 22:38:11 -05005555 view->alpha = 1.0;
5556 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005557 }
5558
Alex Wu07b26062012-03-12 16:06:01 +08005559 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005560 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005561 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005562 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005563 weston_keyboard_end_grab(keyboard);
5564 if (keyboard->input_method_resource)
5565 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005566
5567 /* re-hide surfaces that were temporary shown during the switch */
5568 struct weston_view **minimized;
5569 wl_array_for_each(minimized, &switcher->minimized_array) {
5570 /* with the exception of the current selected */
5571 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005572 weston_layer_entry_remove(&(*minimized)->layer_link);
5573 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005574 weston_view_damage_below(*minimized);
5575 }
5576 }
5577 wl_array_release(&switcher->minimized_array);
5578
Kristian Høgsberg07045392012-02-19 18:52:44 -05005579 free(switcher);
5580}
5581
5582static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005583switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005584 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005585{
5586 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005587 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005588
Daniel Stonec9785ea2012-05-30 16:31:52 +01005589 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005590 switcher_next(switcher);
5591}
5592
5593static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005594switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005595 uint32_t mods_depressed, uint32_t mods_latched,
5596 uint32_t mods_locked, uint32_t group)
5597{
5598 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005599 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005600
Daniel Stone351eb612012-05-31 15:27:47 -04005601 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5602 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005603}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005604
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005605static void
5606switcher_cancel(struct weston_keyboard_grab *grab)
5607{
5608 struct switcher *switcher = container_of(grab, struct switcher, grab);
5609
5610 switcher_destroy(switcher);
5611}
5612
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005613static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005614 switcher_key,
5615 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005616 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005617};
5618
5619static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005620switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005621 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005622{
Tiago Vignattibe143262012-04-16 17:31:41 +03005623 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005624 struct switcher *switcher;
5625
5626 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005627 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005628 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005629 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005630 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005631 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005632
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005633 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005634 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005635 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005636 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5637 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005638 switcher_next(switcher);
5639}
5640
Pekka Paalanen3c647232011-12-22 13:43:43 +02005641static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005642backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005643 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005644{
5645 struct weston_compositor *compositor = data;
5646 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005647 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005648
5649 /* TODO: we're limiting to simple use cases, where we assume just
5650 * control on the primary display. We'd have to extend later if we
5651 * ever get support for setting backlights on random desktop LCD
5652 * panels though */
5653 output = get_default_output(compositor);
5654 if (!output)
5655 return;
5656
5657 if (!output->set_backlight)
5658 return;
5659
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005660 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5661 backlight_new = output->backlight_current - 25;
5662 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5663 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005664
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005665 if (backlight_new < 5)
5666 backlight_new = 5;
5667 if (backlight_new > 255)
5668 backlight_new = 255;
5669
5670 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005671 output->set_backlight(output, output->backlight_current);
5672}
5673
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005674struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005675 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005676 struct weston_seat *seat;
5677 uint32_t key[2];
5678 int key_released[2];
5679};
5680
5681static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005682debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005683 uint32_t key, uint32_t state)
5684{
5685 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005686 struct weston_compositor *ec = db->seat->compositor;
5687 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005688 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005689 uint32_t serial;
5690 int send = 0, terminate = 0;
5691 int check_binding = 1;
5692 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005693 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005694
5695 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5696 /* Do not run bindings on key releases */
5697 check_binding = 0;
5698
5699 for (i = 0; i < 2; i++)
5700 if (key == db->key[i])
5701 db->key_released[i] = 1;
5702
5703 if (db->key_released[0] && db->key_released[1]) {
5704 /* All key releases been swalled so end the grab */
5705 terminate = 1;
5706 } else if (key != db->key[0] && key != db->key[1]) {
5707 /* Should not swallow release of other keys */
5708 send = 1;
5709 }
5710 } else if (key == db->key[0] && !db->key_released[0]) {
5711 /* Do not check bindings for the first press of the binding
5712 * key. This allows it to be used as a debug shortcut.
5713 * We still need to swallow this event. */
5714 check_binding = 0;
5715 } else if (db->key[1]) {
5716 /* If we already ran a binding don't process another one since
5717 * we can't keep track of all the binding keys that were
5718 * pressed in order to swallow the release events. */
5719 send = 1;
5720 check_binding = 0;
5721 }
5722
5723 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005724 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5725 key, state)) {
5726 /* We ran a binding so swallow the press and keep the
5727 * grab to swallow the released too. */
5728 send = 0;
5729 terminate = 0;
5730 db->key[1] = key;
5731 } else {
5732 /* Terminate the grab since the key pressed is not a
5733 * debug binding key. */
5734 send = 1;
5735 terminate = 1;
5736 }
5737 }
5738
5739 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005740 serial = wl_display_next_serial(display);
5741 resource_list = &grab->keyboard->focus_resource_list;
5742 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005743 wl_keyboard_send_key(resource, serial, time, key, state);
5744 }
5745 }
5746
5747 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005748 weston_keyboard_end_grab(grab->keyboard);
5749 if (grab->keyboard->input_method_resource)
5750 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005751 free(db);
5752 }
5753}
5754
5755static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005756debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005757 uint32_t mods_depressed, uint32_t mods_latched,
5758 uint32_t mods_locked, uint32_t group)
5759{
5760 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005761 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005762
Neil Roberts96d790e2013-09-19 17:32:00 +01005763 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005764
Neil Roberts96d790e2013-09-19 17:32:00 +01005765 wl_resource_for_each(resource, resource_list) {
5766 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5767 mods_latched, mods_locked, group);
5768 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005769}
5770
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005771static void
5772debug_binding_cancel(struct weston_keyboard_grab *grab)
5773{
5774 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5775
5776 weston_keyboard_end_grab(grab->keyboard);
5777 free(db);
5778}
5779
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005780struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005781 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005782 debug_binding_modifiers,
5783 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005784};
5785
5786static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005787debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005788{
5789 struct debug_binding_grab *grab;
5790
5791 grab = calloc(1, sizeof *grab);
5792 if (!grab)
5793 return;
5794
5795 grab->seat = (struct weston_seat *) seat;
5796 grab->key[0] = key;
5797 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005798 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005799}
5800
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005801static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005802force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005803 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005804{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005805 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005806 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005807 struct desktop_shell *shell = data;
5808 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005809 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005810
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005811 focus_surface = seat->keyboard->focus;
5812 if (!focus_surface)
5813 return;
5814
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005815 wl_signal_emit(&compositor->kill_signal, focus_surface);
5816
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005817 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005818 wl_client_get_credentials(client, &pid, NULL, NULL);
5819
5820 /* Skip clients that we launched ourselves (the credentials of
5821 * the socketpair is ours) */
5822 if (pid == getpid())
5823 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005824
Daniel Stone325fc2d2012-05-30 16:31:58 +01005825 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005826}
5827
5828static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005829workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005830 uint32_t key, void *data)
5831{
5832 struct desktop_shell *shell = data;
5833 unsigned int new_index = shell->workspaces.current;
5834
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005835 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005836 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005837 if (new_index != 0)
5838 new_index--;
5839
5840 change_workspace(shell, new_index);
5841}
5842
5843static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005844workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005845 uint32_t key, void *data)
5846{
5847 struct desktop_shell *shell = data;
5848 unsigned int new_index = shell->workspaces.current;
5849
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005850 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005851 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005852 if (new_index < shell->workspaces.num - 1)
5853 new_index++;
5854
5855 change_workspace(shell, new_index);
5856}
5857
5858static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005859workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005860 uint32_t key, void *data)
5861{
5862 struct desktop_shell *shell = data;
5863 unsigned int new_index;
5864
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005865 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005866 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005867 new_index = key - KEY_F1;
5868 if (new_index >= shell->workspaces.num)
5869 new_index = shell->workspaces.num - 1;
5870
5871 change_workspace(shell, new_index);
5872}
5873
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005874static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005875workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005876 uint32_t key, void *data)
5877{
5878 struct desktop_shell *shell = data;
5879 unsigned int new_index = shell->workspaces.current;
5880
5881 if (shell->locked)
5882 return;
5883
5884 if (new_index != 0)
5885 new_index--;
5886
5887 take_surface_to_workspace_by_seat(shell, seat, new_index);
5888}
5889
5890static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005891workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005892 uint32_t key, void *data)
5893{
5894 struct desktop_shell *shell = data;
5895 unsigned int new_index = shell->workspaces.current;
5896
5897 if (shell->locked)
5898 return;
5899
5900 if (new_index < shell->workspaces.num - 1)
5901 new_index++;
5902
5903 take_surface_to_workspace_by_seat(shell, seat, new_index);
5904}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005905
5906static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005907shell_reposition_view_on_output_destroy(struct weston_view *view)
5908{
5909 struct weston_output *output, *first_output;
5910 struct weston_compositor *ec = view->surface->compositor;
5911 struct shell_surface *shsurf;
5912 float x, y;
5913 int visible;
5914
5915 x = view->geometry.x;
5916 y = view->geometry.y;
5917
5918 /* At this point the destroyed output is not in the list anymore.
5919 * If the view is still visible somewhere, we leave where it is,
5920 * otherwise, move it to the first output. */
5921 visible = 0;
5922 wl_list_for_each(output, &ec->output_list, link) {
5923 if (pixman_region32_contains_point(&output->region,
5924 x, y, NULL)) {
5925 visible = 1;
5926 break;
5927 }
5928 }
5929
5930 if (!visible) {
5931 first_output = container_of(ec->output_list.next,
5932 struct weston_output, link);
5933
5934 x = first_output->x + first_output->width / 4;
5935 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08005936
5937 weston_view_set_position(view, x, y);
5938 } else {
5939 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005940 }
5941
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005942
5943 shsurf = get_shell_surface(view->surface);
5944
5945 if (shsurf) {
5946 shsurf->saved_position_valid = false;
5947 shsurf->next_state.maximized = false;
5948 shsurf->next_state.fullscreen = false;
5949 shsurf->state_changed = true;
5950 }
5951}
5952
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005953void
5954shell_for_each_layer(struct desktop_shell *shell,
5955 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005956{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005957 struct workspace **ws;
5958
5959 func(shell, &shell->fullscreen_layer, data);
5960 func(shell, &shell->panel_layer, data);
5961 func(shell, &shell->background_layer, data);
5962 func(shell, &shell->lock_layer, data);
5963 func(shell, &shell->input_panel_layer, data);
5964
5965 wl_array_for_each(ws, &shell->workspaces.array)
5966 func(shell, &(*ws)->layer, data);
5967}
5968
5969static void
5970shell_output_destroy_move_layer(struct desktop_shell *shell,
5971 struct weston_layer *layer,
5972 void *data)
5973{
5974 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005975 struct weston_view *view;
5976
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005977 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005978 if (view->output != output)
5979 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005980
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005981 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005982 }
5983}
5984
5985static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005986handle_output_destroy(struct wl_listener *listener, void *data)
5987{
5988 struct shell_output *output_listener =
5989 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005990 struct weston_output *output = output_listener->output;
5991 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005992
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005993 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005994
Xiong Zhang6b481422013-10-23 13:58:32 +08005995 wl_list_remove(&output_listener->destroy_listener.link);
5996 wl_list_remove(&output_listener->link);
5997 free(output_listener);
5998}
5999
6000static void
6001create_shell_output(struct desktop_shell *shell,
6002 struct weston_output *output)
6003{
6004 struct shell_output *shell_output;
6005
6006 shell_output = zalloc(sizeof *shell_output);
6007 if (shell_output == NULL)
6008 return;
6009
6010 shell_output->output = output;
6011 shell_output->shell = shell;
6012 shell_output->destroy_listener.notify = handle_output_destroy;
6013 wl_signal_add(&output->destroy_signal,
6014 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07006015 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006016}
6017
6018static void
6019handle_output_create(struct wl_listener *listener, void *data)
6020{
6021 struct desktop_shell *shell =
6022 container_of(listener, struct desktop_shell, output_create_listener);
6023 struct weston_output *output = (struct weston_output *)data;
6024
6025 create_shell_output(shell, output);
6026}
6027
6028static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006029handle_output_move_layer(struct desktop_shell *shell,
6030 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006031{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006032 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006033 struct weston_view *view;
6034 float x, y;
6035
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006036 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006037 if (view->output != output)
6038 continue;
6039
6040 x = view->geometry.x + output->move_x;
6041 y = view->geometry.y + output->move_y;
6042 weston_view_set_position(view, x, y);
6043 }
6044}
6045
6046static void
6047handle_output_move(struct wl_listener *listener, void *data)
6048{
6049 struct desktop_shell *shell;
6050
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006051 shell = container_of(listener, struct desktop_shell,
6052 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006053
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006054 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006055}
6056
6057static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006058setup_output_destroy_handler(struct weston_compositor *ec,
6059 struct desktop_shell *shell)
6060{
6061 struct weston_output *output;
6062
6063 wl_list_init(&shell->output_list);
6064 wl_list_for_each(output, &ec->output_list, link)
6065 create_shell_output(shell, output);
6066
6067 shell->output_create_listener.notify = handle_output_create;
6068 wl_signal_add(&ec->output_created_signal,
6069 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006070
6071 shell->output_move_listener.notify = handle_output_move;
6072 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006073}
6074
6075static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006076shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006077{
Tiago Vignattibe143262012-04-16 17:31:41 +03006078 struct desktop_shell *shell =
6079 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006080 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006081 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006082
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006083 /* Force state to unlocked so we don't try to fade */
6084 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006085 if (shell->child.client)
6086 wl_client_destroy(shell->child.client);
6087
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006088 wl_list_remove(&shell->idle_listener.link);
6089 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006090
6091 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006092
Xiong Zhang6b481422013-10-23 13:58:32 +08006093 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6094 wl_list_remove(&shell_output->destroy_listener.link);
6095 wl_list_remove(&shell_output->link);
6096 free(shell_output);
6097 }
6098
6099 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006100 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006101
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006102 wl_array_for_each(ws, &shell->workspaces.array)
6103 workspace_destroy(*ws);
6104 wl_array_release(&shell->workspaces.array);
6105
Pekka Paalanen3c647232011-12-22 13:43:43 +02006106 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006107 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006108 free(shell);
6109}
6110
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006111static void
6112shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6113{
6114 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006115 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006116
6117 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006118 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6119 MODIFIER_CTRL | MODIFIER_ALT,
6120 terminate_binding, ec);
6121 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6122 click_to_activate_binding,
6123 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006124 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6125 click_to_activate_binding,
6126 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006127 weston_compositor_add_touch_binding(ec, 0,
6128 touch_to_activate_binding,
6129 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006130 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6131 MODIFIER_SUPER | MODIFIER_ALT,
6132 surface_opacity_binding, NULL);
6133 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6134 MODIFIER_SUPER, zoom_axis_binding,
6135 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006136
6137 /* configurable bindings */
6138 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006139 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6140 zoom_key_binding, NULL);
6141 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6142 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006143 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6144 maximize_binding, NULL);
6145 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6146 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006147 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6148 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006149 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006150 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6151 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006152 weston_compositor_add_button_binding(ec, BTN_LEFT,
6153 mod | MODIFIER_SHIFT,
6154 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006155
6156 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6157 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6158 rotate_binding, NULL);
6159
Daniel Stone325fc2d2012-05-30 16:31:58 +01006160 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6161 shell);
6162 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6163 ec);
6164 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6165 backlight_binding, ec);
6166 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6167 ec);
6168 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6169 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006170 weston_compositor_add_key_binding(ec, KEY_K, mod,
6171 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006172 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6173 workspace_up_binding, shell);
6174 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6175 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006176 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6177 workspace_move_surface_up_binding,
6178 shell);
6179 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6180 workspace_move_surface_down_binding,
6181 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006182
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006183 if (shell->exposay_modifier)
6184 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6185 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006186
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006187 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6188 if (shell->workspaces.num > 1) {
6189 num_workspace_bindings = shell->workspaces.num;
6190 if (num_workspace_bindings > 6)
6191 num_workspace_bindings = 6;
6192 for (i = 0; i < num_workspace_bindings; i++)
6193 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6194 workspace_f_binding,
6195 shell);
6196 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006197
6198 /* Debug bindings */
6199 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6200 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006201}
6202
Jason Ekstrand024177c2014-04-21 19:42:58 -05006203static void
6204handle_seat_created(struct wl_listener *listener, void *data)
6205{
6206 struct weston_seat *seat = data;
6207
6208 create_shell_seat(seat);
6209}
6210
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006211WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006212module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006213 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006214{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006215 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006216 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006217 struct workspace **pws;
6218 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006219 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006220
Peter Huttererf3d62272013-08-08 11:57:05 +10006221 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006222 if (shell == NULL)
6223 return -1;
6224
Kristian Høgsberg75840622011-09-06 13:48:16 -04006225 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006226
6227 shell->destroy_listener.notify = shell_destroy;
6228 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006229 shell->idle_listener.notify = idle_handler;
6230 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6231 shell->wake_listener.notify = wake_handler;
6232 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006233
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006234 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006235 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006236 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006237 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006238 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006239 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006240 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006241 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006242 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006243 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006244 ec->shell_interface.set_window_geometry = set_window_geometry;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006245
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006246 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6247 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006248 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6249 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006250 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006251
6252 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006253 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006254
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006255 if (input_panel_setup(shell) < 0)
6256 return -1;
6257
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006258 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006259
Daniel Stonedf8133b2013-11-19 11:37:14 +01006260 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6261 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6262
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006263 for (i = 0; i < shell->workspaces.num; i++) {
6264 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6265 if (pws == NULL)
6266 return -1;
6267
6268 *pws = workspace_create();
6269 if (*pws == NULL)
6270 return -1;
6271 }
6272 activate_workspace(shell, 0);
6273
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006274 weston_layer_init(&shell->minimized_layer, NULL);
6275
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006276 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006277 wl_list_init(&shell->workspaces.animation.link);
6278 shell->workspaces.animation.frame = animate_workspace_change_frame;
6279
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006280 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006281 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006282 return -1;
6283
Rafael Antognollie2a34552013-12-03 15:35:45 -02006284 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6285 shell, bind_xdg_shell) == NULL)
6286 return -1;
6287
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006288 if (wl_global_create(ec->wl_display,
Jonny Lamb765760d2014-08-20 15:53:19 +02006289 &desktop_shell_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006290 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006291 return -1;
6292
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006293 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6294 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006295 return -1;
6296
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006297 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6298 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006299 return -1;
6300
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006301 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006302
Jonny Lamb765760d2014-08-20 15:53:19 +02006303 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6304
Xiong Zhang6b481422013-10-23 13:58:32 +08006305 setup_output_destroy_handler(ec, shell);
6306
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006307 loop = wl_display_get_event_loop(ec->wl_display);
6308 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006309
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006310 shell->screensaver.timer =
6311 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6312
Jason Ekstrand024177c2014-04-21 19:42:58 -05006313 wl_list_for_each(seat, &ec->seat_list, link)
6314 handle_seat_created(NULL, seat);
6315 shell->seat_create_listener.notify = handle_seat_created;
6316 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006317
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006318 screenshooter_create(ec);
6319
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006320 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006321
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006322 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006323
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006324 return 0;
6325}