blob: d756076820752007d0426908fa454fd20f525c92 [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
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002985 weston_pointer_move(pointer, x, y);
2986
Neil Roberts96d790e2013-09-19 17:32:00 +01002987 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002988 weston_view_from_global_fixed(pointer->focus,
2989 pointer->x, pointer->y,
2990 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002991 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002992 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002993}
2994
2995static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002996popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002997 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002998{
2999 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003000 struct shell_seat *shseat =
3001 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01003002 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003003 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003004 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003005 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003006
Neil Roberts96d790e2013-09-19 17:32:00 +01003007 resource_list = &grab->pointer->focus_resource_list;
3008 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003009 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003010 wl_resource_for_each(resource, resource_list) {
3011 wl_pointer_send_button(resource, serial,
3012 time, button, state);
3013 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003014 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003015 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003016 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003017 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003018 }
3019
Daniel Stone4dbadb12012-05-30 16:31:51 +01003020 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003021 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003022}
3023
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003024static void
3025popup_grab_cancel(struct weston_pointer_grab *grab)
3026{
3027 popup_grab_end(grab->pointer);
3028}
3029
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003030static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003031 popup_grab_focus,
3032 popup_grab_motion,
3033 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003034 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003035};
3036
3037static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003038shell_surface_send_popup_done(struct shell_surface *shsurf)
3039{
3040 if (shell_surface_is_wl_shell_surface(shsurf))
3041 wl_shell_surface_send_popup_done(shsurf->resource);
3042 else if (shell_surface_is_xdg_popup(shsurf))
3043 xdg_popup_send_popup_done(shsurf->resource,
3044 shsurf->popup.serial);
3045}
3046
3047static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003048popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003049{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003050 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003051 struct shell_seat *shseat =
3052 container_of(grab, struct shell_seat, popup_grab.grab);
3053 struct shell_surface *shsurf;
3054 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003055
3056 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003057 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003058 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003059 shseat->popup_grab.grab.interface = NULL;
3060 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003061 /* Send the popup_done event to all the popups open */
3062 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003063 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003064 shsurf->popup.shseat = NULL;
3065 if (prev) {
3066 wl_list_init(&prev->popup.grab_link);
3067 }
3068 prev = shsurf;
3069 }
3070 wl_list_init(&prev->popup.grab_link);
3071 wl_list_init(&shseat->popup_grab.surfaces_list);
3072 }
3073}
3074
3075static void
3076add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
3077{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003078 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003079
3080 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003081 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003082 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003083 /* We must make sure here that this popup was opened after
3084 * a mouse press, and not just by moving around with other
3085 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04003086 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003087 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003088
Rob Bradforddfe31052013-06-26 19:49:11 +01003089 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003090 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003091 } else {
3092 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003093 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003094}
3095
3096static void
3097remove_popup_grab(struct shell_surface *shsurf)
3098{
3099 struct shell_seat *shseat = shsurf->popup.shseat;
3100
3101 wl_list_remove(&shsurf->popup.grab_link);
3102 wl_list_init(&shsurf->popup.grab_link);
3103 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003104 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003105 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003106 }
3107}
3108
3109static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003110shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003111{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003112 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003113 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003114
Jason Ekstranda7af7042013-10-12 22:38:11 -05003115 shsurf->surface->output = parent_view->output;
3116 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003117
Jason Ekstranda7af7042013-10-12 22:38:11 -05003118 weston_view_set_transform_parent(shsurf->view, parent_view);
3119 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3120 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003121
Jonny Lambe84ab4e2014-08-06 11:50:12 +02003122 if (shseat->seat->pointer &&
3123 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003124 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003125 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003126 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003127 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003128 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003129}
3130
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003131static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003132 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003133 shell_surface_move,
3134 shell_surface_resize,
3135 shell_surface_set_toplevel,
3136 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003137 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003138 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003139 shell_surface_set_maximized,
3140 shell_surface_set_title,
3141 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003142};
3143
3144static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003145destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003146{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003147 struct shell_surface *child, *next;
3148
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003149 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3150
Giulio Camuffo5085a752013-03-25 21:42:45 +01003151 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3152 remove_popup_grab(shsurf);
3153 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003154
Alex Wubd3354b2012-04-17 17:20:49 +08003155 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003156 shell_surface_is_top_fullscreen(shsurf))
3157 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003158
Jason Ekstranda7af7042013-10-12 22:38:11 -05003159 if (shsurf->fullscreen.black_view)
3160 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003161
Alex Wubd3354b2012-04-17 17:20:49 +08003162 /* As destroy_resource() use wl_list_for_each_safe(),
3163 * we can always remove the listener.
3164 */
3165 wl_list_remove(&shsurf->surface_destroy_listener.link);
3166 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003167 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003168
Jason Ekstranda7af7042013-10-12 22:38:11 -05003169 weston_view_destroy(shsurf->view);
3170
Philip Withnall648a4dd2013-11-25 18:01:44 +00003171 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003172 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3173 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003174
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003175 wl_list_remove(&shsurf->link);
3176 free(shsurf);
3177}
3178
3179static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003180shell_destroy_shell_surface(struct wl_resource *resource)
3181{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003182 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003183
Bryan Caina46b9462014-04-04 17:41:24 -05003184 if (!wl_list_empty(&shsurf->popup.grab_link))
3185 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003186 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003187}
3188
3189static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003190shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003191{
3192 struct shell_surface *shsurf = container_of(listener,
3193 struct shell_surface,
3194 surface_destroy_listener);
3195
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003196 if (shsurf->resource)
3197 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003198
3199 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003200}
3201
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003202static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003203fade_out_done(struct weston_view_animation *animation, void *data)
3204{
3205 struct shell_surface *shsurf = data;
3206
3207 weston_surface_destroy(shsurf->surface);
3208}
3209
3210static void
3211handle_resource_destroy(struct wl_listener *listener, void *data)
3212{
3213 struct shell_surface *shsurf =
3214 container_of(listener, struct shell_surface,
3215 resource_destroy_listener);
3216
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003217 if (!weston_surface_is_mapped(shsurf->surface))
3218 return;
3219
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003220 shsurf->surface->ref_count++;
3221
3222 pixman_region32_fini(&shsurf->surface->pending.input);
3223 pixman_region32_init(&shsurf->surface->pending.input);
3224 pixman_region32_fini(&shsurf->surface->input);
3225 pixman_region32_init(&shsurf->surface->input);
Jonny Lambf322f8e2014-08-12 15:13:30 +02003226 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3227 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3228 fade_out_done, shsurf);
3229 } else {
3230 weston_surface_destroy(shsurf->surface);
3231 }
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003232}
3233
3234static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003235shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003236
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003237struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003238get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003239{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003240 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003241 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003242 else
3243 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003244}
3245
Rafael Antognollie2a34552013-12-03 15:35:45 -02003246static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003247create_common_surface(struct shell_client *owner, void *shell,
3248 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003249 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003250{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003251 struct shell_surface *shsurf;
3252
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003253 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003254 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003255 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003256 }
3257
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003258 shsurf = calloc(1, sizeof *shsurf);
3259 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003260 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003261 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003262 }
3263
Jason Ekstranda7af7042013-10-12 22:38:11 -05003264 shsurf->view = weston_view_create(surface);
3265 if (!shsurf->view) {
3266 weston_log("no memory to allocate shell surface\n");
3267 free(shsurf);
3268 return NULL;
3269 }
3270
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003271 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003272 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003273
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003274 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3275 wl_resource_add_destroy_listener(surface->resource,
3276 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003277 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003278
Tiago Vignattibc052c92012-04-19 16:18:18 +03003279 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003280 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003281 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003282 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003283 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003284 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003285 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3286 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003287 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003288 wl_list_init(&shsurf->fullscreen.transform.link);
3289
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003290 shsurf->output = get_default_output(shsurf->shell->compositor);
3291
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003292 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003293 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003294 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003295 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003296
3297 /* init link so its safe to always remove it in destroy_shell_surface */
3298 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003299 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003300
Pekka Paalanen460099f2012-01-20 16:48:25 +02003301 /* empty when not in use */
3302 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003303 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003304
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003305 wl_list_init(&shsurf->workspace_transform.link);
3306
Philip Withnall648a4dd2013-11-25 18:01:44 +00003307 wl_list_init(&shsurf->children_link);
3308 wl_list_init(&shsurf->children_list);
3309 shsurf->parent = NULL;
3310
Pekka Paalanen98262232011-12-01 10:42:22 +02003311 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003312
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003313 shsurf->client = client;
3314
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003315 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003316}
3317
Rafael Antognollie2a34552013-12-03 15:35:45 -02003318static struct shell_surface *
3319create_shell_surface(void *shell, struct weston_surface *surface,
3320 const struct weston_shell_client *client)
3321{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003322 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003323}
3324
Jason Ekstranda7af7042013-10-12 22:38:11 -05003325static struct weston_view *
3326get_primary_view(void *shell, struct shell_surface *shsurf)
3327{
3328 return shsurf->view;
3329}
3330
Tiago Vignattibc052c92012-04-19 16:18:18 +03003331static void
3332shell_get_shell_surface(struct wl_client *client,
3333 struct wl_resource *resource,
3334 uint32_t id,
3335 struct wl_resource *surface_resource)
3336{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003337 struct weston_surface *surface =
3338 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003339 struct shell_client *sc = wl_resource_get_user_data(resource);
3340 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003341 struct shell_surface *shsurf;
3342
3343 if (get_shell_surface(surface)) {
3344 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003345 WL_DISPLAY_ERROR_INVALID_OBJECT,
3346 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003347 return;
3348 }
3349
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003350 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003351 if (!shsurf) {
3352 wl_resource_post_error(surface_resource,
3353 WL_DISPLAY_ERROR_INVALID_OBJECT,
3354 "surface->configure already set");
3355 return;
3356 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003357
Jason Ekstranda85118c2013-06-27 20:17:02 -05003358 shsurf->resource =
3359 wl_resource_create(client,
3360 &wl_shell_surface_interface, 1, id);
3361 wl_resource_set_implementation(shsurf->resource,
3362 &shell_surface_implementation,
3363 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003364}
3365
Rafael Antognollie2a34552013-12-03 15:35:45 -02003366static bool
3367shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3368{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003369 /* A shell surface without a resource is created from xwayland
3370 * and is considered a wl_shell surface for now. */
3371
3372 return shsurf->resource == NULL ||
3373 wl_resource_instance_of(shsurf->resource,
3374 &wl_shell_surface_interface,
3375 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003376}
3377
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003378static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003379 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003380};
3381
Rafael Antognollie2a34552013-12-03 15:35:45 -02003382/****************************
3383 * xdg-shell implementation */
3384
3385static void
3386xdg_surface_destroy(struct wl_client *client,
3387 struct wl_resource *resource)
3388{
3389 wl_resource_destroy(resource);
3390}
3391
3392static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003393xdg_surface_set_parent(struct wl_client *client,
3394 struct wl_resource *resource,
3395 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003396{
3397 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3398 struct weston_surface *parent;
3399
3400 if (parent_resource)
3401 parent = wl_resource_get_user_data(parent_resource);
3402 else
3403 parent = NULL;
3404
3405 shell_surface_set_parent(shsurf, parent);
3406}
3407
3408static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003409xdg_surface_set_app_id(struct wl_client *client,
3410 struct wl_resource *resource,
3411 const char *app_id)
3412{
3413 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3414
3415 free(shsurf->class);
3416 shsurf->class = strdup(app_id);
3417}
3418
3419static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003420xdg_surface_show_window_menu(struct wl_client *client,
3421 struct wl_resource *surface_resource,
3422 struct wl_resource *seat_resource,
3423 uint32_t serial,
3424 int32_t x,
3425 int32_t y)
3426{
3427 /* TODO */
3428}
3429
3430static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003431xdg_surface_set_title(struct wl_client *client,
3432 struct wl_resource *resource, const char *title)
3433{
3434 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3435
3436 set_title(shsurf, title);
3437}
3438
3439static void
3440xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3441 struct wl_resource *seat_resource, uint32_t serial)
3442{
3443 common_surface_move(resource, seat_resource, serial);
3444}
3445
3446static void
3447xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3448 struct wl_resource *seat_resource, uint32_t serial,
3449 uint32_t edges)
3450{
3451 common_surface_resize(resource, seat_resource, serial, edges);
3452}
3453
3454static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003455xdg_surface_ack_configure(struct wl_client *client,
3456 struct wl_resource *resource,
3457 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003458{
3459 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3460
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003461 if (shsurf->state_requested) {
3462 shsurf->next_state = shsurf->requested_state;
3463 shsurf->state_changed = true;
3464 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003465 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003466}
3467
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003468static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003469xdg_surface_set_window_geometry(struct wl_client *client,
3470 struct wl_resource *resource,
3471 int32_t x,
3472 int32_t y,
3473 int32_t width,
3474 int32_t height)
3475{
3476 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3477
3478 set_window_geometry(shsurf, x, y, width, height);
3479}
3480
3481static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003482xdg_surface_set_maximized(struct wl_client *client,
3483 struct wl_resource *resource)
3484{
3485 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3486
3487 shsurf->state_requested = true;
3488 shsurf->requested_state.maximized = true;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003489 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003490}
3491
3492static void
3493xdg_surface_unset_maximized(struct wl_client *client,
3494 struct wl_resource *resource)
3495{
3496 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3497
3498 shsurf->state_requested = true;
3499 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003500 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003501}
3502
3503static void
3504xdg_surface_set_fullscreen(struct wl_client *client,
3505 struct wl_resource *resource,
3506 struct wl_resource *output_resource)
3507{
3508 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3509 struct weston_output *output;
3510
3511 shsurf->state_requested = true;
3512 shsurf->requested_state.fullscreen = true;
3513
3514 if (output_resource)
3515 output = wl_resource_get_user_data(output_resource);
3516 else
3517 output = NULL;
3518
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003519 shell_surface_set_output(shsurf, output);
3520 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003521
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003522 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003523}
3524
3525static void
3526xdg_surface_unset_fullscreen(struct wl_client *client,
3527 struct wl_resource *resource)
3528{
3529 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3530
3531 shsurf->state_requested = true;
3532 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003533 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003534}
3535
3536static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003537xdg_surface_set_minimized(struct wl_client *client,
3538 struct wl_resource *resource)
3539{
3540 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3541
3542 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3543 return;
3544
3545 /* apply compositor's own minimization logic (hide) */
3546 set_minimized(shsurf->surface, 1);
3547}
3548
Rafael Antognollie2a34552013-12-03 15:35:45 -02003549static const struct xdg_surface_interface xdg_surface_implementation = {
3550 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003551 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003552 xdg_surface_set_title,
3553 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003554 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003555 xdg_surface_move,
3556 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003557 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003558 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003559 xdg_surface_set_maximized,
3560 xdg_surface_unset_maximized,
3561 xdg_surface_set_fullscreen,
3562 xdg_surface_unset_fullscreen,
3563 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003564};
3565
3566static void
3567xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003568 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003569{
3570 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003571 uint32_t *s;
3572 struct wl_array states;
3573 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003574
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003575 assert(shsurf);
3576
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003577 if (!shsurf->resource)
3578 return;
3579
3580 wl_array_init(&states);
3581 if (shsurf->requested_state.fullscreen) {
3582 s = wl_array_add(&states, sizeof *s);
3583 *s = XDG_SURFACE_STATE_FULLSCREEN;
3584 } else if (shsurf->requested_state.maximized) {
3585 s = wl_array_add(&states, sizeof *s);
3586 *s = XDG_SURFACE_STATE_MAXIMIZED;
3587 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003588 if (shsurf->resize_edges != 0) {
3589 s = wl_array_add(&states, sizeof *s);
3590 *s = XDG_SURFACE_STATE_RESIZING;
3591 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003592 if (shsurf->focus_count > 0) {
3593 s = wl_array_add(&states, sizeof *s);
3594 *s = XDG_SURFACE_STATE_ACTIVATED;
3595 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003596
3597 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3598 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3599
3600 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003601}
3602
3603static const struct weston_shell_client xdg_client = {
3604 xdg_send_configure
3605};
3606
3607static void
3608xdg_use_unstable_version(struct wl_client *client,
3609 struct wl_resource *resource,
3610 int32_t version)
3611{
3612 if (version > 1) {
3613 wl_resource_post_error(resource,
3614 1,
3615 "xdg-shell:: version not implemented yet.");
3616 return;
3617 }
3618}
3619
3620static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003621create_xdg_surface(struct shell_client *owner, void *shell,
3622 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003623 const struct weston_shell_client *client)
3624{
3625 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003626
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003627 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003628 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003629
3630 return shsurf;
3631}
3632
3633static void
3634xdg_get_xdg_surface(struct wl_client *client,
3635 struct wl_resource *resource,
3636 uint32_t id,
3637 struct wl_resource *surface_resource)
3638{
3639 struct weston_surface *surface =
3640 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003641 struct shell_client *sc = wl_resource_get_user_data(resource);
3642 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003643 struct shell_surface *shsurf;
3644
3645 if (get_shell_surface(surface)) {
3646 wl_resource_post_error(surface_resource,
3647 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003648 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003649 return;
3650 }
3651
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003652 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003653 if (!shsurf) {
3654 wl_resource_post_error(surface_resource,
3655 WL_DISPLAY_ERROR_INVALID_OBJECT,
3656 "surface->configure already set");
3657 return;
3658 }
3659
3660 shsurf->resource =
3661 wl_resource_create(client,
3662 &xdg_surface_interface, 1, id);
3663 wl_resource_set_implementation(shsurf->resource,
3664 &xdg_surface_implementation,
3665 shsurf, shell_destroy_shell_surface);
3666}
3667
3668static bool
3669shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3670{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003671 return shsurf->resource &&
3672 wl_resource_instance_of(shsurf->resource,
3673 &xdg_surface_interface,
3674 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003675}
3676
3677/* xdg-popup implementation */
3678
3679static void
3680xdg_popup_destroy(struct wl_client *client,
3681 struct wl_resource *resource)
3682{
3683 wl_resource_destroy(resource);
3684}
3685
Rafael Antognollie2a34552013-12-03 15:35:45 -02003686static const struct xdg_popup_interface xdg_popup_implementation = {
3687 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003688};
3689
3690static void
3691xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003692 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003693{
3694}
3695
3696static const struct weston_shell_client xdg_popup_client = {
3697 xdg_popup_send_configure
3698};
3699
3700static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003701create_xdg_popup(struct shell_client *owner, void *shell,
3702 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003703 const struct weston_shell_client *client,
3704 struct weston_surface *parent,
3705 struct shell_seat *seat,
3706 uint32_t serial,
3707 int32_t x, int32_t y)
3708{
3709 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003710
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003711 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003712 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003713 shsurf->popup.shseat = seat;
3714 shsurf->popup.serial = serial;
3715 shsurf->popup.x = x;
3716 shsurf->popup.y = y;
3717 shell_surface_set_parent(shsurf, parent);
3718
3719 return shsurf;
3720}
3721
3722static void
3723xdg_get_xdg_popup(struct wl_client *client,
3724 struct wl_resource *resource,
3725 uint32_t id,
3726 struct wl_resource *surface_resource,
3727 struct wl_resource *parent_resource,
3728 struct wl_resource *seat_resource,
3729 uint32_t serial,
3730 int32_t x, int32_t y, uint32_t flags)
3731{
3732 struct weston_surface *surface =
3733 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003734 struct shell_client *sc = wl_resource_get_user_data(resource);
3735 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003736 struct shell_surface *shsurf;
3737 struct weston_surface *parent;
3738 struct shell_seat *seat;
3739
3740 if (get_shell_surface(surface)) {
3741 wl_resource_post_error(surface_resource,
3742 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003743 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003744 return;
3745 }
3746
3747 if (!parent_resource) {
3748 wl_resource_post_error(surface_resource,
3749 WL_DISPLAY_ERROR_INVALID_OBJECT,
3750 "xdg_shell::get_xdg_popup requires a parent shell surface");
Jasper St. Pierre666bc922014-08-07 16:43:13 -04003751 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003752 }
3753
3754 parent = wl_resource_get_user_data(parent_resource);
3755 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3756
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003757 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003758 parent, seat, serial, x, y);
3759 if (!shsurf) {
3760 wl_resource_post_error(surface_resource,
3761 WL_DISPLAY_ERROR_INVALID_OBJECT,
3762 "surface->configure already set");
3763 return;
3764 }
3765
3766 shsurf->resource =
3767 wl_resource_create(client,
3768 &xdg_popup_interface, 1, id);
3769 wl_resource_set_implementation(shsurf->resource,
3770 &xdg_popup_implementation,
3771 shsurf, shell_destroy_shell_surface);
3772}
3773
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003774static void
3775xdg_pong(struct wl_client *client,
3776 struct wl_resource *resource, uint32_t serial)
3777{
3778 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003779
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003780 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003781}
3782
Rafael Antognollie2a34552013-12-03 15:35:45 -02003783static bool
3784shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3785{
3786 return wl_resource_instance_of(shsurf->resource,
3787 &xdg_popup_interface,
3788 &xdg_popup_implementation);
3789}
3790
3791static const struct xdg_shell_interface xdg_implementation = {
3792 xdg_use_unstable_version,
3793 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003794 xdg_get_xdg_popup,
3795 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003796};
3797
3798static int
3799xdg_shell_unversioned_dispatch(const void *implementation,
3800 void *_target, uint32_t opcode,
3801 const struct wl_message *message,
3802 union wl_argument *args)
3803{
3804 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003805 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003806
3807 if (opcode != 0) {
3808 wl_resource_post_error(resource,
3809 WL_DISPLAY_ERROR_INVALID_OBJECT,
3810 "must call use_unstable_version first");
3811 return 0;
3812 }
3813
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003814#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003815
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003816 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3817 "shell implementation doesn't match protocol version");
3818
Rafael Antognollie2a34552013-12-03 15:35:45 -02003819 if (args[0].i != XDG_SERVER_VERSION) {
3820 wl_resource_post_error(resource,
3821 WL_DISPLAY_ERROR_INVALID_OBJECT,
3822 "incompatible version, server is %d "
3823 "client wants %d",
3824 XDG_SERVER_VERSION, args[0].i);
3825 return 0;
3826 }
3827
3828 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003829 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003830
3831 return 1;
3832}
3833
3834/* end of xdg-shell implementation */
3835/***********************************/
3836
Kristian Høgsberg07937562011-04-12 17:25:42 -04003837static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003838shell_fade(struct desktop_shell *shell, enum fade_type type);
3839
3840static int
3841screensaver_timeout(void *data)
3842{
3843 struct desktop_shell *shell = data;
3844
3845 shell_fade(shell, FADE_OUT);
3846
3847 return 1;
3848}
3849
3850static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003851handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003852{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003853 struct desktop_shell *shell =
3854 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003855
Pekka Paalanen18027e52011-12-02 16:31:49 +02003856 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003857
3858 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003859 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003860}
3861
3862static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003863launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003864{
3865 if (shell->screensaver.binding)
3866 return;
3867
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003868 if (!shell->screensaver.path) {
3869 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003870 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003871 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003872
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003873 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003874 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003875 return;
3876 }
3877
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003878 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003879 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003880 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003881 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003882}
3883
3884static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003885terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003886{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003887 if (shell->screensaver.process.pid == 0)
3888 return;
3889
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003890 /* Disarm the screensaver timer, otherwise it may fire when the
3891 * compositor is not in the idle state. In that case, the screen will
3892 * be locked, but the wake_signal won't fire on user input, making the
3893 * system unresponsive. */
3894 wl_event_source_timer_update(shell->screensaver.timer, 0);
3895
Pekka Paalanen18027e52011-12-02 16:31:49 +02003896 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003897}
3898
3899static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003900configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003901{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003902 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003903
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003904 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003905 if (v->output == ev->output && v != ev) {
3906 weston_view_unmap(v);
3907 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003908 }
3909 }
3910
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003911 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003912
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003913 if (wl_list_empty(&ev->layer_link.link)) {
3914 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003915 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003916 }
3917}
3918
3919static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003920background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003921{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003922 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003923 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003924
Jason Ekstranda7af7042013-10-12 22:38:11 -05003925 view = container_of(es->views.next, struct weston_view, surface_link);
3926
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003927 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003928}
3929
3930static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003931desktop_shell_set_background(struct wl_client *client,
3932 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003933 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003934 struct wl_resource *surface_resource)
3935{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003936 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003937 struct weston_surface *surface =
3938 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003939 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003940
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003941 if (surface->configure) {
3942 wl_resource_post_error(surface_resource,
3943 WL_DISPLAY_ERROR_INVALID_OBJECT,
3944 "surface role already assigned");
3945 return;
3946 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003947
Jason Ekstranda7af7042013-10-12 22:38:11 -05003948 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3949 weston_view_destroy(view);
3950 view = weston_view_create(surface);
3951
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003952 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003953 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003954 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003955 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003956 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003957 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003958 surface->output->width,
3959 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003960}
3961
3962static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003963panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003964{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003965 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003966 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003967
Jason Ekstranda7af7042013-10-12 22:38:11 -05003968 view = container_of(es->views.next, struct weston_view, surface_link);
3969
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003970 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003971}
3972
3973static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003974desktop_shell_set_panel(struct wl_client *client,
3975 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003976 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003977 struct wl_resource *surface_resource)
3978{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003979 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003980 struct weston_surface *surface =
3981 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003982 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003983
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003984 if (surface->configure) {
3985 wl_resource_post_error(surface_resource,
3986 WL_DISPLAY_ERROR_INVALID_OBJECT,
3987 "surface role already assigned");
3988 return;
3989 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003990
Jason Ekstranda7af7042013-10-12 22:38:11 -05003991 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3992 weston_view_destroy(view);
3993 view = weston_view_create(surface);
3994
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003995 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003996 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003997 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003998 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003999 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04004000 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004001 surface->output->width,
4002 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004003}
4004
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004005static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004006lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004007{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004008 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004009 struct weston_view *view;
4010
4011 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004012
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004013 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004014 return;
4015
Jason Ekstranda7af7042013-10-12 22:38:11 -05004016 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004017
4018 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004019 weston_layer_entry_insert(&shell->lock_layer.view_list,
4020 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004021 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004022 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004023 }
4024}
4025
4026static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004027handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004028{
Tiago Vignattibe143262012-04-16 17:31:41 +03004029 struct desktop_shell *shell =
4030 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004031
Martin Minarik6d118362012-06-07 18:01:59 +02004032 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004033 shell->lock_surface = NULL;
4034}
4035
4036static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004037desktop_shell_set_lock_surface(struct wl_client *client,
4038 struct wl_resource *resource,
4039 struct wl_resource *surface_resource)
4040{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004041 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004042 struct weston_surface *surface =
4043 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004044
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004045 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004046
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004047 if (!shell->locked)
4048 return;
4049
Pekka Paalanen98262232011-12-01 10:42:22 +02004050 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004051
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004052 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004053 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004054 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004055
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004056 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004057 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004058 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004059}
4060
4061static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004062resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004063{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004064 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004065
Pekka Paalanen77346a62011-11-30 16:26:35 +02004066 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004067
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004068 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004069 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004070 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004071 &shell->input_panel_layer.link);
4072 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004073 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004074 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004075 wl_list_insert(&shell->compositor->cursor_layer.link,
4076 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004077 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004078 wl_list_insert(&shell->fullscreen_layer.link,
4079 &shell->panel_layer.link);
4080 wl_list_insert(&shell->panel_layer.link,
4081 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004082
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004083 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004084
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004085 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004086 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004087 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004088}
4089
4090static void
4091desktop_shell_unlock(struct wl_client *client,
4092 struct wl_resource *resource)
4093{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004094 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004095
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004096 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004097
4098 if (shell->locked)
4099 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004100}
4101
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004102static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004103desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004104 struct wl_resource *resource,
4105 struct wl_resource *surface_resource)
4106{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004107 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004108
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004109 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004110 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004111}
4112
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004113static void
4114desktop_shell_desktop_ready(struct wl_client *client,
4115 struct wl_resource *resource)
4116{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004117 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004118
4119 shell_fade_startup(shell);
4120}
4121
Kristian Høgsberg75840622011-09-06 13:48:16 -04004122static const struct desktop_shell_interface desktop_shell_implementation = {
4123 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004124 desktop_shell_set_panel,
4125 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004126 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004127 desktop_shell_set_grab_surface,
4128 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04004129};
4130
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004131static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004132get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004133{
4134 struct shell_surface *shsurf;
4135
4136 shsurf = get_shell_surface(surface);
4137 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004138 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004139 return shsurf->type;
4140}
4141
Kristian Høgsberg75840622011-09-06 13:48:16 -04004142static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004143move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004144{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004145 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004146 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004147 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004148
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004149 if (seat->pointer->focus == NULL)
4150 return;
4151
4152 focus = seat->pointer->focus->surface;
4153
Pekka Paalanen01388e22013-04-25 13:57:44 +03004154 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004155 if (surface == NULL)
4156 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004157
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004158 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004159 if (shsurf == NULL || shsurf->state.fullscreen ||
4160 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004161 return;
4162
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004163 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004164}
4165
4166static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004167maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4168{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004169 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004170 struct weston_surface *surface;
4171 struct shell_surface *shsurf;
4172
4173 surface = weston_surface_get_main_surface(focus);
4174 if (surface == NULL)
4175 return;
4176
4177 shsurf = get_shell_surface(surface);
4178 if (shsurf == NULL)
4179 return;
4180
4181 if (!shell_surface_is_xdg_surface(shsurf))
4182 return;
4183
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004184 shsurf->state_requested = true;
4185 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004186 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004187}
4188
4189static void
4190fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4191{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004192 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004193 struct weston_surface *surface;
4194 struct shell_surface *shsurf;
4195
4196 surface = weston_surface_get_main_surface(focus);
4197 if (surface == NULL)
4198 return;
4199
4200 shsurf = get_shell_surface(surface);
4201 if (shsurf == NULL)
4202 return;
4203
4204 if (!shell_surface_is_xdg_surface(shsurf))
4205 return;
4206
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004207 shsurf->state_requested = true;
4208 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004209 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004210 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004211}
4212
4213static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004214touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4215{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004216 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004217 struct weston_surface *surface;
4218 struct shell_surface *shsurf;
4219
4220 surface = weston_surface_get_main_surface(focus);
4221 if (surface == NULL)
4222 return;
4223
4224 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004225 if (shsurf == NULL || shsurf->state.fullscreen ||
4226 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004227 return;
4228
4229 surface_touch_move(shsurf, (struct weston_seat *) seat);
4230}
4231
4232static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004233resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004234{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004235 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004236 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004237 uint32_t edges = 0;
4238 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004239 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004240
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004241 if (seat->pointer->focus == NULL)
4242 return;
4243
4244 focus = seat->pointer->focus->surface;
4245
Pekka Paalanen01388e22013-04-25 13:57:44 +03004246 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004247 if (surface == NULL)
4248 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004249
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004250 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004251 if (shsurf == NULL || shsurf->state.fullscreen ||
4252 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004253 return;
4254
Jason Ekstranda7af7042013-10-12 22:38:11 -05004255 weston_view_from_global(shsurf->view,
4256 wl_fixed_to_int(seat->pointer->grab_x),
4257 wl_fixed_to_int(seat->pointer->grab_y),
4258 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004259
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004260 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004261 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004262 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004263 edges |= 0;
4264 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004265 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004266
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004267 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004268 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004269 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004270 edges |= 0;
4271 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004272 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004273
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004274 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004275}
4276
4277static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004278surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004279 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004280{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004281 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004282 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004283 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004284 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004285
Pekka Paalanen01388e22013-04-25 13:57:44 +03004286 /* XXX: broken for windows containing sub-surfaces */
4287 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004288 if (surface == NULL)
4289 return;
4290
4291 shsurf = get_shell_surface(surface);
4292 if (!shsurf)
4293 return;
4294
Jason Ekstranda7af7042013-10-12 22:38:11 -05004295 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004296
Jason Ekstranda7af7042013-10-12 22:38:11 -05004297 if (shsurf->view->alpha > 1.0)
4298 shsurf->view->alpha = 1.0;
4299 if (shsurf->view->alpha < step)
4300 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004301
Jason Ekstranda7af7042013-10-12 22:38:11 -05004302 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004303 weston_surface_damage(surface);
4304}
4305
4306static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004307do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004308 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004309{
Daniel Stone37816df2012-05-16 18:45:18 +01004310 struct weston_seat *ws = (struct weston_seat *) seat;
4311 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004312 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004313 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004314
4315 wl_list_for_each(output, &compositor->output_list, link) {
4316 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004317 wl_fixed_to_double(seat->pointer->x),
4318 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004319 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004320 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004321 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004322 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004323 increment = -output->zoom.increment;
4324 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004325 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004326 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004327 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004328 else
4329 increment = 0;
4330
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004331 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004332
Scott Moreaue6603982012-06-11 13:07:51 -06004333 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004334 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004335 else if (output->zoom.level > output->zoom.max_level)
4336 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004337 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004338 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004339 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004340
Scott Moreaue6603982012-06-11 13:07:51 -06004341 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004342
Jason Ekstranda7af7042013-10-12 22:38:11 -05004343 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004344 }
4345 }
4346}
4347
Scott Moreauccbf29d2012-02-22 14:21:41 -07004348static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004349zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004350 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004351{
4352 do_zoom(seat, time, 0, axis, value);
4353}
4354
4355static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004356zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004357 void *data)
4358{
4359 do_zoom(seat, time, key, 0, 0);
4360}
4361
4362static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004363terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004364 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004365{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004366 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004367
Daniel Stone325fc2d2012-05-30 16:31:58 +01004368 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004369}
4370
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004371static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004372rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4373 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004374{
4375 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004376 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004377 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004378 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004379 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004380
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004381 weston_pointer_move(pointer, x, y);
4382
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004383 if (!shsurf)
4384 return;
4385
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004386 cx = 0.5f * shsurf->surface->width;
4387 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004388
Daniel Stone37816df2012-05-16 18:45:18 +01004389 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4390 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004391 r = sqrtf(dx * dx + dy * dy);
4392
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004393 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004394 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004395
4396 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004397 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004398 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004399
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004400 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004401 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004402
4403 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004404 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004405 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004406 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004407 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004408
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004409 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004410 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004411 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004412 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004413 wl_list_init(&shsurf->rotation.transform.link);
4414 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004415 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004416 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004417
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004418 /* We need to adjust the position of the surface
4419 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004420 cposx = shsurf->view->geometry.x + cx;
4421 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004422 dposx = rotate->center.x - cposx;
4423 dposy = rotate->center.y - cposy;
4424 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004425 weston_view_set_position(shsurf->view,
4426 shsurf->view->geometry.x + dposx,
4427 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004428 }
4429
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004430 /* Repaint implies weston_surface_update_transform(), which
4431 * lazily applies the damage due to rotation update.
4432 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004433 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004434}
4435
4436static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004437rotate_grab_button(struct weston_pointer_grab *grab,
4438 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004439{
4440 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004441 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004442 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004443 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004444 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004445
Daniel Stone4dbadb12012-05-30 16:31:51 +01004446 if (pointer->button_count == 0 &&
4447 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004448 if (shsurf)
4449 weston_matrix_multiply(&shsurf->rotation.rotation,
4450 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004451 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004452 free(rotate);
4453 }
4454}
4455
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004456static void
4457rotate_grab_cancel(struct weston_pointer_grab *grab)
4458{
4459 struct rotate_grab *rotate =
4460 container_of(grab, struct rotate_grab, base.grab);
4461
4462 shell_grab_end(&rotate->base);
4463 free(rotate);
4464}
4465
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004466static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004467 noop_grab_focus,
4468 rotate_grab_motion,
4469 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004470 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004471};
4472
4473static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004474surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004475{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004476 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004477 float dx, dy;
4478 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004479
Pekka Paalanen460099f2012-01-20 16:48:25 +02004480 rotate = malloc(sizeof *rotate);
4481 if (!rotate)
4482 return;
4483
Jason Ekstranda7af7042013-10-12 22:38:11 -05004484 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004485 surface->surface->width * 0.5f,
4486 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004487 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004488
Daniel Stone37816df2012-05-16 18:45:18 +01004489 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4490 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004491 r = sqrtf(dx * dx + dy * dy);
4492 if (r > 20.0f) {
4493 struct weston_matrix inverse;
4494
4495 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004496 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004497 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004498
4499 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004500 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004501 } else {
4502 weston_matrix_init(&surface->rotation.rotation);
4503 weston_matrix_init(&rotate->rotation);
4504 }
4505
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004506 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4507 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004508}
4509
4510static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004511rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004512 void *data)
4513{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004514 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004515 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004516 struct shell_surface *surface;
4517
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004518 if (seat->pointer->focus == NULL)
4519 return;
4520
4521 focus = seat->pointer->focus->surface;
4522
Pekka Paalanen01388e22013-04-25 13:57:44 +03004523 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004524 if (base_surface == NULL)
4525 return;
4526
4527 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004528 if (surface == NULL || surface->state.fullscreen ||
4529 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004530 return;
4531
4532 surface_rotate(surface, seat);
4533}
4534
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004535/* Move all fullscreen layers down to the current workspace and hide their
4536 * black views. The surfaces' state is set to both fullscreen and lowered,
4537 * and this is reversed when such a surface is re-configured, see
4538 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4539 *
4540 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004541 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004542void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004543lower_fullscreen_layer(struct desktop_shell *shell)
4544{
4545 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004546 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004547
4548 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004549 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004550 &shell->fullscreen_layer.view_list.link,
4551 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004552 struct shell_surface *shsurf = get_shell_surface(view->surface);
4553
4554 if (!shsurf)
4555 continue;
4556
4557 /* We can have a non-fullscreen popup for a fullscreen surface
4558 * in the fullscreen layer. */
4559 if (shsurf->state.fullscreen) {
4560 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004561 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
4562 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07004563 weston_view_damage_below(shsurf->fullscreen.black_view);
4564
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004565 }
4566
4567 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004568 weston_layer_entry_remove(&view->layer_link);
4569 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004570 weston_view_damage_below(view);
4571 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004572
4573 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004574 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004575}
4576
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004577void
Tiago Vignattibe143262012-04-16 17:31:41 +03004578activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004579 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004580{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004581 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004582 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004583 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004584 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004585 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004586
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004587 lower_fullscreen_layer(shell);
4588
Pekka Paalanen01388e22013-04-25 13:57:44 +03004589 main_surface = weston_surface_get_main_surface(es);
4590
Daniel Stone37816df2012-05-16 18:45:18 +01004591 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004592
Jonas Ådahl8538b222012-08-29 22:13:03 +02004593 state = ensure_focus_state(shell, seat);
4594 if (state == NULL)
4595 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004596
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004597 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004598 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004599
Rafael Antognolli03b16592013-12-03 15:35:42 -02004600 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004601 assert(shsurf);
4602
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004603 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004604 shell_configure_fullscreen(shsurf);
4605 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004606 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004607
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004608 /* Update the surface’s layer. This brings it to the top of the stacking
4609 * order as appropriate. */
4610 shell_surface_update_layer(shsurf);
4611
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004612 if (shell->focus_animation_type != ANIMATION_NONE) {
4613 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004614 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004615 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004616}
4617
Alex Wu21858432012-04-01 20:13:08 +08004618/* no-op func for checking black surface */
4619static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004620black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004621{
4622}
4623
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004624static bool
Alex Wu21858432012-04-01 20:13:08 +08004625is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4626{
4627 if (es->configure == black_surface_configure) {
4628 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004629 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004630 return true;
4631 }
4632 return false;
4633}
4634
Kristian Høgsberg75840622011-09-06 13:48:16 -04004635static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004636activate_binding(struct weston_seat *seat,
4637 struct desktop_shell *shell,
4638 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004639{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004640 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004641
Alex Wu9c35e6b2012-03-05 14:13:13 +08004642 if (!focus)
4643 return;
4644
Pekka Paalanen01388e22013-04-25 13:57:44 +03004645 if (is_black_surface(focus, &main_surface))
4646 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004647
Pekka Paalanen01388e22013-04-25 13:57:44 +03004648 main_surface = weston_surface_get_main_surface(focus);
4649 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004650 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004651
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004652 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004653}
4654
4655static void
4656click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4657 void *data)
4658{
4659 if (seat->pointer->grab != &seat->pointer->default_grab)
4660 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004661 if (seat->pointer->focus == NULL)
4662 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004663
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004664 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004665}
4666
4667static void
4668touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4669{
4670 if (seat->touch->grab != &seat->touch->default_grab)
4671 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004672 if (seat->touch->focus == NULL)
4673 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004674
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004675 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004676}
4677
4678static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004679unfocus_all_seats(struct desktop_shell *shell)
4680{
4681 struct weston_seat *seat, *next;
4682
4683 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4684 if (seat->keyboard == NULL)
4685 continue;
4686
4687 weston_keyboard_set_focus(seat->keyboard, NULL);
4688 }
4689}
4690
4691static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004692lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004693{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004694 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004695
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004696 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004697 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004698 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004699 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004700
4701 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004702
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004703 /* Hide all surfaces by removing the fullscreen, panel and
4704 * toplevel layers. This way nothing else can show or receive
4705 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004706
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004707 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004708 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004709 if (shell->showing_input_panels)
4710 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004711 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004712 wl_list_insert(&shell->compositor->cursor_layer.link,
4713 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004714
Pekka Paalanen77346a62011-11-30 16:26:35 +02004715 launch_screensaver(shell);
4716
Neil Robertsb4a91702014-04-09 16:33:32 +01004717 /* Remove the keyboard focus on all seats. This will be
4718 * restored to the workspace's saved state via
4719 * restore_focus_state when the compositor is unlocked */
4720 unfocus_all_seats(shell);
4721
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004722 /* TODO: disable bindings that should not work while locked. */
4723
4724 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004725}
4726
4727static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004728unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004729{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004730 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004731 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004732 return;
4733 }
4734
4735 /* If desktop-shell client has gone away, unlock immediately. */
4736 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004737 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004738 return;
4739 }
4740
4741 if (shell->prepare_event_sent)
4742 return;
4743
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004744 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004745 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004746}
4747
4748static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004749shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004750{
4751 struct desktop_shell *shell = data;
4752
4753 shell->fade.animation = NULL;
4754
4755 switch (shell->fade.type) {
4756 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004757 weston_surface_destroy(shell->fade.view->surface);
4758 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004759 break;
4760 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004761 lock(shell);
4762 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004763 default:
4764 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004765 }
4766}
4767
Jason Ekstranda7af7042013-10-12 22:38:11 -05004768static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004769shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004770{
4771 struct weston_compositor *compositor = shell->compositor;
4772 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004773 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004774
4775 surface = weston_surface_create(compositor);
4776 if (!surface)
4777 return NULL;
4778
Jason Ekstranda7af7042013-10-12 22:38:11 -05004779 view = weston_view_create(surface);
4780 if (!view) {
4781 weston_surface_destroy(surface);
4782 return NULL;
4783 }
4784
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004785 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004786 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004787 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004788 weston_layer_entry_insert(&compositor->fade_layer.view_list,
4789 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004790 pixman_region32_init(&surface->input);
4791
Jason Ekstranda7af7042013-10-12 22:38:11 -05004792 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004793}
4794
4795static void
4796shell_fade(struct desktop_shell *shell, enum fade_type type)
4797{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004798 float tint;
4799
4800 switch (type) {
4801 case FADE_IN:
4802 tint = 0.0;
4803 break;
4804 case FADE_OUT:
4805 tint = 1.0;
4806 break;
4807 default:
4808 weston_log("shell: invalid fade type\n");
4809 return;
4810 }
4811
4812 shell->fade.type = type;
4813
Jason Ekstranda7af7042013-10-12 22:38:11 -05004814 if (shell->fade.view == NULL) {
4815 shell->fade.view = shell_fade_create_surface(shell);
4816 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004817 return;
4818
Jason Ekstranda7af7042013-10-12 22:38:11 -05004819 shell->fade.view->alpha = 1.0 - tint;
4820 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004821 }
4822
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004823 if (shell->fade.view->output == NULL) {
4824 /* If the black view gets a NULL output, we lost the
4825 * last output and we'll just cancel the fade. This
4826 * happens when you close the last window under the
4827 * X11 or Wayland backends. */
4828 shell->locked = false;
4829 weston_surface_destroy(shell->fade.view->surface);
4830 shell->fade.view = NULL;
4831 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004832 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004833 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004834 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004835 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004836 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004837 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004838 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004839}
4840
4841static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004842do_shell_fade_startup(void *data)
4843{
4844 struct desktop_shell *shell = data;
4845
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004846 if (shell->startup_animation_type == ANIMATION_FADE)
4847 shell_fade(shell, FADE_IN);
4848 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004849 weston_surface_destroy(shell->fade.view->surface);
4850 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004851 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004852}
4853
4854static void
4855shell_fade_startup(struct desktop_shell *shell)
4856{
4857 struct wl_event_loop *loop;
4858
4859 if (!shell->fade.startup_timer)
4860 return;
4861
4862 wl_event_source_remove(shell->fade.startup_timer);
4863 shell->fade.startup_timer = NULL;
4864
4865 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4866 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4867}
4868
4869static int
4870fade_startup_timeout(void *data)
4871{
4872 struct desktop_shell *shell = data;
4873
4874 shell_fade_startup(shell);
4875 return 0;
4876}
4877
4878static void
4879shell_fade_init(struct desktop_shell *shell)
4880{
4881 /* Make compositor output all black, and wait for the desktop-shell
4882 * client to signal it is ready, then fade in. The timer triggers a
4883 * fade-in, in case the desktop-shell client takes too long.
4884 */
4885
4886 struct wl_event_loop *loop;
4887
Jason Ekstranda7af7042013-10-12 22:38:11 -05004888 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004889 weston_log("%s: warning: fade surface already exists\n",
4890 __func__);
4891 return;
4892 }
4893
Jason Ekstranda7af7042013-10-12 22:38:11 -05004894 shell->fade.view = shell_fade_create_surface(shell);
4895 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004896 return;
4897
Jason Ekstranda7af7042013-10-12 22:38:11 -05004898 weston_view_update_transform(shell->fade.view);
4899 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004900
4901 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4902 shell->fade.startup_timer =
4903 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4904 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4905}
4906
4907static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004908idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004909{
4910 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004911 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004912 struct weston_seat *seat;
4913
4914 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4915 if (seat->pointer)
4916 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004917
4918 shell_fade(shell, FADE_OUT);
4919 /* lock() is called from shell_fade_done() */
4920}
4921
4922static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004923wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004924{
4925 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004926 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004927
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004928 unlock(shell);
4929}
4930
4931static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004932center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004933{
Giulio Camuffob8366642013-04-25 13:57:46 +03004934 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004935 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004936
Jason Ekstranda7af7042013-10-12 22:38:11 -05004937 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004938
4939 x = output->x + (output->width - width) / 2 - surf_x / 2;
4940 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004941
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004942 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004943}
4944
4945static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004946weston_view_set_initial_position(struct weston_view *view,
4947 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004948{
4949 struct weston_compositor *compositor = shell->compositor;
4950 int ix = 0, iy = 0;
4951 int range_x, range_y;
4952 int dx, dy, x, y, panel_height;
4953 struct weston_output *output, *target_output = NULL;
4954 struct weston_seat *seat;
4955
4956 /* As a heuristic place the new window on the same output as the
4957 * pointer. Falling back to the output containing 0, 0.
4958 *
4959 * TODO: Do something clever for touch too?
4960 */
4961 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004962 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004963 ix = wl_fixed_to_int(seat->pointer->x);
4964 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004965 break;
4966 }
4967 }
4968
4969 wl_list_for_each(output, &compositor->output_list, link) {
4970 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4971 target_output = output;
4972 break;
4973 }
4974 }
4975
4976 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004977 weston_view_set_position(view, 10 + random() % 400,
4978 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004979 return;
4980 }
4981
4982 /* Valid range within output where the surface will still be onscreen.
4983 * If this is negative it means that the surface is bigger than
4984 * output.
4985 */
4986 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004987 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004988 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004989 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004990
Rob Bradford4cb88c72012-08-13 15:18:44 +01004991 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004992 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004993 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004994 dx = 0;
4995
4996 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004997 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004998 else
4999 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005000
5001 x = target_output->x + dx;
5002 y = target_output->y + dy;
5003
Jason Ekstranda7af7042013-10-12 22:38:11 -05005004 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01005005}
5006
5007static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05005008map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005009 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005010{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005011 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005012 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08005013 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03005014 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005015
Pekka Paalanen77346a62011-11-30 16:26:35 +02005016 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005017 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005018 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005019 if (shsurf->state.fullscreen) {
5020 center_on_output(shsurf->view, shsurf->fullscreen_output);
5021 shell_map_fullscreen(shsurf);
5022 } else if (shsurf->state.maximized) {
5023 /* use surface configure to set the geometry */
5024 panel_height = get_output_panel_height(shell, shsurf->output);
5025 surface_subsurfaces_boundingbox(shsurf->surface,
5026 &surf_x, &surf_y, NULL, NULL);
5027 weston_view_set_position(shsurf->view,
5028 shsurf->output->x - surf_x,
5029 shsurf->output->y +
5030 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02005031 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005032 weston_view_set_initial_position(shsurf->view, shell);
5033 }
Juan Zhao96879df2012-02-07 08:45:41 +08005034 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005035 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04005036 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00005037 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005038 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005039 weston_view_set_position(shsurf->view,
5040 shsurf->view->geometry.x + sx,
5041 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005042 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005043 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005044 default:
5045 ;
5046 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005047
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005048 /* Surface stacking order, see also activate(). */
5049 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005050
Jason Ekstranda7af7042013-10-12 22:38:11 -05005051 if (shsurf->type != SHELL_SURFACE_NONE) {
5052 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005053 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005054 shsurf->surface->output = shsurf->output;
5055 shsurf->view->output = shsurf->output;
5056 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005057 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005058
Jason Ekstranda7af7042013-10-12 22:38:11 -05005059 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005060 /* XXX: xwayland's using the same fields for transient type */
5061 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005062 if (shsurf->transient.flags ==
5063 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5064 break;
5065 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005066 if (shsurf->state.relative &&
5067 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5068 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005069 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005070 break;
5071 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005072 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005073 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005074 case SHELL_SURFACE_POPUP:
5075 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005076 default:
5077 break;
5078 }
5079
Rafael Antognolli03b16592013-12-03 15:35:42 -02005080 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5081 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005082 {
5083 switch (shell->win_animation_type) {
5084 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005085 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005086 break;
5087 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005088 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005089 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005090 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005091 default:
5092 break;
5093 }
5094 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005095}
5096
5097static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005098configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005099 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005100{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005101 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005102 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005103 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005104
Pekka Paalanen77346a62011-11-30 16:26:35 +02005105 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005106
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005107 assert(shsurf);
5108
Rafael Antognolli03b16592013-12-03 15:35:42 -02005109 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005110 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005111 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08005112 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03005113 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
5114 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005115 mx = shsurf->output->x - surf_x;
5116 my = shsurf->output->y +
5117 get_output_panel_height(shell,shsurf->output) - surf_y;
5118 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005119 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005120 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005121 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005122
Alex Wu4539b082012-03-01 12:57:46 +08005123 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005124 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005125 wl_list_for_each(view, &surface->views, surface_link)
5126 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005127
Rafael Antognolli03b16592013-12-03 15:35:42 -02005128 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005129 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005130 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005131}
5132
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005133static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005134shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005135{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005136 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005137 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005138 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005139
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005140 assert(shsurf);
5141
5142 shell = shsurf->shell;
5143
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005144 if (!weston_surface_is_mapped(es) &&
5145 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005146 remove_popup_grab(shsurf);
5147 }
5148
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005149 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005150 return;
5151
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005152 if (shsurf->has_next_geometry) {
5153 shsurf->geometry = shsurf->next_geometry;
5154 shsurf->has_next_geometry = false;
5155 shsurf->has_set_geometry = true;
5156 } else if (!shsurf->has_set_geometry) {
5157 surface_subsurfaces_boundingbox(shsurf->surface,
5158 &shsurf->geometry.x,
5159 &shsurf->geometry.y,
5160 &shsurf->geometry.width,
5161 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005162 }
5163
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005164 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005165 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005166 type_changed = 1;
5167 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005168
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005169 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005170 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005171 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005172 shsurf->last_width != es->width ||
5173 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005174 float from_x, from_y;
5175 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005176
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005177 if (shsurf->resize_edges) {
5178 sx = 0;
5179 sy = 0;
5180 }
5181
5182 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5183 sx = shsurf->last_width - es->width;
5184 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5185 sy = shsurf->last_height - es->height;
5186
5187 shsurf->last_width = es->width;
5188 shsurf->last_height = es->height;
5189
Jason Ekstranda7af7042013-10-12 22:38:11 -05005190 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5191 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005192 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005193 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005194 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005195 }
5196}
5197
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005198static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005199
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005200static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005201desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005202{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005203 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005204 struct desktop_shell *shell =
5205 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005206
5207 shell->child.process.pid = 0;
5208 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005209
5210 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5211 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005212 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005213 shell->child.deathstamp = time;
5214 shell->child.deathcount = 0;
5215 }
5216
5217 shell->child.deathcount++;
5218 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005219 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005220 return;
5221 }
5222
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005223 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005224 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005225 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005226}
5227
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005228static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005229desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5230{
5231 struct desktop_shell *shell;
5232
5233 shell = container_of(listener, struct desktop_shell,
5234 child.client_destroy_listener);
5235
5236 shell->child.client = NULL;
5237}
5238
5239static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005240launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005241{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005242 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005243
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005244 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005245 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005246 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005247 desktop_shell_sigchld);
5248
5249 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005250 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005251
5252 shell->child.client_destroy_listener.notify =
5253 desktop_shell_client_destroy;
5254 wl_client_add_destroy_listener(shell->child.client,
5255 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005256}
5257
5258static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005259handle_shell_client_destroy(struct wl_listener *listener, void *data)
5260{
5261 struct shell_client *sc =
5262 container_of(listener, struct shell_client, destroy_listener);
5263
5264 if (sc->ping_timer)
5265 wl_event_source_remove(sc->ping_timer);
5266 free(sc);
5267}
5268
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005269static struct shell_client *
5270shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5271 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005272{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005273 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005274
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005275 sc = zalloc(sizeof *sc);
5276 if (sc == NULL) {
5277 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005278 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005279 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005280
5281 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005282 if (sc->resource == NULL) {
5283 free(sc);
5284 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005285 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005286 }
5287
5288 sc->client = client;
5289 sc->shell = shell;
5290 sc->destroy_listener.notify = handle_shell_client_destroy;
5291 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5292
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005293 return sc;
5294}
5295
5296static void
5297bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5298{
5299 struct desktop_shell *shell = data;
5300 struct shell_client *sc;
5301
5302 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5303 if (sc)
5304 wl_resource_set_implementation(sc->resource,
5305 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005306 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005307}
5308
5309static void
5310bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5311{
5312 struct desktop_shell *shell = data;
5313 struct shell_client *sc;
5314
5315 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5316 if (sc)
5317 wl_resource_set_dispatcher(sc->resource,
5318 xdg_shell_unversioned_dispatch,
5319 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005320}
5321
5322static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005323unbind_desktop_shell(struct wl_resource *resource)
5324{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005325 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005326
5327 if (shell->locked)
5328 resume_desktop(shell);
5329
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005330 shell->child.desktop_shell = NULL;
5331 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005332}
5333
5334static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005335bind_desktop_shell(struct wl_client *client,
5336 void *data, uint32_t version, uint32_t id)
5337{
Tiago Vignattibe143262012-04-16 17:31:41 +03005338 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005339 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005340
Jason Ekstranda85118c2013-06-27 20:17:02 -05005341 resource = wl_resource_create(client, &desktop_shell_interface,
5342 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005343
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005344 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005345 wl_resource_set_implementation(resource,
5346 &desktop_shell_implementation,
5347 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005348 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005349
5350 if (version < 2)
5351 shell_fade_startup(shell);
5352
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005353 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005354 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005355
5356 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5357 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04005358}
5359
Pekka Paalanen6e168112011-11-24 11:34:05 +02005360static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005361screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005362{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005363 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005364 struct weston_view *view;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005365 struct weston_layer_entry *prev;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005366
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005367 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005368 return;
5369
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005370 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005371 if (!shell->locked)
5372 return;
5373
Jason Ekstranda7af7042013-10-12 22:38:11 -05005374 view = container_of(surface->views.next, struct weston_view, surface_link);
5375 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005376
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005377 if (wl_list_empty(&view->layer_link.link)) {
5378 prev = container_of(shell->lock_layer.view_list.link.prev,
5379 struct weston_layer_entry, link);
5380 weston_layer_entry_insert(prev, &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005381 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005382 wl_event_source_timer_update(shell->screensaver.timer,
5383 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005384 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005385 }
5386}
5387
5388static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005389screensaver_set_surface(struct wl_client *client,
5390 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005391 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005392 struct wl_resource *output_resource)
5393{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005394 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005395 struct weston_surface *surface =
5396 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005397 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005398 struct weston_view *view, *next;
5399
5400 /* Make sure we only have one view */
5401 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5402 weston_view_destroy(view);
5403 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005404
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005405 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005406 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005407 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005408}
5409
5410static const struct screensaver_interface screensaver_implementation = {
5411 screensaver_set_surface
5412};
5413
5414static void
5415unbind_screensaver(struct wl_resource *resource)
5416{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005417 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005418
Pekka Paalanen77346a62011-11-30 16:26:35 +02005419 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005420}
5421
5422static void
5423bind_screensaver(struct wl_client *client,
5424 void *data, uint32_t version, uint32_t id)
5425{
Tiago Vignattibe143262012-04-16 17:31:41 +03005426 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005427 struct wl_resource *resource;
5428
Jason Ekstranda85118c2013-06-27 20:17:02 -05005429 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005430
Pekka Paalanen77346a62011-11-30 16:26:35 +02005431 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005432 wl_resource_set_implementation(resource,
5433 &screensaver_implementation,
5434 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005435 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005436 return;
5437 }
5438
5439 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5440 "interface object already bound");
Pekka Paalanen6e168112011-11-24 11:34:05 +02005441}
5442
Kristian Høgsberg07045392012-02-19 18:52:44 -05005443struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005444 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005445 struct weston_surface *current;
5446 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005447 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005448 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005449};
5450
5451static void
5452switcher_next(struct switcher *switcher)
5453{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005454 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005455 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005456 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005457 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005458
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005459 /* temporary re-display minimized surfaces */
5460 struct weston_view *tmp;
5461 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005462 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5463 weston_layer_entry_remove(&view->layer_link);
5464 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005465 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5466 *minimized = view;
5467 }
5468
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005469 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005470 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005471 if (shsurf &&
5472 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5473 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005474 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005475 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005476 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005477 next = view->surface;
5478 prev = view->surface;
5479 view->alpha = 0.25;
5480 weston_view_geometry_dirty(view);
5481 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005482 }
Alex Wu1659daa2012-04-01 20:13:09 +08005483
Jason Ekstranda7af7042013-10-12 22:38:11 -05005484 if (is_black_surface(view->surface, NULL)) {
5485 view->alpha = 0.25;
5486 weston_view_geometry_dirty(view);
5487 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005488 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005489 }
5490
5491 if (next == NULL)
5492 next = first;
5493
Alex Wu07b26062012-03-12 16:06:01 +08005494 if (next == NULL)
5495 return;
5496
Kristian Høgsberg07045392012-02-19 18:52:44 -05005497 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005498 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005499
5500 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005501 wl_list_for_each(view, &next->views, surface_link)
5502 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005503
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005504 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005505 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005506 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005507}
5508
5509static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005510switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005511{
5512 struct switcher *switcher =
5513 container_of(listener, struct switcher, listener);
5514
5515 switcher_next(switcher);
5516}
5517
5518static void
Daniel Stone351eb612012-05-31 15:27:47 -04005519switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005520{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005521 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005522 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005523 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005524
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005525 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005526 if (is_focus_view(view))
5527 continue;
5528
Jason Ekstranda7af7042013-10-12 22:38:11 -05005529 view->alpha = 1.0;
5530 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005531 }
5532
Alex Wu07b26062012-03-12 16:06:01 +08005533 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005534 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005535 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005536 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005537 weston_keyboard_end_grab(keyboard);
5538 if (keyboard->input_method_resource)
5539 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005540
5541 /* re-hide surfaces that were temporary shown during the switch */
5542 struct weston_view **minimized;
5543 wl_array_for_each(minimized, &switcher->minimized_array) {
5544 /* with the exception of the current selected */
5545 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005546 weston_layer_entry_remove(&(*minimized)->layer_link);
5547 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005548 weston_view_damage_below(*minimized);
5549 }
5550 }
5551 wl_array_release(&switcher->minimized_array);
5552
Kristian Høgsberg07045392012-02-19 18:52:44 -05005553 free(switcher);
5554}
5555
5556static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005557switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005558 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005559{
5560 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005561 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005562
Daniel Stonec9785ea2012-05-30 16:31:52 +01005563 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005564 switcher_next(switcher);
5565}
5566
5567static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005568switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005569 uint32_t mods_depressed, uint32_t mods_latched,
5570 uint32_t mods_locked, uint32_t group)
5571{
5572 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005573 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005574
Daniel Stone351eb612012-05-31 15:27:47 -04005575 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5576 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005577}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005578
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005579static void
5580switcher_cancel(struct weston_keyboard_grab *grab)
5581{
5582 struct switcher *switcher = container_of(grab, struct switcher, grab);
5583
5584 switcher_destroy(switcher);
5585}
5586
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005587static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005588 switcher_key,
5589 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005590 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005591};
5592
5593static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005594switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005595 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005596{
Tiago Vignattibe143262012-04-16 17:31:41 +03005597 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005598 struct switcher *switcher;
5599
5600 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005601 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005602 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005603 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005604 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005605 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005606
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005607 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005608 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005609 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005610 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5611 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005612 switcher_next(switcher);
5613}
5614
Pekka Paalanen3c647232011-12-22 13:43:43 +02005615static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005616backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005617 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005618{
5619 struct weston_compositor *compositor = data;
5620 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005621 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005622
5623 /* TODO: we're limiting to simple use cases, where we assume just
5624 * control on the primary display. We'd have to extend later if we
5625 * ever get support for setting backlights on random desktop LCD
5626 * panels though */
5627 output = get_default_output(compositor);
5628 if (!output)
5629 return;
5630
5631 if (!output->set_backlight)
5632 return;
5633
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005634 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5635 backlight_new = output->backlight_current - 25;
5636 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5637 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005638
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005639 if (backlight_new < 5)
5640 backlight_new = 5;
5641 if (backlight_new > 255)
5642 backlight_new = 255;
5643
5644 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005645 output->set_backlight(output, output->backlight_current);
5646}
5647
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005648struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005649 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005650 struct weston_seat *seat;
5651 uint32_t key[2];
5652 int key_released[2];
5653};
5654
5655static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005656debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005657 uint32_t key, uint32_t state)
5658{
5659 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005660 struct weston_compositor *ec = db->seat->compositor;
5661 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005662 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005663 uint32_t serial;
5664 int send = 0, terminate = 0;
5665 int check_binding = 1;
5666 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005667 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005668
5669 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5670 /* Do not run bindings on key releases */
5671 check_binding = 0;
5672
5673 for (i = 0; i < 2; i++)
5674 if (key == db->key[i])
5675 db->key_released[i] = 1;
5676
5677 if (db->key_released[0] && db->key_released[1]) {
5678 /* All key releases been swalled so end the grab */
5679 terminate = 1;
5680 } else if (key != db->key[0] && key != db->key[1]) {
5681 /* Should not swallow release of other keys */
5682 send = 1;
5683 }
5684 } else if (key == db->key[0] && !db->key_released[0]) {
5685 /* Do not check bindings for the first press of the binding
5686 * key. This allows it to be used as a debug shortcut.
5687 * We still need to swallow this event. */
5688 check_binding = 0;
5689 } else if (db->key[1]) {
5690 /* If we already ran a binding don't process another one since
5691 * we can't keep track of all the binding keys that were
5692 * pressed in order to swallow the release events. */
5693 send = 1;
5694 check_binding = 0;
5695 }
5696
5697 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005698 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5699 key, state)) {
5700 /* We ran a binding so swallow the press and keep the
5701 * grab to swallow the released too. */
5702 send = 0;
5703 terminate = 0;
5704 db->key[1] = key;
5705 } else {
5706 /* Terminate the grab since the key pressed is not a
5707 * debug binding key. */
5708 send = 1;
5709 terminate = 1;
5710 }
5711 }
5712
5713 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005714 serial = wl_display_next_serial(display);
5715 resource_list = &grab->keyboard->focus_resource_list;
5716 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005717 wl_keyboard_send_key(resource, serial, time, key, state);
5718 }
5719 }
5720
5721 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005722 weston_keyboard_end_grab(grab->keyboard);
5723 if (grab->keyboard->input_method_resource)
5724 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005725 free(db);
5726 }
5727}
5728
5729static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005730debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005731 uint32_t mods_depressed, uint32_t mods_latched,
5732 uint32_t mods_locked, uint32_t group)
5733{
5734 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005735 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005736
Neil Roberts96d790e2013-09-19 17:32:00 +01005737 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005738
Neil Roberts96d790e2013-09-19 17:32:00 +01005739 wl_resource_for_each(resource, resource_list) {
5740 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5741 mods_latched, mods_locked, group);
5742 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005743}
5744
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005745static void
5746debug_binding_cancel(struct weston_keyboard_grab *grab)
5747{
5748 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5749
5750 weston_keyboard_end_grab(grab->keyboard);
5751 free(db);
5752}
5753
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005754struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005755 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005756 debug_binding_modifiers,
5757 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005758};
5759
5760static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005761debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005762{
5763 struct debug_binding_grab *grab;
5764
5765 grab = calloc(1, sizeof *grab);
5766 if (!grab)
5767 return;
5768
5769 grab->seat = (struct weston_seat *) seat;
5770 grab->key[0] = key;
5771 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005772 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005773}
5774
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005775static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005776force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005777 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005778{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005779 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005780 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005781 struct desktop_shell *shell = data;
5782 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005783 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005784
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005785 focus_surface = seat->keyboard->focus;
5786 if (!focus_surface)
5787 return;
5788
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005789 wl_signal_emit(&compositor->kill_signal, focus_surface);
5790
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005791 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005792 wl_client_get_credentials(client, &pid, NULL, NULL);
5793
5794 /* Skip clients that we launched ourselves (the credentials of
5795 * the socketpair is ours) */
5796 if (pid == getpid())
5797 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005798
Daniel Stone325fc2d2012-05-30 16:31:58 +01005799 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005800}
5801
5802static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005803workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005804 uint32_t key, void *data)
5805{
5806 struct desktop_shell *shell = data;
5807 unsigned int new_index = shell->workspaces.current;
5808
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005809 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005810 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005811 if (new_index != 0)
5812 new_index--;
5813
5814 change_workspace(shell, new_index);
5815}
5816
5817static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005818workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005819 uint32_t key, void *data)
5820{
5821 struct desktop_shell *shell = data;
5822 unsigned int new_index = shell->workspaces.current;
5823
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005824 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005825 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005826 if (new_index < shell->workspaces.num - 1)
5827 new_index++;
5828
5829 change_workspace(shell, new_index);
5830}
5831
5832static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005833workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005834 uint32_t key, void *data)
5835{
5836 struct desktop_shell *shell = data;
5837 unsigned int new_index;
5838
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005839 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005840 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005841 new_index = key - KEY_F1;
5842 if (new_index >= shell->workspaces.num)
5843 new_index = shell->workspaces.num - 1;
5844
5845 change_workspace(shell, new_index);
5846}
5847
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005848static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005849workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005850 uint32_t key, void *data)
5851{
5852 struct desktop_shell *shell = data;
5853 unsigned int new_index = shell->workspaces.current;
5854
5855 if (shell->locked)
5856 return;
5857
5858 if (new_index != 0)
5859 new_index--;
5860
5861 take_surface_to_workspace_by_seat(shell, seat, new_index);
5862}
5863
5864static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005865workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005866 uint32_t key, void *data)
5867{
5868 struct desktop_shell *shell = data;
5869 unsigned int new_index = shell->workspaces.current;
5870
5871 if (shell->locked)
5872 return;
5873
5874 if (new_index < shell->workspaces.num - 1)
5875 new_index++;
5876
5877 take_surface_to_workspace_by_seat(shell, seat, new_index);
5878}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005879
5880static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005881shell_reposition_view_on_output_destroy(struct weston_view *view)
5882{
5883 struct weston_output *output, *first_output;
5884 struct weston_compositor *ec = view->surface->compositor;
5885 struct shell_surface *shsurf;
5886 float x, y;
5887 int visible;
5888
5889 x = view->geometry.x;
5890 y = view->geometry.y;
5891
5892 /* At this point the destroyed output is not in the list anymore.
5893 * If the view is still visible somewhere, we leave where it is,
5894 * otherwise, move it to the first output. */
5895 visible = 0;
5896 wl_list_for_each(output, &ec->output_list, link) {
5897 if (pixman_region32_contains_point(&output->region,
5898 x, y, NULL)) {
5899 visible = 1;
5900 break;
5901 }
5902 }
5903
5904 if (!visible) {
5905 first_output = container_of(ec->output_list.next,
5906 struct weston_output, link);
5907
5908 x = first_output->x + first_output->width / 4;
5909 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08005910
5911 weston_view_set_position(view, x, y);
5912 } else {
5913 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005914 }
5915
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005916
5917 shsurf = get_shell_surface(view->surface);
5918
5919 if (shsurf) {
5920 shsurf->saved_position_valid = false;
5921 shsurf->next_state.maximized = false;
5922 shsurf->next_state.fullscreen = false;
5923 shsurf->state_changed = true;
5924 }
5925}
5926
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005927void
5928shell_for_each_layer(struct desktop_shell *shell,
5929 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005930{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005931 struct workspace **ws;
5932
5933 func(shell, &shell->fullscreen_layer, data);
5934 func(shell, &shell->panel_layer, data);
5935 func(shell, &shell->background_layer, data);
5936 func(shell, &shell->lock_layer, data);
5937 func(shell, &shell->input_panel_layer, data);
5938
5939 wl_array_for_each(ws, &shell->workspaces.array)
5940 func(shell, &(*ws)->layer, data);
5941}
5942
5943static void
5944shell_output_destroy_move_layer(struct desktop_shell *shell,
5945 struct weston_layer *layer,
5946 void *data)
5947{
5948 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005949 struct weston_view *view;
5950
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005951 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005952 if (view->output != output)
5953 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005954
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005955 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005956 }
5957}
5958
5959static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005960handle_output_destroy(struct wl_listener *listener, void *data)
5961{
5962 struct shell_output *output_listener =
5963 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005964 struct weston_output *output = output_listener->output;
5965 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005966
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005967 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005968
Xiong Zhang6b481422013-10-23 13:58:32 +08005969 wl_list_remove(&output_listener->destroy_listener.link);
5970 wl_list_remove(&output_listener->link);
5971 free(output_listener);
5972}
5973
5974static void
5975create_shell_output(struct desktop_shell *shell,
5976 struct weston_output *output)
5977{
5978 struct shell_output *shell_output;
5979
5980 shell_output = zalloc(sizeof *shell_output);
5981 if (shell_output == NULL)
5982 return;
5983
5984 shell_output->output = output;
5985 shell_output->shell = shell;
5986 shell_output->destroy_listener.notify = handle_output_destroy;
5987 wl_signal_add(&output->destroy_signal,
5988 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005989 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005990}
5991
5992static void
5993handle_output_create(struct wl_listener *listener, void *data)
5994{
5995 struct desktop_shell *shell =
5996 container_of(listener, struct desktop_shell, output_create_listener);
5997 struct weston_output *output = (struct weston_output *)data;
5998
5999 create_shell_output(shell, output);
6000}
6001
6002static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006003handle_output_move_layer(struct desktop_shell *shell,
6004 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006005{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006006 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006007 struct weston_view *view;
6008 float x, y;
6009
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006010 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006011 if (view->output != output)
6012 continue;
6013
6014 x = view->geometry.x + output->move_x;
6015 y = view->geometry.y + output->move_y;
6016 weston_view_set_position(view, x, y);
6017 }
6018}
6019
6020static void
6021handle_output_move(struct wl_listener *listener, void *data)
6022{
6023 struct desktop_shell *shell;
6024
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006025 shell = container_of(listener, struct desktop_shell,
6026 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006027
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006028 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006029}
6030
6031static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006032setup_output_destroy_handler(struct weston_compositor *ec,
6033 struct desktop_shell *shell)
6034{
6035 struct weston_output *output;
6036
6037 wl_list_init(&shell->output_list);
6038 wl_list_for_each(output, &ec->output_list, link)
6039 create_shell_output(shell, output);
6040
6041 shell->output_create_listener.notify = handle_output_create;
6042 wl_signal_add(&ec->output_created_signal,
6043 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006044
6045 shell->output_move_listener.notify = handle_output_move;
6046 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006047}
6048
6049static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006050shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006051{
Tiago Vignattibe143262012-04-16 17:31:41 +03006052 struct desktop_shell *shell =
6053 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006054 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006055 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006056
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006057 /* Force state to unlocked so we don't try to fade */
6058 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006059 if (shell->child.client)
6060 wl_client_destroy(shell->child.client);
6061
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006062 wl_list_remove(&shell->idle_listener.link);
6063 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006064
6065 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006066
Xiong Zhang6b481422013-10-23 13:58:32 +08006067 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6068 wl_list_remove(&shell_output->destroy_listener.link);
6069 wl_list_remove(&shell_output->link);
6070 free(shell_output);
6071 }
6072
6073 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006074 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006075
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006076 wl_array_for_each(ws, &shell->workspaces.array)
6077 workspace_destroy(*ws);
6078 wl_array_release(&shell->workspaces.array);
6079
Pekka Paalanen3c647232011-12-22 13:43:43 +02006080 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006081 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006082 free(shell);
6083}
6084
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006085static void
6086shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6087{
6088 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006089 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006090
6091 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006092 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6093 MODIFIER_CTRL | MODIFIER_ALT,
6094 terminate_binding, ec);
6095 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6096 click_to_activate_binding,
6097 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006098 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6099 click_to_activate_binding,
6100 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006101 weston_compositor_add_touch_binding(ec, 0,
6102 touch_to_activate_binding,
6103 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006104 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6105 MODIFIER_SUPER | MODIFIER_ALT,
6106 surface_opacity_binding, NULL);
6107 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6108 MODIFIER_SUPER, zoom_axis_binding,
6109 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006110
6111 /* configurable bindings */
6112 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006113 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6114 zoom_key_binding, NULL);
6115 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6116 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006117 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6118 maximize_binding, NULL);
6119 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6120 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006121 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6122 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006123 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006124 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6125 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006126 weston_compositor_add_button_binding(ec, BTN_LEFT,
6127 mod | MODIFIER_SHIFT,
6128 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006129
6130 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6131 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6132 rotate_binding, NULL);
6133
Daniel Stone325fc2d2012-05-30 16:31:58 +01006134 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6135 shell);
6136 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6137 ec);
6138 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6139 backlight_binding, ec);
6140 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6141 ec);
6142 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6143 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006144 weston_compositor_add_key_binding(ec, KEY_K, mod,
6145 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006146 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6147 workspace_up_binding, shell);
6148 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6149 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006150 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6151 workspace_move_surface_up_binding,
6152 shell);
6153 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6154 workspace_move_surface_down_binding,
6155 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006156
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006157 if (shell->exposay_modifier)
6158 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6159 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006160
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006161 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6162 if (shell->workspaces.num > 1) {
6163 num_workspace_bindings = shell->workspaces.num;
6164 if (num_workspace_bindings > 6)
6165 num_workspace_bindings = 6;
6166 for (i = 0; i < num_workspace_bindings; i++)
6167 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6168 workspace_f_binding,
6169 shell);
6170 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006171
6172 /* Debug bindings */
6173 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6174 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006175}
6176
Jason Ekstrand024177c2014-04-21 19:42:58 -05006177static void
6178handle_seat_created(struct wl_listener *listener, void *data)
6179{
6180 struct weston_seat *seat = data;
6181
6182 create_shell_seat(seat);
6183}
6184
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006185WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006186module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006187 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006188{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006189 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006190 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006191 struct workspace **pws;
6192 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006193 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006194
Peter Huttererf3d62272013-08-08 11:57:05 +10006195 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006196 if (shell == NULL)
6197 return -1;
6198
Kristian Høgsberg75840622011-09-06 13:48:16 -04006199 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006200
6201 shell->destroy_listener.notify = shell_destroy;
6202 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006203 shell->idle_listener.notify = idle_handler;
6204 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6205 shell->wake_listener.notify = wake_handler;
6206 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006207
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006208 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006209 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006210 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006211 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006212 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006213 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006214 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006215 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006216 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006217 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006218 ec->shell_interface.set_window_geometry = set_window_geometry;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006219
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006220 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6221 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006222 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6223 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006224 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006225
6226 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006227 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006228
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006229 if (input_panel_setup(shell) < 0)
6230 return -1;
6231
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006232 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006233
Daniel Stonedf8133b2013-11-19 11:37:14 +01006234 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6235 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6236
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006237 for (i = 0; i < shell->workspaces.num; i++) {
6238 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6239 if (pws == NULL)
6240 return -1;
6241
6242 *pws = workspace_create();
6243 if (*pws == NULL)
6244 return -1;
6245 }
6246 activate_workspace(shell, 0);
6247
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006248 weston_layer_init(&shell->minimized_layer, NULL);
6249
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006250 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006251 wl_list_init(&shell->workspaces.animation.link);
6252 shell->workspaces.animation.frame = animate_workspace_change_frame;
6253
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006254 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006255 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006256 return -1;
6257
Rafael Antognollie2a34552013-12-03 15:35:45 -02006258 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6259 shell, bind_xdg_shell) == NULL)
6260 return -1;
6261
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006262 if (wl_global_create(ec->wl_display,
6263 &desktop_shell_interface, 2,
6264 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006265 return -1;
6266
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006267 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6268 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006269 return -1;
6270
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006271 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6272 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006273 return -1;
6274
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006275 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006276
Xiong Zhang6b481422013-10-23 13:58:32 +08006277 setup_output_destroy_handler(ec, shell);
6278
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006279 loop = wl_display_get_event_loop(ec->wl_display);
6280 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006281
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006282 shell->screensaver.timer =
6283 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6284
Jason Ekstrand024177c2014-04-21 19:42:58 -05006285 wl_list_for_each(seat, &ec->seat_list, link)
6286 handle_seat_created(NULL, seat);
6287 shell->seat_create_listener.notify = handle_seat_created;
6288 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006289
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006290 screenshooter_create(ec);
6291
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006292 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006293
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006294 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006295
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006296 return 0;
6297}