blob: 3c3649ce950fc2b1971aab4acefb864927f85768 [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);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700538 weston_config_section_get_string(section,
539 "startup-animation", &s, "fade");
540 shell->startup_animation_type = get_animation_type(s);
541 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700542 if (shell->startup_animation_type == ANIMATION_ZOOM)
543 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100544 weston_config_section_get_string(section, "focus-animation", &s, "none");
545 shell->focus_animation_type = get_animation_type(s);
546 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400547 weston_config_section_get_uint(section, "num-workspaces",
548 &shell->workspaces.num,
549 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200550}
551
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800552struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100553get_default_output(struct weston_compositor *compositor)
554{
555 return container_of(compositor->output_list.next,
556 struct weston_output, link);
557}
558
559
560/* no-op func for checking focus surface */
561static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600562focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100563{
564}
565
566static struct focus_surface *
567get_focus_surface(struct weston_surface *surface)
568{
569 if (surface->configure == focus_surface_configure)
570 return surface->configure_private;
571 else
572 return NULL;
573}
574
575static bool
576is_focus_surface (struct weston_surface *es)
577{
578 return (es->configure == focus_surface_configure);
579}
580
581static bool
582is_focus_view (struct weston_view *view)
583{
584 return is_focus_surface (view->surface);
585}
586
587static struct focus_surface *
588create_focus_surface(struct weston_compositor *ec,
589 struct weston_output *output)
590{
591 struct focus_surface *fsurf = NULL;
592 struct weston_surface *surface = NULL;
593
594 fsurf = malloc(sizeof *fsurf);
595 if (!fsurf)
596 return NULL;
597
598 fsurf->surface = weston_surface_create(ec);
599 surface = fsurf->surface;
600 if (surface == NULL) {
601 free(fsurf);
602 return NULL;
603 }
604
605 surface->configure = focus_surface_configure;
606 surface->output = output;
607 surface->configure_private = fsurf;
608
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800609 fsurf->view = weston_view_create(surface);
610 if (fsurf->view == NULL) {
611 weston_surface_destroy(surface);
612 free(fsurf);
613 return NULL;
614 }
Emilio Pozuelo Monfortda644262013-11-19 11:37:19 +0100615 fsurf->view->output = output;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100616
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600617 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600618 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100619 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
620 pixman_region32_fini(&surface->opaque);
621 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
622 output->width, output->height);
623 pixman_region32_fini(&surface->input);
624 pixman_region32_init(&surface->input);
625
626 wl_list_init(&fsurf->workspace_transform.link);
627
628 return fsurf;
629}
630
631static void
632focus_surface_destroy(struct focus_surface *fsurf)
633{
634 weston_surface_destroy(fsurf->surface);
635 free(fsurf);
636}
637
638static void
639focus_animation_done(struct weston_view_animation *animation, void *data)
640{
641 struct workspace *ws = data;
642
643 ws->focus_animation = NULL;
644}
645
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200646static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200647focus_state_destroy(struct focus_state *state)
648{
649 wl_list_remove(&state->seat_destroy_listener.link);
650 wl_list_remove(&state->surface_destroy_listener.link);
651 free(state);
652}
653
654static void
655focus_state_seat_destroy(struct wl_listener *listener, void *data)
656{
657 struct focus_state *state = container_of(listener,
658 struct focus_state,
659 seat_destroy_listener);
660
661 wl_list_remove(&state->link);
662 focus_state_destroy(state);
663}
664
665static void
666focus_state_surface_destroy(struct wl_listener *listener, void *data)
667{
668 struct focus_state *state = container_of(listener,
669 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400670 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400671 struct desktop_shell *shell;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500672 struct weston_surface *main_surface, *next;
673 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200674
Pekka Paalanen01388e22013-04-25 13:57:44 +0300675 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
676
Kristian Høgsberge3778222012-07-31 17:29:30 -0400677 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300678 wl_list_for_each(view,
679 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500680 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400681 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100682 if (is_focus_view(view))
683 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400684
Jason Ekstranda7af7042013-10-12 22:38:11 -0500685 next = view->surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400686 break;
687 }
688
Pekka Paalanen01388e22013-04-25 13:57:44 +0300689 /* if the focus was a sub-surface, activate its main surface */
690 if (main_surface != state->keyboard_focus)
691 next = main_surface;
692
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100693 shell = state->seat->compositor->shell_interface.shell;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400694 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100695 state->keyboard_focus = NULL;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100696 activate(shell, next, state->seat, true);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400697 } else {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100698 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
699 if (state->ws->focus_animation)
700 weston_view_animation_destroy(state->ws->focus_animation);
701
702 state->ws->focus_animation = weston_fade_run(
703 state->ws->fsurf_front->view,
704 state->ws->fsurf_front->view->alpha, 0.0, 300,
705 focus_animation_done, state->ws);
706 }
707
Kristian Høgsberge3778222012-07-31 17:29:30 -0400708 wl_list_remove(&state->link);
709 focus_state_destroy(state);
710 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200711}
712
713static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400714focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200715{
Jonas Ådahl04769742012-06-13 00:01:24 +0200716 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200717
718 state = malloc(sizeof *state);
719 if (state == NULL)
720 return NULL;
721
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100722 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400723 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200724 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400725 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200726
727 state->seat_destroy_listener.notify = focus_state_seat_destroy;
728 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400729 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200730 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400731 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200732
733 return state;
734}
735
Jonas Ådahl8538b222012-08-29 22:13:03 +0200736static struct focus_state *
737ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
738{
739 struct workspace *ws = get_current_workspace(shell);
740 struct focus_state *state;
741
742 wl_list_for_each(state, &ws->focus_list, link)
743 if (state->seat == seat)
744 break;
745
746 if (&state->link == &ws->focus_list)
747 state = focus_state_create(seat, ws);
748
749 return state;
750}
751
Jonas Ådahl04769742012-06-13 00:01:24 +0200752static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800753focus_state_set_focus(struct focus_state *state,
754 struct weston_surface *surface)
755{
756 if (state->keyboard_focus) {
757 wl_list_remove(&state->surface_destroy_listener.link);
758 wl_list_init(&state->surface_destroy_listener.link);
759 }
760
761 state->keyboard_focus = surface;
762 if (surface)
763 wl_signal_add(&surface->destroy_signal,
764 &state->surface_destroy_listener);
765}
766
767static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400768restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200769{
770 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400771 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100772 struct wl_list pending_seat_list;
773 struct weston_seat *seat, *next_seat;
774
775 /* Temporarily steal the list of seats so that we can keep
776 * track of the seats we've already processed */
777 wl_list_init(&pending_seat_list);
778 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
779 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200780
781 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Neil Roberts4237f502014-04-09 16:33:31 +0100782 wl_list_remove(&state->seat->link);
783 wl_list_insert(&shell->compositor->seat_list,
784 &state->seat->link);
785
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800786 if (state->seat->keyboard == NULL)
787 continue;
788
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400789 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200790
Kristian Høgsberge3148752013-05-06 23:19:49 -0400791 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200792 }
Neil Roberts4237f502014-04-09 16:33:31 +0100793
794 /* For any remaining seats that we don't have a focus state
795 * for we'll reset the keyboard focus to NULL */
796 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
797 wl_list_insert(&shell->compositor->seat_list, &seat->link);
798
Ander Conselvan de Oliveira6e56ab42014-05-07 11:57:28 +0300799 if (seat->keyboard == NULL)
Neil Roberts4237f502014-04-09 16:33:31 +0100800 continue;
801
802 weston_keyboard_set_focus(seat->keyboard, NULL);
803 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200804}
805
806static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200807replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
808 struct weston_seat *seat)
809{
810 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200811
812 wl_list_for_each(state, &ws->focus_list, link) {
813 if (state->seat == seat) {
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800814 focus_state_set_focus(state, seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200815 return;
816 }
817 }
818}
819
820static void
821drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
822 struct weston_surface *surface)
823{
824 struct focus_state *state;
825
826 wl_list_for_each(state, &ws->focus_list, link)
827 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800828 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200829}
830
831static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100832animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
833 struct weston_view *from, struct weston_view *to)
834{
835 struct weston_output *output;
836 bool focus_surface_created = false;
837
838 /* FIXME: Only support dim animation using two layers */
839 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
840 return;
841
842 output = get_default_output(shell->compositor);
843 if (ws->fsurf_front == NULL && (from || to)) {
844 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800845 if (ws->fsurf_front == NULL)
846 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100847 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800848
849 ws->fsurf_back = create_focus_surface(shell->compositor, output);
850 if (ws->fsurf_back == NULL) {
851 focus_surface_destroy(ws->fsurf_front);
852 return;
853 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100854 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800855
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100856 focus_surface_created = true;
857 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300858 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
859 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100860 }
861
862 if (ws->focus_animation) {
863 weston_view_animation_destroy(ws->focus_animation);
864 ws->focus_animation = NULL;
865 }
866
867 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300868 weston_layer_entry_insert(&to->layer_link,
869 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100870 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300871 weston_layer_entry_insert(&ws->layer.view_list,
872 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100873
874 if (focus_surface_created) {
875 ws->focus_animation = weston_fade_run(
876 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200877 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100878 focus_animation_done, ws);
879 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300880 weston_layer_entry_insert(&from->layer_link,
881 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100882 ws->focus_animation = weston_stable_fade_run(
883 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200884 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100885 focus_animation_done, ws);
886 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300887 weston_layer_entry_insert(&ws->layer.view_list,
888 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100889 ws->focus_animation = weston_stable_fade_run(
890 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200891 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100892 focus_animation_done, ws);
893 }
894}
895
896static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200897workspace_destroy(struct workspace *ws)
898{
Jonas Ådahl04769742012-06-13 00:01:24 +0200899 struct focus_state *state, *next;
900
901 wl_list_for_each_safe(state, next, &ws->focus_list, link)
902 focus_state_destroy(state);
903
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100904 if (ws->fsurf_front)
905 focus_surface_destroy(ws->fsurf_front);
906 if (ws->fsurf_back)
907 focus_surface_destroy(ws->fsurf_back);
908
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200909 free(ws);
910}
911
Jonas Ådahl04769742012-06-13 00:01:24 +0200912static void
913seat_destroyed(struct wl_listener *listener, void *data)
914{
915 struct weston_seat *seat = data;
916 struct focus_state *state, *next;
917 struct workspace *ws = container_of(listener,
918 struct workspace,
919 seat_destroyed_listener);
920
921 wl_list_for_each_safe(state, next, &ws->focus_list, link)
922 if (state->seat == seat)
923 wl_list_remove(&state->link);
924}
925
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200926static struct workspace *
927workspace_create(void)
928{
929 struct workspace *ws = malloc(sizeof *ws);
930 if (ws == NULL)
931 return NULL;
932
933 weston_layer_init(&ws->layer, NULL);
934
Jonas Ådahl04769742012-06-13 00:01:24 +0200935 wl_list_init(&ws->focus_list);
936 wl_list_init(&ws->seat_destroyed_listener.link);
937 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100938 ws->fsurf_front = NULL;
939 ws->fsurf_back = NULL;
940 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200941
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200942 return ws;
943}
944
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200945static int
946workspace_is_empty(struct workspace *ws)
947{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300948 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200949}
950
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200951static struct workspace *
952get_workspace(struct desktop_shell *shell, unsigned int index)
953{
954 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200955 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200956 pws += index;
957 return *pws;
958}
959
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800960struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200961get_current_workspace(struct desktop_shell *shell)
962{
963 return get_workspace(shell, shell->workspaces.current);
964}
965
966static void
967activate_workspace(struct desktop_shell *shell, unsigned int index)
968{
969 struct workspace *ws;
970
971 ws = get_workspace(shell, index);
972 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
973
974 shell->workspaces.current = index;
975}
976
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200977static unsigned int
978get_output_height(struct weston_output *output)
979{
980 return abs(output->region.extents.y1 - output->region.extents.y2);
981}
982
983static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100984view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200985{
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200986 struct weston_transform *transform;
987
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100988 if (is_focus_view(view)) {
989 struct focus_surface *fsurf = get_focus_surface(view->surface);
990 transform = &fsurf->workspace_transform;
991 } else {
992 struct shell_surface *shsurf = get_shell_surface(view->surface);
993 transform = &shsurf->workspace_transform;
994 }
995
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200996 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100998 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200999
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001000 weston_matrix_init(&transform->matrix);
1001 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001002 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001004}
1005
1006static void
1007workspace_translate_out(struct workspace *ws, double fraction)
1008{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001009 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001010 unsigned int height;
1011 double d;
1012
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001013 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001014 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001015 d = height * fraction;
1016
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001017 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001018 }
1019}
1020
1021static void
1022workspace_translate_in(struct workspace *ws, double fraction)
1023{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001024 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001025 unsigned int height;
1026 double d;
1027
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001028 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001029 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001030
1031 if (fraction > 0)
1032 d = -(height - height * fraction);
1033 else
1034 d = height + height * fraction;
1035
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001036 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001037 }
1038}
1039
1040static void
Jonas Ådahle9d22502012-08-29 22:13:01 +02001041broadcast_current_workspace_state(struct desktop_shell *shell)
1042{
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001043 struct wl_resource *resource;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001044
Kristian Høgsberg2e3c3962013-09-11 12:00:47 -07001045 wl_resource_for_each(resource, &shell->workspaces.client_list)
1046 workspace_manager_send_state(resource,
Jonas Ådahle9d22502012-08-29 22:13:01 +02001047 shell->workspaces.current,
1048 shell->workspaces.num);
1049}
1050
1051static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001052reverse_workspace_change_animation(struct desktop_shell *shell,
1053 unsigned int index,
1054 struct workspace *from,
1055 struct workspace *to)
1056{
1057 shell->workspaces.current = index;
1058
1059 shell->workspaces.anim_to = to;
1060 shell->workspaces.anim_from = from;
1061 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1062 shell->workspaces.anim_timestamp = 0;
1063
Scott Moreau4272e632012-08-13 09:58:41 -06001064 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001065}
1066
1067static void
1068workspace_deactivate_transforms(struct workspace *ws)
1069{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001070 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001071 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001072
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001073 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001074 if (is_focus_view(view)) {
1075 struct focus_surface *fsurf = get_focus_surface(view->surface);
1076 transform = &fsurf->workspace_transform;
1077 } else {
1078 struct shell_surface *shsurf = get_shell_surface(view->surface);
1079 transform = &shsurf->workspace_transform;
1080 }
1081
1082 if (!wl_list_empty(&transform->link)) {
1083 wl_list_remove(&transform->link);
1084 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001085 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001086 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001087 }
1088}
1089
1090static void
1091finish_workspace_change_animation(struct desktop_shell *shell,
1092 struct workspace *from,
1093 struct workspace *to)
1094{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001095 struct weston_view *view;
1096
Scott Moreau4272e632012-08-13 09:58:41 -06001097 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001098
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001099 /* Views that extend past the bottom of the output are still
1100 * visible after the workspace animation ends but before its layer
1101 * is hidden. In that case, we need to damage below those views so
1102 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001103 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001104 weston_view_damage_below(view);
1105
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001106 wl_list_remove(&shell->workspaces.animation.link);
1107 workspace_deactivate_transforms(from);
1108 workspace_deactivate_transforms(to);
1109 shell->workspaces.anim_to = NULL;
1110
1111 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1112}
1113
1114static void
1115animate_workspace_change_frame(struct weston_animation *animation,
1116 struct weston_output *output, uint32_t msecs)
1117{
1118 struct desktop_shell *shell =
1119 container_of(animation, struct desktop_shell,
1120 workspaces.animation);
1121 struct workspace *from = shell->workspaces.anim_from;
1122 struct workspace *to = shell->workspaces.anim_to;
1123 uint32_t t;
1124 double x, y;
1125
1126 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1127 finish_workspace_change_animation(shell, from, to);
1128 return;
1129 }
1130
1131 if (shell->workspaces.anim_timestamp == 0) {
1132 if (shell->workspaces.anim_current == 0.0)
1133 shell->workspaces.anim_timestamp = msecs;
1134 else
1135 shell->workspaces.anim_timestamp =
1136 msecs -
1137 /* Invers of movement function 'y' below. */
1138 (asin(1.0 - shell->workspaces.anim_current) *
1139 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1140 M_2_PI);
1141 }
1142
1143 t = msecs - shell->workspaces.anim_timestamp;
1144
1145 /*
1146 * x = [0, π/2]
1147 * y(x) = sin(x)
1148 */
1149 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1150 y = sin(x);
1151
1152 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001153 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001154
1155 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1156 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1157 shell->workspaces.anim_current = y;
1158
Scott Moreau4272e632012-08-13 09:58:41 -06001159 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001160 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001161 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001162 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001163}
1164
1165static void
1166animate_workspace_change(struct desktop_shell *shell,
1167 unsigned int index,
1168 struct workspace *from,
1169 struct workspace *to)
1170{
1171 struct weston_output *output;
1172
1173 int dir;
1174
1175 if (index > shell->workspaces.current)
1176 dir = -1;
1177 else
1178 dir = 1;
1179
1180 shell->workspaces.current = index;
1181
1182 shell->workspaces.anim_dir = dir;
1183 shell->workspaces.anim_from = from;
1184 shell->workspaces.anim_to = to;
1185 shell->workspaces.anim_current = 0.0;
1186 shell->workspaces.anim_timestamp = 0;
1187
1188 output = container_of(shell->compositor->output_list.next,
1189 struct weston_output, link);
1190 wl_list_insert(&output->animation_list,
1191 &shell->workspaces.animation.link);
1192
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001193 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001194
1195 workspace_translate_in(to, 0);
1196
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001197 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001198
Scott Moreau4272e632012-08-13 09:58:41 -06001199 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001200}
1201
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001202static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001203update_workspace(struct desktop_shell *shell, unsigned int index,
1204 struct workspace *from, struct workspace *to)
1205{
1206 shell->workspaces.current = index;
1207 wl_list_insert(&from->layer.link, &to->layer.link);
1208 wl_list_remove(&from->layer.link);
1209}
1210
1211static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001212change_workspace(struct desktop_shell *shell, unsigned int index)
1213{
1214 struct workspace *from;
1215 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001216 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001217
1218 if (index == shell->workspaces.current)
1219 return;
1220
1221 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001222 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001223 return;
1224
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001225 from = get_current_workspace(shell);
1226 to = get_workspace(shell, index);
1227
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001228 if (shell->workspaces.anim_from == to &&
1229 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001230 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001231 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001232 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001233 return;
1234 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001235
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001236 if (shell->workspaces.anim_to != NULL)
1237 finish_workspace_change_animation(shell,
1238 shell->workspaces.anim_from,
1239 shell->workspaces.anim_to);
1240
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001241 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001242
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001243 if (shell->focus_animation_type != ANIMATION_NONE) {
1244 wl_list_for_each(state, &from->focus_list, link)
1245 if (state->keyboard_focus)
1246 animate_focus_change(shell, from,
1247 get_default_view(state->keyboard_focus), NULL);
1248
1249 wl_list_for_each(state, &to->focus_list, link)
1250 if (state->keyboard_focus)
1251 animate_focus_change(shell, to,
1252 NULL, get_default_view(state->keyboard_focus));
1253 }
1254
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001255 if (workspace_is_empty(to) && workspace_is_empty(from))
1256 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001257 else
1258 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001259
1260 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001261}
1262
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001263static bool
1264workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1265{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001266 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001267 struct wl_list *e;
1268
1269 if (wl_list_empty(list))
1270 return false;
1271
1272 e = list->next;
1273
1274 if (e->next != list)
1275 return false;
1276
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001277 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001278}
1279
1280static void
Philip Withnall659163d2013-11-25 18:01:36 +00001281move_surface_to_workspace(struct desktop_shell *shell,
1282 struct shell_surface *shsurf,
1283 uint32_t workspace)
Jonas Ådahle9d22502012-08-29 22:13:01 +02001284{
1285 struct workspace *from;
1286 struct workspace *to;
1287 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +03001288 struct weston_surface *focus;
Philip Withnall659163d2013-11-25 18:01:36 +00001289 struct weston_view *view;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001290
1291 if (workspace == shell->workspaces.current)
1292 return;
1293
Philip Withnall659163d2013-11-25 18:01:36 +00001294 view = get_default_view(shsurf->surface);
1295 if (!view)
1296 return;
1297
1298 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1299
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02001300 if (workspace >= shell->workspaces.num)
1301 workspace = shell->workspaces.num - 1;
1302
Jonas Ådahle9d22502012-08-29 22:13:01 +02001303 from = get_current_workspace(shell);
1304 to = get_workspace(shell, workspace);
1305
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001306 weston_layer_entry_remove(&view->layer_link);
1307 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001308
Philip Withnall648a4dd2013-11-25 18:01:44 +00001309 shell_surface_update_child_surface_layers(shsurf);
1310
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 drop_focus_state(shell, from, view->surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001312 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1313 if (!seat->keyboard)
1314 continue;
1315
1316 focus = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001317 if (focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001318 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001319 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001320
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 weston_view_damage_below(view);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001322}
1323
1324static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001325take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001326 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001327 unsigned int index)
1328{
Pekka Paalanen01388e22013-04-25 13:57:44 +03001329 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001331 struct shell_surface *shsurf;
1332 struct workspace *from;
1333 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001334 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001335
Pekka Paalanen01388e22013-04-25 13:57:44 +03001336 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001337 view = get_default_view(surface);
1338 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001339 index == shell->workspaces.current ||
1340 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001341 return;
1342
1343 from = get_current_workspace(shell);
1344 to = get_workspace(shell, index);
1345
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001346 weston_layer_entry_remove(&view->layer_link);
1347 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001348
Philip Withnall659163d2013-11-25 18:01:36 +00001349 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001350 if (shsurf != NULL)
1351 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001352
Jonas Ådahle9d22502012-08-29 22:13:01 +02001353 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001354 drop_focus_state(shell, from, surface);
1355
1356 if (shell->workspaces.anim_from == to &&
1357 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +02001358 wl_list_remove(&to->layer.link);
1359 wl_list_insert(from->layer.link.prev, &to->layer.link);
1360
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001361 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001362 broadcast_current_workspace_state(shell);
1363
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001364 return;
1365 }
1366
1367 if (shell->workspaces.anim_to != NULL)
1368 finish_workspace_change_animation(shell,
1369 shell->workspaces.anim_from,
1370 shell->workspaces.anim_to);
1371
1372 if (workspace_is_empty(from) &&
1373 workspace_has_only(to, surface))
1374 update_workspace(shell, index, from, to);
1375 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001376 if (shsurf != NULL &&
1377 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001378 wl_list_insert(&shell->workspaces.anim_sticky_list,
1379 &shsurf->workspace_transform.link);
1380
1381 animate_workspace_change(shell, index, from, to);
1382 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001383
1384 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +02001385
1386 state = ensure_focus_state(shell, seat);
1387 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001388 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001389}
1390
1391static void
1392workspace_manager_move_surface(struct wl_client *client,
1393 struct wl_resource *resource,
1394 struct wl_resource *surface_resource,
1395 uint32_t workspace)
1396{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001397 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001398 struct weston_surface *surface =
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001399 wl_resource_get_user_data(surface_resource);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001400 struct weston_surface *main_surface;
Philip Withnall659163d2013-11-25 18:01:36 +00001401 struct shell_surface *shell_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +02001402
Pekka Paalanen01388e22013-04-25 13:57:44 +03001403 main_surface = weston_surface_get_main_surface(surface);
Philip Withnall659163d2013-11-25 18:01:36 +00001404 shell_surface = get_shell_surface(main_surface);
1405 if (shell_surface == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 return;
Philip Withnall659163d2013-11-25 18:01:36 +00001407
1408 move_surface_to_workspace(shell, shell_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001409}
1410
1411static const struct workspace_manager_interface workspace_manager_implementation = {
1412 workspace_manager_move_surface,
1413};
1414
1415static void
1416unbind_resource(struct wl_resource *resource)
1417{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001418 wl_list_remove(wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001419}
1420
1421static void
1422bind_workspace_manager(struct wl_client *client,
1423 void *data, uint32_t version, uint32_t id)
1424{
1425 struct desktop_shell *shell = data;
1426 struct wl_resource *resource;
1427
Jason Ekstranda85118c2013-06-27 20:17:02 -05001428 resource = wl_resource_create(client,
1429 &workspace_manager_interface, 1, id);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001430
1431 if (resource == NULL) {
1432 weston_log("couldn't add workspace manager object");
1433 return;
1434 }
1435
Jason Ekstranda85118c2013-06-27 20:17:02 -05001436 wl_resource_set_implementation(resource,
1437 &workspace_manager_implementation,
1438 shell, unbind_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001439 wl_list_insert(&shell->workspaces.client_list,
1440 wl_resource_get_link(resource));
Jonas Ådahle9d22502012-08-29 22:13:01 +02001441
1442 workspace_manager_send_state(resource,
1443 shell->workspaces.current,
1444 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001445}
1446
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001447static void
Rusty Lynch1084da52013-08-15 09:10:08 -07001448touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1449 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1450{
1451}
1452
1453static void
1454touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1455{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001456 struct weston_touch_move_grab *move =
1457 (struct weston_touch_move_grab *) container_of(
1458 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001459
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001460 if (touch_id == 0)
1461 move->active = 0;
1462
Jonas Ådahl9484b692013-12-02 22:05:03 +01001463 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001464 shell_touch_grab_end(&move->base);
1465 free(move);
1466 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001467}
1468
1469static void
1470touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1471 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1472{
1473 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1474 struct shell_surface *shsurf = move->base.shsurf;
1475 struct weston_surface *es;
1476 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1477 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1478
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001479 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001480 return;
1481
1482 es = shsurf->surface;
1483
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001484 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001485
1486 weston_compositor_schedule_repaint(es->compositor);
1487}
1488
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001489static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001490touch_move_grab_frame(struct weston_touch_grab *grab)
1491{
1492}
1493
1494static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001495touch_move_grab_cancel(struct weston_touch_grab *grab)
1496{
1497 struct weston_touch_move_grab *move =
1498 (struct weston_touch_move_grab *) container_of(
1499 grab, struct shell_touch_grab, grab);
1500
1501 shell_touch_grab_end(&move->base);
1502 free(move);
1503}
1504
Rusty Lynch1084da52013-08-15 09:10:08 -07001505static const struct weston_touch_grab_interface touch_move_grab_interface = {
1506 touch_move_grab_down,
1507 touch_move_grab_up,
1508 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001509 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001510 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001511};
1512
1513static int
1514surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1515{
1516 struct weston_touch_move_grab *move;
1517
1518 if (!shsurf)
1519 return -1;
1520
Ander Conselvan de Oliveira6d43f042014-05-07 14:22:23 +03001521 if (shsurf->state.fullscreen || shsurf->state.maximized)
Rusty Lynch1084da52013-08-15 09:10:08 -07001522 return 0;
1523
1524 move = malloc(sizeof *move);
1525 if (!move)
1526 return -1;
1527
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001528 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001529 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001530 seat->touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001531 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Rusty Lynch1084da52013-08-15 09:10:08 -07001532 seat->touch->grab_y;
1533
1534 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1535 seat->touch);
1536
1537 return 0;
1538}
1539
1540static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001541noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001542{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001543}
1544
1545static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001546constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1547{
1548 struct shell_surface *shsurf = move->base.shsurf;
1549 struct weston_pointer *pointer = move->base.grab.pointer;
Kristian Høgsberg3434b332014-04-29 16:38:23 -07001550 int x, y, panel_height, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001551 const int safety = 50;
1552
1553 x = wl_fixed_to_int(pointer->x + move->dx);
1554 y = wl_fixed_to_int(pointer->y + move->dy);
1555
1556 panel_height = get_output_panel_height(shsurf->shell,
1557 shsurf->surface->output);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001558 bottom = y + shsurf->geometry.height;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001559 if (bottom - panel_height < safety)
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001560 y = panel_height + safety - shsurf->geometry.height;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001561
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001562 if (move->client_initiated && y + shsurf->geometry.y < panel_height)
1563 y = panel_height - shsurf->geometry.y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001564
1565 *cx = x;
1566 *cy = y;
1567}
1568
1569static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001570move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1571 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001572{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001573 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001574 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001575 struct shell_surface *shsurf = move->base.shsurf;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001576 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001577
1578 weston_pointer_move(pointer, x, y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001579 if (!shsurf)
1580 return;
1581
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001582 constrain_position(move, &cx, &cy);
1583
1584 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001585
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001587}
1588
1589static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001590move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001591 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001592{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001593 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1594 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001595 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001596 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001597
Daniel Stone4dbadb12012-05-30 16:31:51 +01001598 if (pointer->button_count == 0 &&
1599 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001600 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001601 free(grab);
1602 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001603}
1604
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001605static void
1606move_grab_cancel(struct weston_pointer_grab *grab)
1607{
1608 struct shell_grab *shell_grab =
1609 container_of(grab, struct shell_grab, grab);
1610
1611 shell_grab_end(shell_grab);
1612 free(grab);
1613}
1614
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001615static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001616 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001617 move_grab_motion,
1618 move_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001619 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001620};
1621
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001622static int
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001623surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1624 int client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001625{
1626 struct weston_move_grab *move;
1627
1628 if (!shsurf)
1629 return -1;
1630
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001631 if (shsurf->grabbed ||
1632 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001633 return 0;
1634
1635 move = malloc(sizeof *move);
1636 if (!move)
1637 return -1;
1638
Jason Ekstranda7af7042013-10-12 22:38:11 -05001639 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001640 seat->pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001641 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001642 seat->pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001643 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001644
1645 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001646 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001647
1648 return 0;
1649}
1650
1651static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001652common_surface_move(struct wl_resource *resource,
1653 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001654{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001655 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001656 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001657 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001658
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001659 if (seat->pointer &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001660 seat->pointer->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001661 seat->pointer->button_count > 0 &&
1662 seat->pointer->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001663 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001664 if ((surface == shsurf->surface) &&
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001665 (surface_move(shsurf, seat, 1) < 0))
Rusty Lynch1084da52013-08-15 09:10:08 -07001666 wl_resource_post_no_memory(resource);
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001667 } else if (seat->touch &&
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001668 seat->touch->focus &&
Kristian Høgsberge1b655d2013-08-28 23:16:20 -07001669 seat->touch->grab_serial == serial) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001670 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001671 if ((surface == shsurf->surface) &&
Rusty Lynch1084da52013-08-15 09:10:08 -07001672 (surface_touch_move(shsurf, seat) < 0))
1673 wl_resource_post_no_memory(resource);
1674 }
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001675}
1676
Rafael Antognollie2a34552013-12-03 15:35:45 -02001677static void
1678shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1679 struct wl_resource *seat_resource, uint32_t serial)
1680{
1681 common_surface_move(resource, seat_resource, serial);
1682}
1683
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001684struct weston_resize_grab {
1685 struct shell_grab base;
1686 uint32_t edges;
1687 int32_t width, height;
1688};
1689
1690static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001691resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1692 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001693{
1694 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001695 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001696 struct shell_surface *shsurf = resize->base.shsurf;
1697 int32_t width, height;
1698 wl_fixed_t from_x, from_y;
1699 wl_fixed_t to_x, to_y;
1700
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001701 weston_pointer_move(pointer, x, y);
1702
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001703 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001704 return;
1705
Jason Ekstranda7af7042013-10-12 22:38:11 -05001706 weston_view_from_global_fixed(shsurf->view,
1707 pointer->grab_x, pointer->grab_y,
1708 &from_x, &from_y);
1709 weston_view_from_global_fixed(shsurf->view,
1710 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001711
1712 width = resize->width;
1713 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1714 width += wl_fixed_to_int(from_x - to_x);
1715 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1716 width += wl_fixed_to_int(to_x - from_x);
1717 }
1718
1719 height = resize->height;
1720 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1721 height += wl_fixed_to_int(from_y - to_y);
1722 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1723 height += wl_fixed_to_int(to_y - from_y);
1724 }
1725
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001726 shsurf->client->send_configure(shsurf->surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001727}
1728
1729static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001730send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001731{
1732 struct shell_surface *shsurf = get_shell_surface(surface);
1733
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08001734 assert(shsurf);
1735
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001736 if (shsurf->resource)
1737 wl_shell_surface_send_configure(shsurf->resource,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04001738 shsurf->resize_edges,
1739 width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001740}
1741
1742static const struct weston_shell_client shell_client = {
1743 send_configure
1744};
1745
1746static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001747resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001748 uint32_t time, uint32_t button, uint32_t state_w)
1749{
1750 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001751 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001752 enum wl_pointer_button_state state = state_w;
1753
1754 if (pointer->button_count == 0 &&
1755 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1756 shell_grab_end(&resize->base);
1757 free(grab);
1758 }
1759}
1760
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001761static void
1762resize_grab_cancel(struct weston_pointer_grab *grab)
1763{
1764 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1765
1766 shell_grab_end(&resize->base);
1767 free(grab);
1768}
1769
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001770static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001771 noop_grab_focus,
1772 resize_grab_motion,
1773 resize_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001774 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001775};
1776
Giulio Camuffob8366642013-04-25 13:57:46 +03001777/*
1778 * Returns the bounding box of a surface and all its sub-surfaces,
1779 * in the surface coordinates system. */
1780static void
1781surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1782 int32_t *y, int32_t *w, int32_t *h) {
1783 pixman_region32_t region;
1784 pixman_box32_t *box;
1785 struct weston_subsurface *subsurface;
1786
1787 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001788 surface->width,
1789 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001790
1791 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1792 pixman_region32_union_rect(&region, &region,
1793 subsurface->position.x,
1794 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001795 subsurface->surface->width,
1796 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001797 }
1798
1799 box = pixman_region32_extents(&region);
1800 if (x)
1801 *x = box->x1;
1802 if (y)
1803 *y = box->y1;
1804 if (w)
1805 *w = box->x2 - box->x1;
1806 if (h)
1807 *h = box->y2 - box->y1;
1808
1809 pixman_region32_fini(&region);
1810}
1811
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001812static int
1813surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001814 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001815{
1816 struct weston_resize_grab *resize;
1817
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001818 if (shsurf->grabbed ||
1819 shsurf->state.fullscreen || shsurf->state.maximized)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001820 return 0;
1821
1822 if (edges == 0 || edges > 15 ||
1823 (edges & 3) == 3 || (edges & 12) == 12)
1824 return 0;
1825
1826 resize = malloc(sizeof *resize);
1827 if (!resize)
1828 return -1;
1829
1830 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001831
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001832 resize->width = shsurf->geometry.width;
1833 resize->height = shsurf->geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001834
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001835 shsurf->resize_edges = edges;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04001836 shell_surface_state_changed(shsurf);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001837 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001838 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001839
1840 return 0;
1841}
1842
1843static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001844common_surface_resize(struct wl_resource *resource,
1845 struct wl_resource *seat_resource, uint32_t serial,
1846 uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001847{
Jason Ekstrand44a38632013-06-14 10:08:00 -05001848 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
Jason Ekstrand651f00e2013-06-14 10:07:54 -05001849 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001850 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001851
Emilio Pozuelo Monfort5872b682014-06-18 17:48:58 +02001852 if (seat->pointer == NULL ||
1853 seat->pointer->button_count == 0 ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001854 seat->pointer->grab_serial != serial ||
Kristian Høgsberg70f29012014-01-09 15:43:17 -08001855 seat->pointer->focus == NULL)
1856 return;
1857
1858 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1859 if (surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001860 return;
1861
Kristian Høgsberge3148752013-05-06 23:19:49 -04001862 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001863 wl_resource_post_no_memory(resource);
1864}
1865
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001866static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02001867shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1868 struct wl_resource *seat_resource, uint32_t serial,
1869 uint32_t edges)
1870{
1871 common_surface_resize(resource, seat_resource, serial, edges);
1872}
1873
1874static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001875busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001876{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001877 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001878 struct weston_pointer *pointer = base->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001879 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001880 wl_fixed_t sx, sy;
1881
Jason Ekstranda7af7042013-10-12 22:38:11 -05001882 view = weston_compositor_pick_view(pointer->seat->compositor,
1883 pointer->x, pointer->y,
1884 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001885
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001886 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1887 shell_grab_end(grab);
1888 free(grab);
1889 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001890}
1891
1892static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001893busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1894 wl_fixed_t x, wl_fixed_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001895{
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001896 weston_pointer_move(grab->pointer, x, y);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001897}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001898
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001899static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001900busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001901 uint32_t time, uint32_t button, uint32_t state)
1902{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001903 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001904 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001905 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001906
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001907 if (shsurf && button == BTN_LEFT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001908 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001909 surface_move(shsurf, seat, 0);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001910 } else if (shsurf && button == BTN_RIGHT && state) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01001911 activate(shsurf->shell, shsurf->surface, seat, true);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001912 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001913 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001914}
1915
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001916static void
1917busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1918{
1919 struct shell_grab *grab = (struct shell_grab *) base;
1920
1921 shell_grab_end(grab);
1922 free(grab);
1923}
1924
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001925static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001926 busy_cursor_grab_focus,
1927 busy_cursor_grab_motion,
1928 busy_cursor_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001929 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001930};
1931
1932static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001933set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001934{
1935 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001936
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001937 if (pointer->grab->interface == &busy_cursor_grab_interface)
1938 return;
1939
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001940 grab = malloc(sizeof *grab);
1941 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001942 return;
1943
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001944 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1945 DESKTOP_SHELL_CURSOR_BUSY);
Ander Conselvan de Oliveirae5a1aee2014-04-09 17:39:39 +03001946 /* Mark the shsurf as ungrabbed so that button binding is able
1947 * to move it. */
1948 shsurf->grabbed = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001949}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001950
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001951static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001952end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001953{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001954 struct shell_grab *grab;
1955 struct weston_seat *seat;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001956
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001957 wl_list_for_each(seat, &compositor->seat_list, link) {
1958 if (seat->pointer == NULL)
1959 continue;
1960
1961 grab = (struct shell_grab *) seat->pointer->grab;
1962 if (grab->grab.interface == &busy_cursor_grab_interface &&
1963 wl_resource_get_client(grab->shsurf->resource) == client) {
1964 shell_grab_end(grab);
1965 free(grab);
1966 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001967 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001968}
1969
Scott Moreau9521d5e2012-04-19 13:06:17 -06001970static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001971handle_shell_client_destroy(struct wl_listener *listener, void *data);
1972
1973static int
1974xdg_ping_timeout_handler(void *data)
1975{
1976 struct shell_client *sc = data;
1977 struct weston_seat *seat;
1978 struct shell_surface *shsurf;
1979
1980 /* Client is not responding */
1981 sc->unresponsive = 1;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001982 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1983 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1984 continue;
1985 if (seat->pointer->focus->surface->resource == NULL)
1986 continue;
1987
1988 shsurf = get_shell_surface(seat->pointer->focus->surface);
1989 if (shsurf &&
1990 wl_resource_get_client(shsurf->resource) == sc->client)
1991 set_busy_cursor(shsurf, seat->pointer);
1992 }
1993
1994 return 1;
1995}
1996
1997static void
1998handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1999{
2000 struct weston_compositor *compositor = shsurf->shell->compositor;
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002001 struct shell_client *sc = shsurf->owner;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002002 struct wl_event_loop *loop;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002003 static const int ping_timeout = 200;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002004
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002005 if (sc->unresponsive) {
2006 xdg_ping_timeout_handler(sc);
2007 return;
2008 }
2009
2010 sc->ping_serial = serial;
2011 loop = wl_display_get_event_loop(compositor->wl_display);
2012 if (sc->ping_timer == NULL)
2013 sc->ping_timer =
2014 wl_event_loop_add_timer(loop,
2015 xdg_ping_timeout_handler, sc);
2016 if (sc->ping_timer == NULL)
2017 return;
2018
2019 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2020
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002021 if (shell_surface_is_xdg_surface(shsurf) ||
2022 shell_surface_is_xdg_popup(shsurf))
2023 xdg_shell_send_ping(sc->resource, serial);
2024 else if (shell_surface_is_wl_shell_surface(shsurf))
2025 wl_shell_surface_send_ping(shsurf->resource, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002026}
2027
Scott Moreauff1db4a2012-04-17 19:06:18 -06002028static void
2029ping_handler(struct weston_surface *surface, uint32_t serial)
2030{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002031 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002032
2033 if (!shsurf)
2034 return;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002035 if (!shsurf->resource)
Kristian Høgsbergca535c12012-04-21 23:20:07 -04002036 return;
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03002037 if (shsurf->surface == shsurf->shell->grab_surface)
2038 return;
2039
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002040 handle_xdg_ping(shsurf, serial);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002041}
2042
2043static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002044handle_pointer_focus(struct wl_listener *listener, void *data)
2045{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002046 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002047 struct weston_view *view = pointer->focus;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002048 struct weston_compositor *compositor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002049 uint32_t serial;
2050
Jason Ekstranda7af7042013-10-12 22:38:11 -05002051 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002052 return;
2053
Jason Ekstranda7af7042013-10-12 22:38:11 -05002054 compositor = view->surface->compositor;
Kristian Høgsberge11ef642014-02-11 16:35:22 -08002055 serial = wl_display_next_serial(compositor->wl_display);
2056 ping_handler(view->surface, serial);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002057}
2058
2059static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002060shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2061{
2062 if (--shsurf->focus_count == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002063 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002064}
2065
2066static void
2067shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2068{
2069 if (shsurf->focus_count++ == 0)
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002070 shell_surface_state_changed(shsurf);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05002071}
2072
2073static void
2074handle_keyboard_focus(struct wl_listener *listener, void *data)
2075{
2076 struct weston_keyboard *keyboard = data;
2077 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2078
2079 if (seat->focused_surface) {
2080 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2081 if (shsurf)
2082 shell_surface_lose_keyboard_focus(shsurf);
2083 }
2084
2085 seat->focused_surface = keyboard->focus;
2086
2087 if (seat->focused_surface) {
2088 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2089 if (shsurf)
2090 shell_surface_gain_keyboard_focus(shsurf);
2091 }
2092}
2093
2094static void
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002095shell_client_pong(struct shell_client *sc, uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002096{
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002097 if (sc->ping_serial != serial)
2098 return;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002099
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002100 sc->unresponsive = 0;
2101 end_busy_cursor(sc->shell->compositor, sc->client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002102
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002103 if (sc->ping_timer) {
2104 wl_event_source_remove(sc->ping_timer);
2105 sc->ping_timer = NULL;
2106 }
2107
2108}
2109
2110static void
2111shell_surface_pong(struct wl_client *client,
2112 struct wl_resource *resource, uint32_t serial)
2113{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05002114 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2115 struct shell_client *sc = shsurf->owner;
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002116
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08002117 shell_client_pong(sc, serial);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002118}
2119
2120static void
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002121set_title(struct shell_surface *shsurf, const char *title)
2122{
2123 free(shsurf->title);
2124 shsurf->title = strdup(title);
2125}
2126
2127static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002128set_window_geometry(struct shell_surface *shsurf,
2129 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002130{
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04002131 shsurf->next_geometry.x = x;
2132 shsurf->next_geometry.y = y;
2133 shsurf->next_geometry.width = width;
2134 shsurf->next_geometry.height = height;
2135 shsurf->has_next_geometry = true;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07002136}
2137
2138static void
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002139shell_surface_set_title(struct wl_client *client,
2140 struct wl_resource *resource, const char *title)
2141{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002142 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002143
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002144 set_title(shsurf, title);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002145}
2146
2147static void
2148shell_surface_set_class(struct wl_client *client,
2149 struct wl_resource *resource, const char *class)
2150{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002151 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002152
2153 free(shsurf->class);
2154 shsurf->class = strdup(class);
2155}
2156
Alex Wu4539b082012-03-01 12:57:46 +08002157static void
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002158restore_output_mode(struct weston_output *output)
2159{
Hardening57388e42013-09-18 23:56:36 +02002160 if (output->current_mode != output->original_mode ||
2161 (int32_t)output->current_scale != output->original_scale)
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002162 weston_output_switch_mode(output,
Hardening57388e42013-09-18 23:56:36 +02002163 output->original_mode,
2164 output->original_scale,
2165 WESTON_MODE_SWITCH_RESTORE_NATIVE);
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002166}
2167
2168static void
2169restore_all_output_modes(struct weston_compositor *compositor)
2170{
2171 struct weston_output *output;
2172
2173 wl_list_for_each(output, &compositor->output_list, link)
2174 restore_output_mode(output);
2175}
2176
Philip Withnall07926d92013-11-25 18:01:40 +00002177/* The surface will be inserted into the list immediately after the link
2178 * returned by this function (i.e. will be stacked immediately above the
2179 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002180static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00002181shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2182{
2183 struct workspace *ws;
Kristian Høgsbergead52422014-01-01 13:51:52 -08002184 struct weston_view *parent;
Philip Withnall07926d92013-11-25 18:01:40 +00002185
2186 switch (shsurf->type) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002187 case SHELL_SURFACE_XWAYLAND:
2188 return &shsurf->shell->fullscreen_layer.view_list;
2189
2190 case SHELL_SURFACE_NONE:
2191 return NULL;
2192
Kristian Høgsbergead52422014-01-01 13:51:52 -08002193 case SHELL_SURFACE_POPUP:
2194 case SHELL_SURFACE_TOPLEVEL:
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002195 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002196 return &shsurf->shell->fullscreen_layer.view_list;
Rafael Antognollied207b42013-12-03 15:35:43 -02002197 } else if (shsurf->parent) {
Kristian Høgsbergd55db692014-01-01 12:26:14 -08002198 /* Move the surface to its parent layer so
2199 * that surfaces which are transient for
2200 * fullscreen surfaces don't get hidden by the
2201 * fullscreen surfaces. */
Rafael Antognollied207b42013-12-03 15:35:43 -02002202
2203 /* TODO: Handle a parent with multiple views */
2204 parent = get_default_view(shsurf->parent);
2205 if (parent)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002206 return container_of(parent->layer_link.link.prev,
2207 struct weston_layer_entry, link);
Rafael Antognollied207b42013-12-03 15:35:43 -02002208 }
Philip Withnall07926d92013-11-25 18:01:40 +00002209
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002210 /* Move the surface to a normal workspace layer so that surfaces
2211 * which were previously fullscreen or transient are no longer
2212 * rendered on top. */
2213 ws = get_current_workspace(shsurf->shell);
2214 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00002215 }
2216
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002217 assert(0 && "Unknown shell surface type");
Philip Withnall07926d92013-11-25 18:01:40 +00002218}
2219
Philip Withnall648a4dd2013-11-25 18:01:44 +00002220static void
2221shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2222{
2223 struct shell_surface *child;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002224 struct weston_layer_entry *prev;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002225
2226 /* Move the child layers to the same workspace as shsurf. They will be
2227 * stacked above shsurf. */
2228 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002229 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
Ander Conselvan de Oliveirafacc0cc2014-04-10 15:35:58 +03002230 weston_view_damage_below(child->view);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002231 weston_view_geometry_dirty(child->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002232 prev = container_of(shsurf->view->layer_link.link.prev,
2233 struct weston_layer_entry, link);
2234 weston_layer_entry_remove(&child->view->layer_link);
2235 weston_layer_entry_insert(prev,
2236 &child->view->layer_link);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002237 weston_view_geometry_dirty(child->view);
2238 weston_surface_damage(child->surface);
2239
2240 /* Recurse. We don’t expect this to recurse very far (if
2241 * at all) because that would imply we have transient
2242 * (or popup) children of transient surfaces, which
2243 * would be unusual. */
2244 shell_surface_update_child_surface_layers(child);
2245 }
2246 }
2247}
2248
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002249/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00002250 * geometry to ensure the changes are redrawn.
2251 *
2252 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2253 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002254static void
2255shell_surface_update_layer(struct shell_surface *shsurf)
2256{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002257 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002258
2259 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2260
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03002261 if (new_layer_link == NULL)
2262 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002263 if (new_layer_link == &shsurf->view->layer_link)
2264 return;
2265
2266 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002267 weston_layer_entry_remove(&shsurf->view->layer_link);
2268 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002269 weston_view_geometry_dirty(shsurf->view);
2270 weston_surface_damage(shsurf->surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00002271
2272 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002273}
2274
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002275static void
Philip Withnalldc4332f2013-11-25 18:01:38 +00002276shell_surface_set_parent(struct shell_surface *shsurf,
2277 struct weston_surface *parent)
2278{
2279 shsurf->parent = parent;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002280
2281 wl_list_remove(&shsurf->children_link);
2282 wl_list_init(&shsurf->children_link);
2283
2284 /* Insert into the parent surface’s child list. */
2285 if (parent != NULL) {
2286 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2287 if (parent_shsurf != NULL)
2288 wl_list_insert(&parent_shsurf->children_list,
2289 &shsurf->children_link);
2290 }
Philip Withnalldc4332f2013-11-25 18:01:38 +00002291}
2292
2293static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00002294shell_surface_set_output(struct shell_surface *shsurf,
2295 struct weston_output *output)
2296{
2297 struct weston_surface *es = shsurf->surface;
2298
2299 /* get the default output, if the client set it as NULL
2300 check whether the ouput is available */
2301 if (output)
2302 shsurf->output = output;
2303 else if (es->output)
2304 shsurf->output = es->output;
2305 else
2306 shsurf->output = get_default_output(es->compositor);
2307}
2308
2309static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002310surface_clear_next_states(struct shell_surface *shsurf)
2311{
2312 shsurf->next_state.maximized = false;
2313 shsurf->next_state.fullscreen = false;
2314
2315 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2316 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2317 shsurf->state_changed = true;
2318}
2319
2320static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002321set_toplevel(struct shell_surface *shsurf)
2322{
Kristian Høgsberg41fbf6f2013-12-05 22:31:25 -08002323 shell_surface_set_parent(shsurf, NULL);
2324 surface_clear_next_states(shsurf);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002325 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002326
2327 /* The layer_link is updated in set_surface_type(),
2328 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002329}
2330
2331static void
2332shell_surface_set_toplevel(struct wl_client *client,
2333 struct wl_resource *resource)
2334{
2335 struct shell_surface *surface = wl_resource_get_user_data(resource);
2336
2337 set_toplevel(surface);
2338}
2339
2340static void
2341set_transient(struct shell_surface *shsurf,
2342 struct weston_surface *parent, int x, int y, uint32_t flags)
2343{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002344 assert(parent != NULL);
2345
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002346 shell_surface_set_parent(shsurf, parent);
2347
2348 surface_clear_next_states(shsurf);
2349
Philip Withnallbecb77e2013-11-25 18:01:30 +00002350 shsurf->transient.x = x;
2351 shsurf->transient.y = y;
2352 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002353
Rafael Antognollied207b42013-12-03 15:35:43 -02002354 shsurf->next_state.relative = true;
Kristian Høgsberga1df7ac2013-12-31 15:01:01 -08002355 shsurf->state_changed = true;
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002356 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnall648a4dd2013-11-25 18:01:44 +00002357
2358 /* The layer_link is updated in set_surface_type(),
2359 * called from configure. */
Philip Withnallbecb77e2013-11-25 18:01:30 +00002360}
2361
2362static void
2363shell_surface_set_transient(struct wl_client *client,
2364 struct wl_resource *resource,
2365 struct wl_resource *parent_resource,
2366 int x, int y, uint32_t flags)
2367{
2368 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2369 struct weston_surface *parent =
2370 wl_resource_get_user_data(parent_resource);
2371
2372 set_transient(shsurf, parent, x, y, flags);
2373}
2374
2375static void
2376set_fullscreen(struct shell_surface *shsurf,
2377 uint32_t method,
2378 uint32_t framerate,
2379 struct weston_output *output)
2380{
Philip Withnall352e7ed2013-11-25 18:01:35 +00002381 shell_surface_set_output(shsurf, output);
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002382 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002383
2384 shsurf->fullscreen_output = shsurf->output;
2385 shsurf->fullscreen.type = method;
2386 shsurf->fullscreen.framerate = framerate;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002387
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002388 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002389}
2390
2391static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002392weston_view_set_initial_position(struct weston_view *view,
2393 struct desktop_shell *shell);
2394
2395static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002396unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002397{
Philip Withnallf85fe842013-11-25 18:01:37 +00002398 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wubd3354b2012-04-17 17:20:49 +08002399 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2400 shell_surface_is_top_fullscreen(shsurf)) {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002401 restore_output_mode(shsurf->fullscreen_output);
Alex Wubd3354b2012-04-17 17:20:49 +08002402 }
Philip Withnallf85fe842013-11-25 18:01:37 +00002403
Alex Wu4539b082012-03-01 12:57:46 +08002404 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2405 shsurf->fullscreen.framerate = 0;
Philip Withnallf85fe842013-11-25 18:01:37 +00002406
Alex Wu4539b082012-03-01 12:57:46 +08002407 wl_list_remove(&shsurf->fullscreen.transform.link);
2408 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002409
Jason Ekstranda7af7042013-10-12 22:38:11 -05002410 if (shsurf->fullscreen.black_view)
2411 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2412 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002413
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002414 if (shsurf->saved_position_valid)
2415 weston_view_set_position(shsurf->view,
2416 shsurf->saved_x, shsurf->saved_y);
2417 else
2418 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2419
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002420 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002421 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002422 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002423 shsurf->saved_rotation_valid = false;
2424 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02002425
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002426 /* Layer is updated in set_surface_type(). */
Alex Wu4539b082012-03-01 12:57:46 +08002427}
2428
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002429static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002430shell_surface_set_fullscreen(struct wl_client *client,
2431 struct wl_resource *resource,
2432 uint32_t method,
2433 uint32_t framerate,
2434 struct wl_resource *output_resource)
2435{
2436 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2437 struct weston_output *output;
2438
2439 if (output_resource)
2440 output = wl_resource_get_user_data(output_resource);
2441 else
2442 output = NULL;
2443
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002444 shell_surface_set_parent(shsurf, NULL);
2445
Rafael Antognolli03b16592013-12-03 15:35:42 -02002446 surface_clear_next_states(shsurf);
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05002447 shsurf->next_state.fullscreen = true;
2448 shsurf->state_changed = true;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07002449 set_fullscreen(shsurf, method, framerate, output);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002450}
2451
2452static void
2453set_popup(struct shell_surface *shsurf,
2454 struct weston_surface *parent,
2455 struct weston_seat *seat,
2456 uint32_t serial,
2457 int32_t x,
2458 int32_t y)
2459{
Philip Withnalldc4332f2013-11-25 18:01:38 +00002460 assert(parent != NULL);
2461
Philip Withnallbecb77e2013-11-25 18:01:30 +00002462 shsurf->popup.shseat = get_shell_seat(seat);
2463 shsurf->popup.serial = serial;
2464 shsurf->popup.x = x;
2465 shsurf->popup.y = y;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002466
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002467 shsurf->type = SHELL_SURFACE_POPUP;
Philip Withnallbecb77e2013-11-25 18:01:30 +00002468}
2469
2470static void
2471shell_surface_set_popup(struct wl_client *client,
2472 struct wl_resource *resource,
2473 struct wl_resource *seat_resource,
2474 uint32_t serial,
2475 struct wl_resource *parent_resource,
2476 int32_t x, int32_t y, uint32_t flags)
2477{
2478 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002479 struct weston_surface *parent =
2480 wl_resource_get_user_data(parent_resource);
2481
2482 shell_surface_set_parent(shsurf, parent);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002483
Rafael Antognolli03b16592013-12-03 15:35:42 -02002484 surface_clear_next_states(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002485 set_popup(shsurf,
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002486 parent,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002487 wl_resource_get_user_data(seat_resource),
2488 serial, x, y);
2489}
2490
2491static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002492unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002493{
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002494 /* undo all maximized things here */
2495 shsurf->output = get_default_output(shsurf->surface->compositor);
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002496
2497 if (shsurf->saved_position_valid)
2498 weston_view_set_position(shsurf->view,
2499 shsurf->saved_x, shsurf->saved_y);
2500 else
2501 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002502
2503 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002504 wl_list_insert(&shsurf->view->geometry.transformation_list,
2505 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002506 shsurf->saved_rotation_valid = false;
2507 }
2508
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002509 /* Layer is updated in set_surface_type(). */
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002510}
2511
Philip Withnallbecb77e2013-11-25 18:01:30 +00002512static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002513set_minimized(struct weston_surface *surface, uint32_t is_true)
2514{
2515 struct shell_surface *shsurf;
2516 struct workspace *current_ws;
2517 struct weston_seat *seat;
2518 struct weston_surface *focus;
2519 struct weston_view *view;
2520
2521 view = get_default_view(surface);
2522 if (!view)
2523 return;
2524
2525 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2526
2527 shsurf = get_shell_surface(surface);
2528 current_ws = get_current_workspace(shsurf->shell);
2529
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002530 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002531 /* hide or show, depending on the state */
2532 if (is_true) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002533 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002534
2535 drop_focus_state(shsurf->shell, current_ws, view->surface);
2536 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2537 if (!seat->keyboard)
2538 continue;
2539 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2540 if (focus == view->surface)
2541 weston_keyboard_set_focus(seat->keyboard, NULL);
2542 }
2543 }
2544 else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002545 weston_layer_entry_insert(&current_ws->layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002546
2547 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2548 if (!seat->keyboard)
2549 continue;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002550 activate(shsurf->shell, view->surface, seat, true);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002551 }
2552 }
2553
2554 shell_surface_update_child_surface_layers(shsurf);
2555
2556 weston_view_damage_below(view);
2557}
2558
2559static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002560shell_surface_set_maximized(struct wl_client *client,
2561 struct wl_resource *resource,
2562 struct wl_resource *output_resource)
2563{
2564 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2565 struct weston_output *output;
2566
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002567 surface_clear_next_states(shsurf);
2568 shsurf->next_state.maximized = true;
2569 shsurf->state_changed = true;
2570
2571 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2572 shell_surface_set_parent(shsurf, NULL);
2573
Philip Withnallbecb77e2013-11-25 18:01:30 +00002574 if (output_resource)
2575 output = wl_resource_get_user_data(output_resource);
2576 else
2577 output = NULL;
2578
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002579 shell_surface_set_output(shsurf, output);
Rafael Antognolli4b99a402013-12-03 15:35:44 -02002580
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002581 send_configure_for_surface(shsurf);
Philip Withnallbecb77e2013-11-25 18:01:30 +00002582}
2583
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002584/* This is only ever called from set_surface_type(), so there’s no need to
2585 * update layer_links here, since they’ll be updated when we return. */
Pekka Paalanen98262232011-12-01 10:42:22 +02002586static int
Philip Withnallbecb77e2013-11-25 18:01:30 +00002587reset_surface_type(struct shell_surface *surface)
Pekka Paalanen98262232011-12-01 10:42:22 +02002588{
Rafael Antognolli03b16592013-12-03 15:35:42 -02002589 if (surface->state.fullscreen)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002590 unset_fullscreen(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02002591 if (surface->state.maximized)
Philip Withnallbecb77e2013-11-25 18:01:30 +00002592 unset_maximized(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02002593
Pekka Paalanen98262232011-12-01 10:42:22 +02002594 return 0;
2595}
2596
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002597static void
Rafael Antognolli03b16592013-12-03 15:35:42 -02002598set_full_output(struct shell_surface *shsurf)
2599{
2600 shsurf->saved_x = shsurf->view->geometry.x;
2601 shsurf->saved_y = shsurf->view->geometry.y;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02002602 shsurf->saved_width = shsurf->surface->width;
2603 shsurf->saved_height = shsurf->surface->height;
2604 shsurf->saved_size_valid = true;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002605 shsurf->saved_position_valid = true;
2606
2607 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2608 wl_list_remove(&shsurf->rotation.transform.link);
2609 wl_list_init(&shsurf->rotation.transform.link);
2610 weston_view_geometry_dirty(shsurf->view);
2611 shsurf->saved_rotation_valid = true;
2612 }
2613}
2614
2615static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002616set_surface_type(struct shell_surface *shsurf)
2617{
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002618 struct weston_surface *pes = shsurf->parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002619 struct weston_view *pev = get_default_view(pes);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002620
Philip Withnallbecb77e2013-11-25 18:01:30 +00002621 reset_surface_type(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002622
Rafael Antognolli03b16592013-12-03 15:35:42 -02002623 shsurf->state = shsurf->next_state;
Rafael Antognolli03b16592013-12-03 15:35:42 -02002624 shsurf->state_changed = false;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002625
2626 switch (shsurf->type) {
2627 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02002628 if (shsurf->state.maximized || shsurf->state.fullscreen) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02002629 set_full_output(shsurf);
Rafael Antognollied207b42013-12-03 15:35:43 -02002630 } else if (shsurf->state.relative && pev) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002631 weston_view_set_position(shsurf->view,
2632 pev->geometry.x + shsurf->transient.x,
2633 pev->geometry.y + shsurf->transient.y);
Rafael Antognollied207b42013-12-03 15:35:43 -02002634 }
Rafael Antognolli44a31622013-12-04 18:37:16 -02002635 break;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002636
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002637 case SHELL_SURFACE_XWAYLAND:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002638 weston_view_set_position(shsurf->view, shsurf->transient.x,
2639 shsurf->transient.y);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002640 break;
2641
Philip Withnall0f640e22013-11-25 18:01:31 +00002642 case SHELL_SURFACE_POPUP:
2643 case SHELL_SURFACE_NONE:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002644 default:
2645 break;
2646 }
Philip Withnalle1d75ae2013-11-25 18:01:41 +00002647
2648 /* Update the surface’s layer. */
2649 shell_surface_update_layer(shsurf);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002650}
2651
Tiago Vignattibe143262012-04-16 17:31:41 +03002652static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002653shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002654{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002655 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002656}
2657
Alex Wu21858432012-04-01 20:13:08 +08002658static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002659black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002660
Jason Ekstranda7af7042013-10-12 22:38:11 -05002661static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002662create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08002663 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002664 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002665{
2666 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002667 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002668
2669 surface = weston_surface_create(ec);
2670 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002671 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002672 return NULL;
2673 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002674 view = weston_view_create(surface);
2675 if (surface == NULL) {
2676 weston_log("no memory\n");
2677 weston_surface_destroy(surface);
2678 return NULL;
2679 }
Alex Wu4539b082012-03-01 12:57:46 +08002680
Alex Wu21858432012-04-01 20:13:08 +08002681 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002682 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002683 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002684 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002685 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002686 pixman_region32_fini(&surface->input);
2687 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002688
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002689 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002690 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002691
2692 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002693}
2694
Philip Withnalled8f1a92013-11-25 18:01:43 +00002695static void
2696shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2697{
2698 struct weston_output *output = shsurf->fullscreen_output;
2699
Rafael Antognolli03b16592013-12-03 15:35:42 -02002700 assert(shsurf->state.fullscreen);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002701
2702 if (!shsurf->fullscreen.black_view)
2703 shsurf->fullscreen.black_view =
2704 create_black_surface(shsurf->surface->compositor,
2705 shsurf->surface,
2706 output->x, output->y,
2707 output->width,
2708 output->height);
2709
2710 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002711 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2712 weston_layer_entry_insert(&shsurf->view->layer_link,
2713 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002714 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2715 weston_surface_damage(shsurf->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002716
2717 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002718}
2719
Alex Wu4539b082012-03-01 12:57:46 +08002720/* Create black surface and append it to the associated fullscreen surface.
2721 * Handle size dismatch and positioning according to the method. */
2722static void
2723shell_configure_fullscreen(struct shell_surface *shsurf)
2724{
2725 struct weston_output *output = shsurf->fullscreen_output;
2726 struct weston_surface *surface = shsurf->surface;
2727 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002728 float scale, output_aspect, surface_aspect, x, y;
Giulio Camuffob8366642013-04-25 13:57:46 +03002729 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002730
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002731 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2732 restore_output_mode(output);
2733
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002734 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002735 weston_layer_entry_remove(&shsurf->view->layer_link);
2736 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002737
Philip Withnalled8f1a92013-11-25 18:01:43 +00002738 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002739
Jason Ekstranda7af7042013-10-12 22:38:11 -05002740 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002741 &surf_width, &surf_height);
2742
Alex Wu4539b082012-03-01 12:57:46 +08002743 switch (shsurf->fullscreen.type) {
2744 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02002745 if (surface->buffer_ref.buffer)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002746 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002747 break;
2748 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00002749 /* 1:1 mapping between surface and output dimensions */
Giulio Camuffob8366642013-04-25 13:57:46 +03002750 if (output->width == surf_width &&
2751 output->height == surf_height) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002752 weston_view_set_position(shsurf->view,
2753 output->x - surf_x,
2754 output->y - surf_y);
Rob Bradford9f3dd152013-02-12 11:53:47 +00002755 break;
2756 }
2757
Alex Wu4539b082012-03-01 12:57:46 +08002758 matrix = &shsurf->fullscreen.transform.matrix;
2759 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002760
Scott Moreau1bad5db2012-08-18 01:04:05 -06002761 output_aspect = (float) output->width /
2762 (float) output->height;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002763 /* XXX: Use surf_width and surf_height here? */
2764 surface_aspect = (float) surface->width /
2765 (float) surface->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002766 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002767 scale = (float) output->width /
Giulio Camuffob8366642013-04-25 13:57:46 +03002768 (float) surf_width;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002769 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06002770 scale = (float) output->height /
Giulio Camuffob8366642013-04-25 13:57:46 +03002771 (float) surf_height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002772
Alex Wu4539b082012-03-01 12:57:46 +08002773 weston_matrix_scale(matrix, scale, scale, 1);
2774 wl_list_remove(&shsurf->fullscreen.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002775 wl_list_insert(&shsurf->view->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08002776 &shsurf->fullscreen.transform.link);
Giulio Camuffob8366642013-04-25 13:57:46 +03002777 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2778 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002779 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04002780
Alex Wu4539b082012-03-01 12:57:46 +08002781 break;
2782 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08002783 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002784 struct weston_mode mode = {0,
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002785 surf_width * surface->buffer_viewport.buffer.scale,
2786 surf_height * surface->buffer_viewport.buffer.scale,
Alex Wubd3354b2012-04-17 17:20:49 +08002787 shsurf->fullscreen.framerate};
2788
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002789 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
Hardening57388e42013-09-18 23:56:36 +02002790 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002791 weston_view_set_position(shsurf->view,
2792 output->x - surf_x,
2793 output->y - surf_y);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002794 shsurf->fullscreen.black_view->surface->width = output->width;
2795 shsurf->fullscreen.black_view->surface->height = output->height;
2796 weston_view_set_position(shsurf->fullscreen.black_view,
2797 output->x - surf_x,
2798 output->y - surf_y);
Alex Wubd3354b2012-04-17 17:20:49 +08002799 break;
Alexander Larssond622ed32013-05-28 16:23:40 +02002800 } else {
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02002801 restore_output_mode(output);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002802 center_on_output(shsurf->view, output);
Alexander Larssond622ed32013-05-28 16:23:40 +02002803 }
Alex Wubd3354b2012-04-17 17:20:49 +08002804 }
Alex Wu4539b082012-03-01 12:57:46 +08002805 break;
2806 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
Jason Ekstranda7af7042013-10-12 22:38:11 -05002807 center_on_output(shsurf->view, output);
Alex Wu4539b082012-03-01 12:57:46 +08002808 break;
2809 default:
2810 break;
2811 }
2812}
2813
Alex Wu4539b082012-03-01 12:57:46 +08002814static void
2815shell_map_fullscreen(struct shell_surface *shsurf)
2816{
Alex Wubd3354b2012-04-17 17:20:49 +08002817 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002818}
2819
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002820static void
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002821set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2822{
2823 /* XXX: using the same fields for transient type */
Rafael Antognolli03b16592013-12-03 15:35:42 -02002824 surface_clear_next_states(shsurf);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002825 shsurf->transient.x = x;
2826 shsurf->transient.y = y;
2827 shsurf->transient.flags = flags;
Philip Withnalldc4332f2013-11-25 18:01:38 +00002828
2829 shell_surface_set_parent(shsurf, NULL);
2830
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08002831 shsurf->type = SHELL_SURFACE_XWAYLAND;
Kristian Høgsberg8bc525c2014-01-01 22:34:49 -08002832 shsurf->state_changed = true;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002833}
2834
Kristian Høgsberg47792412014-05-04 13:47:06 -07002835static void
2836shell_interface_set_fullscreen(struct shell_surface *shsurf,
2837 uint32_t method,
2838 uint32_t framerate,
2839 struct weston_output *output)
2840{
2841 surface_clear_next_states(shsurf);
2842 set_fullscreen(shsurf, method, framerate, output);
2843
2844 shsurf->next_state.fullscreen = true;
2845 shsurf->state_changed = true;
2846}
2847
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07002848static int
2849shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2850{
2851 return surface_move(shsurf, ws, 1);
2852}
2853
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002854static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002855
2856static void
2857destroy_shell_seat(struct wl_listener *listener, void *data)
2858{
2859 struct shell_seat *shseat =
2860 container_of(listener,
2861 struct shell_seat, seat_destroy_listener);
2862 struct shell_surface *shsurf, *prev = NULL;
2863
2864 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002865 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002866 shseat->popup_grab.client = NULL;
2867
2868 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2869 shsurf->popup.shseat = NULL;
2870 if (prev) {
2871 wl_list_init(&prev->popup.grab_link);
2872 }
2873 prev = shsurf;
2874 }
2875 wl_list_init(&prev->popup.grab_link);
2876 }
2877
2878 wl_list_remove(&shseat->seat_destroy_listener.link);
2879 free(shseat);
2880}
2881
Jason Ekstrand024177c2014-04-21 19:42:58 -05002882static void
2883shell_seat_caps_changed(struct wl_listener *listener, void *data)
2884{
2885 struct shell_seat *seat;
2886
2887 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2888
2889 if (seat->seat->keyboard &&
2890 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2891 wl_signal_add(&seat->seat->keyboard->focus_signal,
2892 &seat->keyboard_focus_listener);
2893 } else if (!seat->seat->keyboard) {
2894 wl_list_init(&seat->keyboard_focus_listener.link);
2895 }
2896
2897 if (seat->seat->pointer &&
2898 wl_list_empty(&seat->pointer_focus_listener.link)) {
2899 wl_signal_add(&seat->seat->pointer->focus_signal,
2900 &seat->pointer_focus_listener);
2901 } else if (!seat->seat->pointer) {
2902 wl_list_init(&seat->pointer_focus_listener.link);
2903 }
2904}
2905
Giulio Camuffo5085a752013-03-25 21:42:45 +01002906static struct shell_seat *
2907create_shell_seat(struct weston_seat *seat)
2908{
2909 struct shell_seat *shseat;
2910
2911 shseat = calloc(1, sizeof *shseat);
2912 if (!shseat) {
2913 weston_log("no memory to allocate shell seat\n");
2914 return NULL;
2915 }
2916
2917 shseat->seat = seat;
2918 wl_list_init(&shseat->popup_grab.surfaces_list);
2919
2920 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2921 wl_signal_add(&seat->destroy_signal,
2922 &shseat->seat_destroy_listener);
2923
Jason Ekstrand024177c2014-04-21 19:42:58 -05002924 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2925 wl_list_init(&shseat->keyboard_focus_listener.link);
2926
2927 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2928 wl_list_init(&shseat->pointer_focus_listener.link);
2929
2930 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2931 wl_signal_add(&seat->updated_caps_signal,
2932 &shseat->caps_changed_listener);
2933 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2934
Giulio Camuffo5085a752013-03-25 21:42:45 +01002935 return shseat;
2936}
2937
2938static struct shell_seat *
2939get_shell_seat(struct weston_seat *seat)
2940{
2941 struct wl_listener *listener;
2942
2943 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002944 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002945
2946 return container_of(listener,
2947 struct shell_seat, seat_destroy_listener);
2948}
2949
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002950static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002951popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002952{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002953 struct weston_pointer *pointer = grab->pointer;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002954 struct weston_view *view;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002955 struct shell_seat *shseat =
2956 container_of(grab, struct shell_seat, popup_grab.grab);
2957 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04002958 wl_fixed_t sx, sy;
2959
Jason Ekstranda7af7042013-10-12 22:38:11 -05002960 view = weston_compositor_pick_view(pointer->seat->compositor,
2961 pointer->x, pointer->y,
2962 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002963
Jason Ekstranda7af7042013-10-12 22:38:11 -05002964 if (view && view->surface->resource &&
2965 wl_resource_get_client(view->surface->resource) == client) {
2966 weston_pointer_set_focus(pointer, view, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002967 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002968 weston_pointer_set_focus(pointer, NULL,
2969 wl_fixed_from_int(0),
2970 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002971 }
2972}
2973
2974static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002975popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2976 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002977{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002978 struct weston_pointer *pointer = grab->pointer;
Neil Roberts96d790e2013-09-19 17:32:00 +01002979 struct wl_resource *resource;
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002980 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002981
Giulio Camuffo1959ab82013-11-14 23:42:52 +01002982 weston_pointer_move(pointer, x, y);
2983
Neil Roberts96d790e2013-09-19 17:32:00 +01002984 wl_resource_for_each(resource, &pointer->focus_resource_list) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002985 weston_view_from_global_fixed(pointer->focus,
2986 pointer->x, pointer->y,
2987 &sx, &sy);
Neil Roberts96d790e2013-09-19 17:32:00 +01002988 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002989 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002990}
2991
2992static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002993popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002994 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002995{
2996 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002997 struct shell_seat *shseat =
2998 container_of(grab, struct shell_seat, popup_grab.grab);
Rob Bradford880ebc72013-07-22 17:31:38 +01002999 struct wl_display *display = shseat->seat->compositor->wl_display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003000 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003001 uint32_t serial;
Neil Roberts96d790e2013-09-19 17:32:00 +01003002 struct wl_list *resource_list;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003003
Neil Roberts96d790e2013-09-19 17:32:00 +01003004 resource_list = &grab->pointer->focus_resource_list;
3005 if (!wl_list_empty(resource_list)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003006 serial = wl_display_get_serial(display);
Neil Roberts96d790e2013-09-19 17:32:00 +01003007 wl_resource_for_each(resource, resource_list) {
3008 wl_pointer_send_button(resource, serial,
3009 time, button, state);
3010 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01003011 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01003012 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04003013 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003014 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003015 }
3016
Daniel Stone4dbadb12012-05-30 16:31:51 +01003017 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01003018 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003019}
3020
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003021static void
3022popup_grab_cancel(struct weston_pointer_grab *grab)
3023{
3024 popup_grab_end(grab->pointer);
3025}
3026
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003027static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003028 popup_grab_focus,
3029 popup_grab_motion,
3030 popup_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003031 popup_grab_cancel,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003032};
3033
3034static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003035shell_surface_send_popup_done(struct shell_surface *shsurf)
3036{
3037 if (shell_surface_is_wl_shell_surface(shsurf))
3038 wl_shell_surface_send_popup_done(shsurf->resource);
3039 else if (shell_surface_is_xdg_popup(shsurf))
3040 xdg_popup_send_popup_done(shsurf->resource,
3041 shsurf->popup.serial);
3042}
3043
3044static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003045popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003046{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003047 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003048 struct shell_seat *shseat =
3049 container_of(grab, struct shell_seat, popup_grab.grab);
3050 struct shell_surface *shsurf;
3051 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003052
3053 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003054 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003055 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003056 shseat->popup_grab.grab.interface = NULL;
3057 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01003058 /* Send the popup_done event to all the popups open */
3059 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003060 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003061 shsurf->popup.shseat = NULL;
3062 if (prev) {
3063 wl_list_init(&prev->popup.grab_link);
3064 }
3065 prev = shsurf;
3066 }
3067 wl_list_init(&prev->popup.grab_link);
3068 wl_list_init(&shseat->popup_grab.surfaces_list);
3069 }
3070}
3071
3072static void
3073add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
3074{
Kristian Høgsberge3148752013-05-06 23:19:49 -04003075 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003076
3077 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003078 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003079 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003080 /* We must make sure here that this popup was opened after
3081 * a mouse press, and not just by moving around with other
3082 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04003083 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01003084 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01003085
Rob Bradforddfe31052013-06-26 19:49:11 +01003086 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003087 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Rob Bradforddfe31052013-06-26 19:49:11 +01003088 } else {
3089 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003090 }
Giulio Camuffo5085a752013-03-25 21:42:45 +01003091}
3092
3093static void
3094remove_popup_grab(struct shell_surface *shsurf)
3095{
3096 struct shell_seat *shseat = shsurf->popup.shseat;
3097
3098 wl_list_remove(&shsurf->popup.grab_link);
3099 wl_list_init(&shsurf->popup.grab_link);
3100 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003101 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02003102 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04003103 }
3104}
3105
3106static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003107shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003108{
Giulio Camuffo5085a752013-03-25 21:42:45 +01003109 struct shell_seat *shseat = shsurf->popup.shseat;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003110 struct weston_view *parent_view = get_default_view(shsurf->parent);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003111
Jason Ekstranda7af7042013-10-12 22:38:11 -05003112 shsurf->surface->output = parent_view->output;
3113 shsurf->view->output = parent_view->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003114
Jason Ekstranda7af7042013-10-12 22:38:11 -05003115 weston_view_set_transform_parent(shsurf->view, parent_view);
3116 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3117 weston_view_update_transform(shsurf->view);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003118
Kristian Høgsberge3148752013-05-06 23:19:49 -04003119 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01003120 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003121 } else {
Rafael Antognollie2a34552013-12-03 15:35:45 -02003122 shell_surface_send_popup_done(shsurf);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003123 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003124 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003125}
3126
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003127static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003128 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003129 shell_surface_move,
3130 shell_surface_resize,
3131 shell_surface_set_toplevel,
3132 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003133 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08003134 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04003135 shell_surface_set_maximized,
3136 shell_surface_set_title,
3137 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003138};
3139
3140static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003141destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003142{
Kristian Høgsberg9046d242014-01-10 00:25:30 -08003143 struct shell_surface *child, *next;
3144
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003145 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3146
Giulio Camuffo5085a752013-03-25 21:42:45 +01003147 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3148 remove_popup_grab(shsurf);
3149 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003150
Alex Wubd3354b2012-04-17 17:20:49 +08003151 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02003152 shell_surface_is_top_fullscreen(shsurf))
3153 restore_output_mode (shsurf->fullscreen_output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003154
Jason Ekstranda7af7042013-10-12 22:38:11 -05003155 if (shsurf->fullscreen.black_view)
3156 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
Alex Wuaa08e2d2012-03-05 11:01:40 +08003157
Alex Wubd3354b2012-04-17 17:20:49 +08003158 /* As destroy_resource() use wl_list_for_each_safe(),
3159 * we can always remove the listener.
3160 */
3161 wl_list_remove(&shsurf->surface_destroy_listener.link);
3162 shsurf->surface->configure = NULL;
Scott Moreau976a0502013-03-07 10:15:17 -07003163 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08003164
Jason Ekstranda7af7042013-10-12 22:38:11 -05003165 weston_view_destroy(shsurf->view);
3166
Philip Withnall648a4dd2013-11-25 18:01:44 +00003167 wl_list_remove(&shsurf->children_link);
Emilio Pozuelo Monfortadaa20c2014-01-28 13:54:16 +01003168 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3169 shell_surface_set_parent(child, NULL);
Philip Withnall648a4dd2013-11-25 18:01:44 +00003170
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003171 wl_list_remove(&shsurf->link);
3172 free(shsurf);
3173}
3174
3175static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03003176shell_destroy_shell_surface(struct wl_resource *resource)
3177{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003178 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03003179
Bryan Caina46b9462014-04-04 17:41:24 -05003180 if (!wl_list_empty(&shsurf->popup.grab_link))
3181 remove_popup_grab(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003182 shsurf->resource = NULL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003183}
3184
3185static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003186shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003187{
3188 struct shell_surface *shsurf = container_of(listener,
3189 struct shell_surface,
3190 surface_destroy_listener);
3191
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003192 if (shsurf->resource)
3193 wl_resource_destroy(shsurf->resource);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003194
3195 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003196}
3197
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003198static void
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003199fade_out_done(struct weston_view_animation *animation, void *data)
3200{
3201 struct shell_surface *shsurf = data;
3202
3203 weston_surface_destroy(shsurf->surface);
3204}
3205
3206static void
3207handle_resource_destroy(struct wl_listener *listener, void *data)
3208{
3209 struct shell_surface *shsurf =
3210 container_of(listener, struct shell_surface,
3211 resource_destroy_listener);
3212
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003213 if (!weston_surface_is_mapped(shsurf->surface))
3214 return;
3215
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003216 shsurf->surface->ref_count++;
3217
3218 pixman_region32_fini(&shsurf->surface->pending.input);
3219 pixman_region32_init(&shsurf->surface->pending.input);
3220 pixman_region32_fini(&shsurf->surface->input);
3221 pixman_region32_init(&shsurf->surface->input);
Ander Conselvan de Oliveira15f9a262014-04-29 17:54:02 +03003222 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3223 fade_out_done, shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003224}
3225
3226static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003227shell_surface_configure(struct weston_surface *, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003228
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003229struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003230get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003231{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003232 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003233 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04003234 else
3235 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02003236}
3237
Rafael Antognollie2a34552013-12-03 15:35:45 -02003238static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003239create_common_surface(struct shell_client *owner, void *shell,
3240 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003241 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003242{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003243 struct shell_surface *shsurf;
3244
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003245 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02003246 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003247 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003248 }
3249
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003250 shsurf = calloc(1, sizeof *shsurf);
3251 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02003252 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003253 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003254 }
3255
Jason Ekstranda7af7042013-10-12 22:38:11 -05003256 shsurf->view = weston_view_create(surface);
3257 if (!shsurf->view) {
3258 weston_log("no memory to allocate shell surface\n");
3259 free(shsurf);
3260 return NULL;
3261 }
3262
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003263 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003264 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003265
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003266 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3267 wl_resource_add_destroy_listener(surface->resource,
3268 &shsurf->resource_destroy_listener);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003269 shsurf->owner = owner;
Kristian Høgsberg160fe752014-03-11 10:19:10 -07003270
Tiago Vignattibc052c92012-04-19 16:18:18 +03003271 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06003272 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08003273 shsurf->saved_position_valid = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003274 shsurf->saved_size_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08003275 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003276 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08003277 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3278 shsurf->fullscreen.framerate = 0;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003279 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08003280 wl_list_init(&shsurf->fullscreen.transform.link);
3281
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003282 shsurf->output = get_default_output(shsurf->shell->compositor);
3283
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003284 wl_signal_init(&shsurf->destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003285 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003286 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003287 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003288
3289 /* init link so its safe to always remove it in destroy_shell_surface */
3290 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01003291 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003292
Pekka Paalanen460099f2012-01-20 16:48:25 +02003293 /* empty when not in use */
3294 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003295 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003296
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003297 wl_list_init(&shsurf->workspace_transform.link);
3298
Philip Withnall648a4dd2013-11-25 18:01:44 +00003299 wl_list_init(&shsurf->children_link);
3300 wl_list_init(&shsurf->children_list);
3301 shsurf->parent = NULL;
3302
Pekka Paalanen98262232011-12-01 10:42:22 +02003303 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003304
Kristian Høgsberga61ca062012-05-22 16:05:52 -04003305 shsurf->client = client;
3306
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04003307 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003308}
3309
Rafael Antognollie2a34552013-12-03 15:35:45 -02003310static struct shell_surface *
3311create_shell_surface(void *shell, struct weston_surface *surface,
3312 const struct weston_shell_client *client)
3313{
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003314 return create_common_surface(NULL, shell, surface, client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003315}
3316
Jason Ekstranda7af7042013-10-12 22:38:11 -05003317static struct weston_view *
3318get_primary_view(void *shell, struct shell_surface *shsurf)
3319{
3320 return shsurf->view;
3321}
3322
Tiago Vignattibc052c92012-04-19 16:18:18 +03003323static void
3324shell_get_shell_surface(struct wl_client *client,
3325 struct wl_resource *resource,
3326 uint32_t id,
3327 struct wl_resource *surface_resource)
3328{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003329 struct weston_surface *surface =
3330 wl_resource_get_user_data(surface_resource);
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003331 struct shell_client *sc = wl_resource_get_user_data(resource);
3332 struct desktop_shell *shell = sc->shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003333 struct shell_surface *shsurf;
3334
3335 if (get_shell_surface(surface)) {
3336 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003337 WL_DISPLAY_ERROR_INVALID_OBJECT,
3338 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03003339 return;
3340 }
3341
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003342 shsurf = create_common_surface(sc, shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04003343 if (!shsurf) {
3344 wl_resource_post_error(surface_resource,
3345 WL_DISPLAY_ERROR_INVALID_OBJECT,
3346 "surface->configure already set");
3347 return;
3348 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03003349
Jason Ekstranda85118c2013-06-27 20:17:02 -05003350 shsurf->resource =
3351 wl_resource_create(client,
3352 &wl_shell_surface_interface, 1, id);
3353 wl_resource_set_implementation(shsurf->resource,
3354 &shell_surface_implementation,
3355 shsurf, shell_destroy_shell_surface);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003356}
3357
Rafael Antognollie2a34552013-12-03 15:35:45 -02003358static bool
3359shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3360{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003361 /* A shell surface without a resource is created from xwayland
3362 * and is considered a wl_shell surface for now. */
3363
3364 return shsurf->resource == NULL ||
3365 wl_resource_instance_of(shsurf->resource,
3366 &wl_shell_surface_interface,
3367 &shell_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003368}
3369
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003370static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02003371 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003372};
3373
Rafael Antognollie2a34552013-12-03 15:35:45 -02003374/****************************
3375 * xdg-shell implementation */
3376
3377static void
3378xdg_surface_destroy(struct wl_client *client,
3379 struct wl_resource *resource)
3380{
3381 wl_resource_destroy(resource);
3382}
3383
3384static void
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003385xdg_surface_set_parent(struct wl_client *client,
3386 struct wl_resource *resource,
3387 struct wl_resource *parent_resource)
Jasper St. Pierre63a9c332014-02-01 18:35:15 -05003388{
3389 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3390 struct weston_surface *parent;
3391
3392 if (parent_resource)
3393 parent = wl_resource_get_user_data(parent_resource);
3394 else
3395 parent = NULL;
3396
3397 shell_surface_set_parent(shsurf, parent);
3398}
3399
3400static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003401xdg_surface_set_app_id(struct wl_client *client,
3402 struct wl_resource *resource,
3403 const char *app_id)
3404{
3405 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3406
3407 free(shsurf->class);
3408 shsurf->class = strdup(app_id);
3409}
3410
3411static void
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003412xdg_surface_show_window_menu(struct wl_client *client,
3413 struct wl_resource *surface_resource,
3414 struct wl_resource *seat_resource,
3415 uint32_t serial,
3416 int32_t x,
3417 int32_t y)
3418{
3419 /* TODO */
3420}
3421
3422static void
Rafael Antognollie2a34552013-12-03 15:35:45 -02003423xdg_surface_set_title(struct wl_client *client,
3424 struct wl_resource *resource, const char *title)
3425{
3426 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3427
3428 set_title(shsurf, title);
3429}
3430
3431static void
3432xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3433 struct wl_resource *seat_resource, uint32_t serial)
3434{
3435 common_surface_move(resource, seat_resource, serial);
3436}
3437
3438static void
3439xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3440 struct wl_resource *seat_resource, uint32_t serial,
3441 uint32_t edges)
3442{
3443 common_surface_resize(resource, seat_resource, serial, edges);
3444}
3445
3446static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003447xdg_surface_ack_configure(struct wl_client *client,
3448 struct wl_resource *resource,
3449 uint32_t serial)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003450{
3451 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3452
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -05003453 if (shsurf->state_requested) {
3454 shsurf->next_state = shsurf->requested_state;
3455 shsurf->state_changed = true;
3456 shsurf->state_requested = false;
Rafael Antognolli3c4e3f72013-12-03 15:35:46 -02003457 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02003458}
3459
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003460static void
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003461xdg_surface_set_window_geometry(struct wl_client *client,
3462 struct wl_resource *resource,
3463 int32_t x,
3464 int32_t y,
3465 int32_t width,
3466 int32_t height)
3467{
3468 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3469
3470 set_window_geometry(shsurf, x, y, width, height);
3471}
3472
3473static void
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003474xdg_surface_set_maximized(struct wl_client *client,
3475 struct wl_resource *resource)
3476{
3477 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3478
3479 shsurf->state_requested = true;
3480 shsurf->requested_state.maximized = true;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003481 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003482}
3483
3484static void
3485xdg_surface_unset_maximized(struct wl_client *client,
3486 struct wl_resource *resource)
3487{
3488 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3489
3490 shsurf->state_requested = true;
3491 shsurf->requested_state.maximized = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003492 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003493}
3494
3495static void
3496xdg_surface_set_fullscreen(struct wl_client *client,
3497 struct wl_resource *resource,
3498 struct wl_resource *output_resource)
3499{
3500 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3501 struct weston_output *output;
3502
3503 shsurf->state_requested = true;
3504 shsurf->requested_state.fullscreen = true;
3505
3506 if (output_resource)
3507 output = wl_resource_get_user_data(output_resource);
3508 else
3509 output = NULL;
3510
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003511 shell_surface_set_output(shsurf, output);
3512 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003513
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07003514 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003515}
3516
3517static void
3518xdg_surface_unset_fullscreen(struct wl_client *client,
3519 struct wl_resource *resource)
3520{
3521 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3522
3523 shsurf->state_requested = true;
3524 shsurf->requested_state.fullscreen = false;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -07003525 send_configure_for_surface(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003526}
3527
3528static void
Manuel Bachmann50c87db2014-02-26 15:52:13 +01003529xdg_surface_set_minimized(struct wl_client *client,
3530 struct wl_resource *resource)
3531{
3532 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3533
3534 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3535 return;
3536
3537 /* apply compositor's own minimization logic (hide) */
3538 set_minimized(shsurf->surface, 1);
3539}
3540
Rafael Antognollie2a34552013-12-03 15:35:45 -02003541static const struct xdg_surface_interface xdg_surface_implementation = {
3542 xdg_surface_destroy,
Jasper St. Pierrec815d622014-04-10 18:37:54 -07003543 xdg_surface_set_parent,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003544 xdg_surface_set_title,
3545 xdg_surface_set_app_id,
Jasper St. Pierre81ff0752014-03-13 11:04:53 -04003546 xdg_surface_show_window_menu,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003547 xdg_surface_move,
3548 xdg_surface_resize,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003549 xdg_surface_ack_configure,
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04003550 xdg_surface_set_window_geometry,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003551 xdg_surface_set_maximized,
3552 xdg_surface_unset_maximized,
3553 xdg_surface_set_fullscreen,
3554 xdg_surface_unset_fullscreen,
3555 xdg_surface_set_minimized,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003556};
3557
3558static void
3559xdg_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003560 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003561{
3562 struct shell_surface *shsurf = get_shell_surface(surface);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003563 uint32_t *s;
3564 struct wl_array states;
3565 uint32_t serial;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003566
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08003567 assert(shsurf);
3568
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003569 if (!shsurf->resource)
3570 return;
3571
3572 wl_array_init(&states);
3573 if (shsurf->requested_state.fullscreen) {
3574 s = wl_array_add(&states, sizeof *s);
3575 *s = XDG_SURFACE_STATE_FULLSCREEN;
3576 } else if (shsurf->requested_state.maximized) {
3577 s = wl_array_add(&states, sizeof *s);
3578 *s = XDG_SURFACE_STATE_MAXIMIZED;
3579 }
Jasper St. Pierre5befdda2014-05-06 08:50:47 -04003580 if (shsurf->resize_edges != 0) {
3581 s = wl_array_add(&states, sizeof *s);
3582 *s = XDG_SURFACE_STATE_RESIZING;
3583 }
Jasper St. Pierre973d7872014-05-06 08:44:29 -04003584 if (shsurf->focus_count > 0) {
3585 s = wl_array_add(&states, sizeof *s);
3586 *s = XDG_SURFACE_STATE_ACTIVATED;
3587 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07003588
3589 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3590 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3591
3592 wl_array_release(&states);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003593}
3594
3595static const struct weston_shell_client xdg_client = {
3596 xdg_send_configure
3597};
3598
3599static void
3600xdg_use_unstable_version(struct wl_client *client,
3601 struct wl_resource *resource,
3602 int32_t version)
3603{
3604 if (version > 1) {
3605 wl_resource_post_error(resource,
3606 1,
3607 "xdg-shell:: version not implemented yet.");
3608 return;
3609 }
3610}
3611
3612static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003613create_xdg_surface(struct shell_client *owner, void *shell,
3614 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003615 const struct weston_shell_client *client)
3616{
3617 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003618
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003619 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003620 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003621
3622 return shsurf;
3623}
3624
3625static void
3626xdg_get_xdg_surface(struct wl_client *client,
3627 struct wl_resource *resource,
3628 uint32_t id,
3629 struct wl_resource *surface_resource)
3630{
3631 struct weston_surface *surface =
3632 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003633 struct shell_client *sc = wl_resource_get_user_data(resource);
3634 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003635 struct shell_surface *shsurf;
3636
3637 if (get_shell_surface(surface)) {
3638 wl_resource_post_error(surface_resource,
3639 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003640 "xdg_shell::get_xdg_surface already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003641 return;
3642 }
3643
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003644 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003645 if (!shsurf) {
3646 wl_resource_post_error(surface_resource,
3647 WL_DISPLAY_ERROR_INVALID_OBJECT,
3648 "surface->configure already set");
3649 return;
3650 }
3651
3652 shsurf->resource =
3653 wl_resource_create(client,
3654 &xdg_surface_interface, 1, id);
3655 wl_resource_set_implementation(shsurf->resource,
3656 &xdg_surface_implementation,
3657 shsurf, shell_destroy_shell_surface);
3658}
3659
3660static bool
3661shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3662{
Kristian Høgsberg0b7d9952014-02-03 15:50:38 -08003663 return shsurf->resource &&
3664 wl_resource_instance_of(shsurf->resource,
3665 &xdg_surface_interface,
3666 &xdg_surface_implementation);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003667}
3668
3669/* xdg-popup implementation */
3670
3671static void
3672xdg_popup_destroy(struct wl_client *client,
3673 struct wl_resource *resource)
3674{
3675 wl_resource_destroy(resource);
3676}
3677
Rafael Antognollie2a34552013-12-03 15:35:45 -02003678static const struct xdg_popup_interface xdg_popup_implementation = {
3679 xdg_popup_destroy,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003680};
3681
3682static void
3683xdg_popup_send_configure(struct weston_surface *surface,
Jasper St. Pierreac985be2014-04-28 11:19:28 -04003684 int32_t width, int32_t height)
Rafael Antognollie2a34552013-12-03 15:35:45 -02003685{
3686}
3687
3688static const struct weston_shell_client xdg_popup_client = {
3689 xdg_popup_send_configure
3690};
3691
3692static struct shell_surface *
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003693create_xdg_popup(struct shell_client *owner, void *shell,
3694 struct weston_surface *surface,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003695 const struct weston_shell_client *client,
3696 struct weston_surface *parent,
3697 struct shell_seat *seat,
3698 uint32_t serial,
3699 int32_t x, int32_t y)
3700{
3701 struct shell_surface *shsurf;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003702
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003703 shsurf = create_common_surface(owner, shell, surface, client);
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08003704 shsurf->type = SHELL_SURFACE_POPUP;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003705 shsurf->popup.shseat = seat;
3706 shsurf->popup.serial = serial;
3707 shsurf->popup.x = x;
3708 shsurf->popup.y = y;
3709 shell_surface_set_parent(shsurf, parent);
3710
3711 return shsurf;
3712}
3713
3714static void
3715xdg_get_xdg_popup(struct wl_client *client,
3716 struct wl_resource *resource,
3717 uint32_t id,
3718 struct wl_resource *surface_resource,
3719 struct wl_resource *parent_resource,
3720 struct wl_resource *seat_resource,
3721 uint32_t serial,
3722 int32_t x, int32_t y, uint32_t flags)
3723{
3724 struct weston_surface *surface =
3725 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003726 struct shell_client *sc = wl_resource_get_user_data(resource);
3727 struct desktop_shell *shell = sc->shell;
Rafael Antognollie2a34552013-12-03 15:35:45 -02003728 struct shell_surface *shsurf;
3729 struct weston_surface *parent;
3730 struct shell_seat *seat;
3731
3732 if (get_shell_surface(surface)) {
3733 wl_resource_post_error(surface_resource,
3734 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jasper St. Pierrefe9671e2014-03-25 13:31:44 -04003735 "xdg_shell::get_xdg_popup already requested");
Rafael Antognollie2a34552013-12-03 15:35:45 -02003736 return;
3737 }
3738
3739 if (!parent_resource) {
3740 wl_resource_post_error(surface_resource,
3741 WL_DISPLAY_ERROR_INVALID_OBJECT,
3742 "xdg_shell::get_xdg_popup requires a parent shell surface");
3743 }
3744
3745 parent = wl_resource_get_user_data(parent_resource);
3746 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3747
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05003748 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
Rafael Antognollie2a34552013-12-03 15:35:45 -02003749 parent, seat, serial, x, y);
3750 if (!shsurf) {
3751 wl_resource_post_error(surface_resource,
3752 WL_DISPLAY_ERROR_INVALID_OBJECT,
3753 "surface->configure already set");
3754 return;
3755 }
3756
3757 shsurf->resource =
3758 wl_resource_create(client,
3759 &xdg_popup_interface, 1, id);
3760 wl_resource_set_implementation(shsurf->resource,
3761 &xdg_popup_implementation,
3762 shsurf, shell_destroy_shell_surface);
3763}
3764
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003765static void
3766xdg_pong(struct wl_client *client,
3767 struct wl_resource *resource, uint32_t serial)
3768{
3769 struct shell_client *sc = wl_resource_get_user_data(resource);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003770
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08003771 shell_client_pong(sc, serial);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003772}
3773
Rafael Antognollie2a34552013-12-03 15:35:45 -02003774static bool
3775shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3776{
3777 return wl_resource_instance_of(shsurf->resource,
3778 &xdg_popup_interface,
3779 &xdg_popup_implementation);
3780}
3781
3782static const struct xdg_shell_interface xdg_implementation = {
3783 xdg_use_unstable_version,
3784 xdg_get_xdg_surface,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003785 xdg_get_xdg_popup,
3786 xdg_pong
Rafael Antognollie2a34552013-12-03 15:35:45 -02003787};
3788
3789static int
3790xdg_shell_unversioned_dispatch(const void *implementation,
3791 void *_target, uint32_t opcode,
3792 const struct wl_message *message,
3793 union wl_argument *args)
3794{
3795 struct wl_resource *resource = _target;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003796 struct shell_client *sc = wl_resource_get_user_data(resource);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003797
3798 if (opcode != 0) {
3799 wl_resource_post_error(resource,
3800 WL_DISPLAY_ERROR_INVALID_OBJECT,
3801 "must call use_unstable_version first");
3802 return 0;
3803 }
3804
Kristian Høgsbergc7680b02014-02-19 10:14:46 -08003805#define XDG_SERVER_VERSION 3
Rafael Antognollie2a34552013-12-03 15:35:45 -02003806
Kristian Høgsberg8d344a02013-12-08 22:27:11 -08003807 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3808 "shell implementation doesn't match protocol version");
3809
Rafael Antognollie2a34552013-12-03 15:35:45 -02003810 if (args[0].i != XDG_SERVER_VERSION) {
3811 wl_resource_post_error(resource,
3812 WL_DISPLAY_ERROR_INVALID_OBJECT,
3813 "incompatible version, server is %d "
3814 "client wants %d",
3815 XDG_SERVER_VERSION, args[0].i);
3816 return 0;
3817 }
3818
3819 wl_resource_set_implementation(resource, &xdg_implementation,
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08003820 sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02003821
3822 return 1;
3823}
3824
3825/* end of xdg-shell implementation */
3826/***********************************/
3827
Kristian Høgsberg07937562011-04-12 17:25:42 -04003828static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003829shell_fade(struct desktop_shell *shell, enum fade_type type);
3830
3831static int
3832screensaver_timeout(void *data)
3833{
3834 struct desktop_shell *shell = data;
3835
3836 shell_fade(shell, FADE_OUT);
3837
3838 return 1;
3839}
3840
3841static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003842handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02003843{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003844 struct desktop_shell *shell =
3845 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003846
Pekka Paalanen18027e52011-12-02 16:31:49 +02003847 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02003848
3849 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02003850 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02003851}
3852
3853static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003854launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003855{
3856 if (shell->screensaver.binding)
3857 return;
3858
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003859 if (!shell->screensaver.path) {
3860 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003861 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02003862 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003863
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003864 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003865 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05003866 return;
3867 }
3868
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003869 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003870 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003871 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02003872 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003873}
3874
3875static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003876terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003877{
Pekka Paalanen18027e52011-12-02 16:31:49 +02003878 if (shell->screensaver.process.pid == 0)
3879 return;
3880
Ander Conselvan de Oliveira4e20e9b2014-04-10 14:49:35 +03003881 /* Disarm the screensaver timer, otherwise it may fire when the
3882 * compositor is not in the idle state. In that case, the screen will
3883 * be locked, but the wake_signal won't fire on user input, making the
3884 * system unresponsive. */
3885 wl_event_source_timer_update(shell->screensaver.timer, 0);
3886
Pekka Paalanen18027e52011-12-02 16:31:49 +02003887 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003888}
3889
3890static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003891configure_static_view(struct weston_view *ev, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003892{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003893 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003894
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003895 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003896 if (v->output == ev->output && v != ev) {
3897 weston_view_unmap(v);
3898 v->surface->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003899 }
3900 }
3901
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003902 weston_view_set_position(ev, ev->output->x, ev->output->y);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003903
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003904 if (wl_list_empty(&ev->layer_link.link)) {
3905 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003906 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003907 }
3908}
3909
3910static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003911background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003912{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003913 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003914 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003915
Jason Ekstranda7af7042013-10-12 22:38:11 -05003916 view = container_of(es->views.next, struct weston_view, surface_link);
3917
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003918 configure_static_view(view, &shell->background_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003919}
3920
3921static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003922desktop_shell_set_background(struct wl_client *client,
3923 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003924 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003925 struct wl_resource *surface_resource)
3926{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003927 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003928 struct weston_surface *surface =
3929 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003930 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003931
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003932 if (surface->configure) {
3933 wl_resource_post_error(surface_resource,
3934 WL_DISPLAY_ERROR_INVALID_OBJECT,
3935 "surface role already assigned");
3936 return;
3937 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003938
Jason Ekstranda7af7042013-10-12 22:38:11 -05003939 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3940 weston_view_destroy(view);
3941 view = weston_view_create(surface);
3942
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003943 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003944 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003945 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003946 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003947 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003948 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003949 surface->output->width,
3950 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003951}
3952
3953static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003954panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003955{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003956 struct desktop_shell *shell = es->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003957 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003958
Jason Ekstranda7af7042013-10-12 22:38:11 -05003959 view = container_of(es->views.next, struct weston_view, surface_link);
3960
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003961 configure_static_view(view, &shell->panel_layer);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003962}
3963
3964static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003965desktop_shell_set_panel(struct wl_client *client,
3966 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003967 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003968 struct wl_resource *surface_resource)
3969{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003970 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003971 struct weston_surface *surface =
3972 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003973 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003974
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003975 if (surface->configure) {
3976 wl_resource_post_error(surface_resource,
3977 WL_DISPLAY_ERROR_INVALID_OBJECT,
3978 "surface role already assigned");
3979 return;
3980 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003981
Jason Ekstranda7af7042013-10-12 22:38:11 -05003982 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3983 weston_view_destroy(view);
3984 view = weston_view_create(surface);
3985
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003986 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003987 surface->configure_private = shell;
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003988 surface->output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003989 view->output = surface->output;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003990 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003991 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003992 surface->output->width,
3993 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003994}
3995
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003996static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003997lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003998{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003999 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004000 struct weston_view *view;
4001
4002 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004003
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004004 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01004005 return;
4006
Jason Ekstranda7af7042013-10-12 22:38:11 -05004007 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004008
4009 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004010 weston_layer_entry_insert(&shell->lock_layer.view_list,
4011 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004012 weston_view_update_transform(view);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004013 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004014 }
4015}
4016
4017static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004018handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004019{
Tiago Vignattibe143262012-04-16 17:31:41 +03004020 struct desktop_shell *shell =
4021 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004022
Martin Minarik6d118362012-06-07 18:01:59 +02004023 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004024 shell->lock_surface = NULL;
4025}
4026
4027static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004028desktop_shell_set_lock_surface(struct wl_client *client,
4029 struct wl_resource *resource,
4030 struct wl_resource *surface_resource)
4031{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004032 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004033 struct weston_surface *surface =
4034 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02004035
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004036 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004037
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004038 if (!shell->locked)
4039 return;
4040
Pekka Paalanen98262232011-12-01 10:42:22 +02004041 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004042
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004043 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004044 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04004045 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02004046
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07004047 weston_view_create(surface);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04004048 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004049 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004050}
4051
4052static void
Tiago Vignattibe143262012-04-16 17:31:41 +03004053resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004054{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004055 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004056
Pekka Paalanen77346a62011-11-30 16:26:35 +02004057 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004058
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004059 wl_list_remove(&shell->lock_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004060 if (shell->showing_input_panels) {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004061 wl_list_insert(&shell->compositor->cursor_layer.link,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004062 &shell->input_panel_layer.link);
4063 wl_list_insert(&shell->input_panel_layer.link,
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004064 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004065 } else {
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004066 wl_list_insert(&shell->compositor->cursor_layer.link,
4067 &shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004068 }
Manuel Bachmann805d2f52014-03-05 12:21:34 +01004069 wl_list_insert(&shell->fullscreen_layer.link,
4070 &shell->panel_layer.link);
4071 wl_list_insert(&shell->panel_layer.link,
4072 &ws->layer.link),
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004073
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004074 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02004075
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004076 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004077 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02004078 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004079}
4080
4081static void
4082desktop_shell_unlock(struct wl_client *client,
4083 struct wl_resource *resource)
4084{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004085 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004086
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004087 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004088
4089 if (shell->locked)
4090 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004091}
4092
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004093static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004094desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004095 struct wl_resource *resource,
4096 struct wl_resource *surface_resource)
4097{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004098 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004099
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004100 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07004101 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004102}
4103
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004104static void
4105desktop_shell_desktop_ready(struct wl_client *client,
4106 struct wl_resource *resource)
4107{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004108 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004109
4110 shell_fade_startup(shell);
4111}
4112
Kristian Høgsberg75840622011-09-06 13:48:16 -04004113static const struct desktop_shell_interface desktop_shell_implementation = {
4114 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004115 desktop_shell_set_panel,
4116 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004117 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004118 desktop_shell_set_grab_surface,
4119 desktop_shell_desktop_ready
Kristian Høgsberg75840622011-09-06 13:48:16 -04004120};
4121
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004122static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004123get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004124{
4125 struct shell_surface *shsurf;
4126
4127 shsurf = get_shell_surface(surface);
4128 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02004129 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004130 return shsurf->type;
4131}
4132
Kristian Høgsberg75840622011-09-06 13:48:16 -04004133static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004134move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004135{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004136 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004137 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004138 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004139
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004140 if (seat->pointer->focus == NULL)
4141 return;
4142
4143 focus = seat->pointer->focus->surface;
4144
Pekka Paalanen01388e22013-04-25 13:57:44 +03004145 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004146 if (surface == NULL)
4147 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004148
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004149 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004150 if (shsurf == NULL || shsurf->state.fullscreen ||
4151 shsurf->state.maximized)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004152 return;
4153
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07004154 surface_move(shsurf, (struct weston_seat *) seat, 0);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004155}
4156
4157static void
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004158maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4159{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004160 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004161 struct weston_surface *surface;
4162 struct shell_surface *shsurf;
4163
4164 surface = weston_surface_get_main_surface(focus);
4165 if (surface == NULL)
4166 return;
4167
4168 shsurf = get_shell_surface(surface);
4169 if (shsurf == NULL)
4170 return;
4171
4172 if (!shell_surface_is_xdg_surface(shsurf))
4173 return;
4174
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004175 shsurf->state_requested = true;
4176 shsurf->requested_state.maximized = !shsurf->state.maximized;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004177 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004178}
4179
4180static void
4181fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4182{
Emilio Pozuelo Monfort38b58eb2014-01-29 11:11:12 +01004183 struct weston_surface *focus = seat->keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004184 struct weston_surface *surface;
4185 struct shell_surface *shsurf;
4186
4187 surface = weston_surface_get_main_surface(focus);
4188 if (surface == NULL)
4189 return;
4190
4191 shsurf = get_shell_surface(surface);
4192 if (shsurf == NULL)
4193 return;
4194
4195 if (!shell_surface_is_xdg_surface(shsurf))
4196 return;
4197
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07004198 shsurf->state_requested = true;
4199 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
Boyan Ding32abdbb2014-06-26 10:19:32 +08004200 shsurf->fullscreen_output = shsurf->output;
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07004201 send_configure_for_surface(shsurf);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02004202}
4203
4204static void
Neil Robertsaba0f252013-10-03 16:43:05 +01004205touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4206{
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004207 struct weston_surface *focus = seat->touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01004208 struct weston_surface *surface;
4209 struct shell_surface *shsurf;
4210
4211 surface = weston_surface_get_main_surface(focus);
4212 if (surface == NULL)
4213 return;
4214
4215 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004216 if (shsurf == NULL || shsurf->state.fullscreen ||
4217 shsurf->state.maximized)
Neil Robertsaba0f252013-10-03 16:43:05 +01004218 return;
4219
4220 surface_touch_move(shsurf, (struct weston_seat *) seat);
4221}
4222
4223static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004224resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004225{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004226 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004227 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004228 uint32_t edges = 0;
4229 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004230 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05004231
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004232 if (seat->pointer->focus == NULL)
4233 return;
4234
4235 focus = seat->pointer->focus->surface;
4236
Pekka Paalanen01388e22013-04-25 13:57:44 +03004237 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01004238 if (surface == NULL)
4239 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004240
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004241 shsurf = get_shell_surface(surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004242 if (shsurf == NULL || shsurf->state.fullscreen ||
4243 shsurf->state.maximized)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02004244 return;
4245
Jason Ekstranda7af7042013-10-12 22:38:11 -05004246 weston_view_from_global(shsurf->view,
4247 wl_fixed_to_int(seat->pointer->grab_x),
4248 wl_fixed_to_int(seat->pointer->grab_y),
4249 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004250
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004251 if (x < shsurf->surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004252 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004253 else if (x < 2 * shsurf->surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004254 edges |= 0;
4255 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004256 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004257
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004258 if (y < shsurf->surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004259 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004260 else if (y < 2 * shsurf->surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04004261 edges |= 0;
4262 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004263 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04004264
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004265 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004266}
4267
4268static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004269surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004270 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004271{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004272 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004273 struct shell_surface *shsurf;
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004274 struct weston_surface *focus = seat->pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004275 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004276
Pekka Paalanen01388e22013-04-25 13:57:44 +03004277 /* XXX: broken for windows containing sub-surfaces */
4278 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004279 if (surface == NULL)
4280 return;
4281
4282 shsurf = get_shell_surface(surface);
4283 if (!shsurf)
4284 return;
4285
Jason Ekstranda7af7042013-10-12 22:38:11 -05004286 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004287
Jason Ekstranda7af7042013-10-12 22:38:11 -05004288 if (shsurf->view->alpha > 1.0)
4289 shsurf->view->alpha = 1.0;
4290 if (shsurf->view->alpha < step)
4291 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004292
Jason Ekstranda7af7042013-10-12 22:38:11 -05004293 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06004294 weston_surface_damage(surface);
4295}
4296
4297static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004298do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004299 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004300{
Daniel Stone37816df2012-05-16 18:45:18 +01004301 struct weston_seat *ws = (struct weston_seat *) seat;
4302 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004303 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06004304 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004305
4306 wl_list_for_each(output, &compositor->output_list, link) {
4307 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01004308 wl_fixed_to_double(seat->pointer->x),
4309 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01004310 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01004311 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004312 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004313 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004314 increment = -output->zoom.increment;
4315 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004316 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004317 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02004318 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004319 else
4320 increment = 0;
4321
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04004322 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07004323
Scott Moreaue6603982012-06-11 13:07:51 -06004324 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06004325 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06004326 else if (output->zoom.level > output->zoom.max_level)
4327 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02004328 else if (!output->zoom.active) {
Giulio Camuffo412b0242013-11-14 23:42:51 +01004329 weston_output_activate_zoom(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04004330 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07004331
Scott Moreaue6603982012-06-11 13:07:51 -06004332 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004333
Jason Ekstranda7af7042013-10-12 22:38:11 -05004334 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004335 }
4336 }
4337}
4338
Scott Moreauccbf29d2012-02-22 14:21:41 -07004339static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004340zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01004341 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01004342{
4343 do_zoom(seat, time, 0, axis, value);
4344}
4345
4346static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004347zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004348 void *data)
4349{
4350 do_zoom(seat, time, key, 0, 0);
4351}
4352
4353static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004354terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004355 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004356{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004357 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004358
Daniel Stone325fc2d2012-05-30 16:31:58 +01004359 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004360}
4361
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01004362static void
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004363rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4364 wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004365{
4366 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004367 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004368 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004369 struct shell_surface *shsurf = rotate->base.shsurf;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004370 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004371
Giulio Camuffo1959ab82013-11-14 23:42:52 +01004372 weston_pointer_move(pointer, x, y);
4373
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004374 if (!shsurf)
4375 return;
4376
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004377 cx = 0.5f * shsurf->surface->width;
4378 cy = 0.5f * shsurf->surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004379
Daniel Stone37816df2012-05-16 18:45:18 +01004380 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4381 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004382 r = sqrtf(dx * dx + dy * dy);
4383
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004384 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004385 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004386
4387 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004388 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004389 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004390
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004391 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004392 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004393
4394 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004395 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004396 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004397 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02004398 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004399
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02004400 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05004401 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004402 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004403 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004404 wl_list_init(&shsurf->rotation.transform.link);
4405 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004406 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004407 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004408
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004409 /* We need to adjust the position of the surface
4410 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05004411 cposx = shsurf->view->geometry.x + cx;
4412 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004413 dposx = rotate->center.x - cposx;
4414 dposy = rotate->center.y - cposy;
4415 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004416 weston_view_set_position(shsurf->view,
4417 shsurf->view->geometry.x + dposx,
4418 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004419 }
4420
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02004421 /* Repaint implies weston_surface_update_transform(), which
4422 * lazily applies the damage due to rotation update.
4423 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004424 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004425}
4426
4427static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004428rotate_grab_button(struct weston_pointer_grab *grab,
4429 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004430{
4431 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004432 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004433 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004434 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01004435 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004436
Daniel Stone4dbadb12012-05-30 16:31:51 +01004437 if (pointer->button_count == 0 &&
4438 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03004439 if (shsurf)
4440 weston_matrix_multiply(&shsurf->rotation.rotation,
4441 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004442 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004443 free(rotate);
4444 }
4445}
4446
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004447static void
4448rotate_grab_cancel(struct weston_pointer_grab *grab)
4449{
4450 struct rotate_grab *rotate =
4451 container_of(grab, struct rotate_grab, base.grab);
4452
4453 shell_grab_end(&rotate->base);
4454 free(rotate);
4455}
4456
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04004457static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02004458 noop_grab_focus,
4459 rotate_grab_motion,
4460 rotate_grab_button,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004461 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02004462};
4463
4464static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004465surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02004466{
Pekka Paalanen460099f2012-01-20 16:48:25 +02004467 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02004468 float dx, dy;
4469 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02004470
Pekka Paalanen460099f2012-01-20 16:48:25 +02004471 rotate = malloc(sizeof *rotate);
4472 if (!rotate)
4473 return;
4474
Jason Ekstranda7af7042013-10-12 22:38:11 -05004475 weston_view_to_global_float(surface->view,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004476 surface->surface->width * 0.5f,
4477 surface->surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004478 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004479
Daniel Stone37816df2012-05-16 18:45:18 +01004480 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4481 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004482 r = sqrtf(dx * dx + dy * dy);
4483 if (r > 20.0f) {
4484 struct weston_matrix inverse;
4485
4486 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004487 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004488 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01004489
4490 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03004491 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05004492 } else {
4493 weston_matrix_init(&surface->rotation.rotation);
4494 weston_matrix_init(&rotate->rotation);
4495 }
4496
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03004497 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4498 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02004499}
4500
4501static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004502rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004503 void *data)
4504{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004505 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03004506 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004507 struct shell_surface *surface;
4508
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01004509 if (seat->pointer->focus == NULL)
4510 return;
4511
4512 focus = seat->pointer->focus->surface;
4513
Pekka Paalanen01388e22013-04-25 13:57:44 +03004514 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004515 if (base_surface == NULL)
4516 return;
4517
4518 surface = get_shell_surface(base_surface);
Rafael Antognolli03b16592013-12-03 15:35:42 -02004519 if (surface == NULL || surface->state.fullscreen ||
4520 surface->state.maximized)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05004521 return;
4522
4523 surface_rotate(surface, seat);
4524}
4525
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004526/* Move all fullscreen layers down to the current workspace and hide their
4527 * black views. The surfaces' state is set to both fullscreen and lowered,
4528 * and this is reversed when such a surface is re-configured, see
4529 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4530 *
4531 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004532 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004533void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004534lower_fullscreen_layer(struct desktop_shell *shell)
4535{
4536 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004537 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004538
4539 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004540 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004541 &shell->fullscreen_layer.view_list.link,
4542 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004543 struct shell_surface *shsurf = get_shell_surface(view->surface);
4544
4545 if (!shsurf)
4546 continue;
4547
4548 /* We can have a non-fullscreen popup for a fullscreen surface
4549 * in the fullscreen layer. */
4550 if (shsurf->state.fullscreen) {
4551 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004552 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
4553 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07004554 weston_view_damage_below(shsurf->fullscreen.black_view);
4555
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004556 }
4557
4558 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004559 weston_layer_entry_remove(&view->layer_link);
4560 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004561 weston_view_damage_below(view);
4562 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004563
4564 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004565 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004566}
4567
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08004568void
Tiago Vignattibe143262012-04-16 17:31:41 +03004569activate(struct desktop_shell *shell, struct weston_surface *es,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004570 struct weston_seat *seat, bool configure)
Kristian Høgsberg75840622011-09-06 13:48:16 -04004571{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004572 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004573 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02004574 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004575 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02004576 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004577
Kristian Høgsberg6110d072014-04-29 15:15:45 -07004578 lower_fullscreen_layer(shell);
4579
Pekka Paalanen01388e22013-04-25 13:57:44 +03004580 main_surface = weston_surface_get_main_surface(es);
4581
Daniel Stone37816df2012-05-16 18:45:18 +01004582 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004583
Jonas Ådahl8538b222012-08-29 22:13:03 +02004584 state = ensure_focus_state(shell, seat);
4585 if (state == NULL)
4586 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004587
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004588 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08004589 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04004590
Rafael Antognolli03b16592013-12-03 15:35:42 -02004591 shsurf = get_shell_surface(main_surface);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08004592 assert(shsurf);
4593
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004594 if (shsurf->state.fullscreen && configure)
Rafael Antognolli03b16592013-12-03 15:35:42 -02004595 shell_configure_fullscreen(shsurf);
4596 else
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02004597 restore_all_output_modes(shell->compositor);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004598
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01004599 /* Update the surface’s layer. This brings it to the top of the stacking
4600 * order as appropriate. */
4601 shell_surface_update_layer(shsurf);
4602
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004603 if (shell->focus_animation_type != ANIMATION_NONE) {
4604 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004605 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00004606 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04004607}
4608
Alex Wu21858432012-04-01 20:13:08 +08004609/* no-op func for checking black surface */
4610static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004611black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08004612{
4613}
4614
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01004615static bool
Alex Wu21858432012-04-01 20:13:08 +08004616is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4617{
4618 if (es->configure == black_surface_configure) {
4619 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01004620 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08004621 return true;
4622 }
4623 return false;
4624}
4625
Kristian Høgsberg75840622011-09-06 13:48:16 -04004626static void
Neil Robertsa28c6932013-10-03 16:43:04 +01004627activate_binding(struct weston_seat *seat,
4628 struct desktop_shell *shell,
4629 struct weston_surface *focus)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004630{
Pekka Paalanen01388e22013-04-25 13:57:44 +03004631 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004632
Alex Wu9c35e6b2012-03-05 14:13:13 +08004633 if (!focus)
4634 return;
4635
Pekka Paalanen01388e22013-04-25 13:57:44 +03004636 if (is_black_surface(focus, &main_surface))
4637 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08004638
Pekka Paalanen01388e22013-04-25 13:57:44 +03004639 main_surface = weston_surface_get_main_surface(focus);
4640 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04004641 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04004642
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01004643 activate(shell, focus, seat, true);
Neil Robertsa28c6932013-10-03 16:43:04 +01004644}
4645
4646static void
4647click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4648 void *data)
4649{
4650 if (seat->pointer->grab != &seat->pointer->default_grab)
4651 return;
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08004652 if (seat->pointer->focus == NULL)
4653 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004654
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004655 activate_binding(seat, data, seat->pointer->focus->surface);
Neil Robertsa28c6932013-10-03 16:43:04 +01004656}
4657
4658static void
4659touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4660{
4661 if (seat->touch->grab != &seat->touch->default_grab)
4662 return;
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08004663 if (seat->touch->focus == NULL)
4664 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01004665
Kristian Høgsberg7ab139c2013-10-24 16:21:39 -07004666 activate_binding(seat, data, seat->touch->focus->surface);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05004667}
4668
4669static void
Neil Robertsb4a91702014-04-09 16:33:32 +01004670unfocus_all_seats(struct desktop_shell *shell)
4671{
4672 struct weston_seat *seat, *next;
4673
4674 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4675 if (seat->keyboard == NULL)
4676 continue;
4677
4678 weston_keyboard_set_focus(seat->keyboard, NULL);
4679 }
4680}
4681
4682static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004683lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004684{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004685 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004686
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004687 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004688 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004689 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004690 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004691
4692 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004693
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004694 /* Hide all surfaces by removing the fullscreen, panel and
4695 * toplevel layers. This way nothing else can show or receive
4696 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004697
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004698 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004699 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004700 if (shell->showing_input_panels)
4701 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004702 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004703 wl_list_insert(&shell->compositor->cursor_layer.link,
4704 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004705
Pekka Paalanen77346a62011-11-30 16:26:35 +02004706 launch_screensaver(shell);
4707
Neil Robertsb4a91702014-04-09 16:33:32 +01004708 /* Remove the keyboard focus on all seats. This will be
4709 * restored to the workspace's saved state via
4710 * restore_focus_state when the compositor is unlocked */
4711 unfocus_all_seats(shell);
4712
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004713 /* TODO: disable bindings that should not work while locked. */
4714
4715 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004716}
4717
4718static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004719unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004720{
Pekka Paalanend81c2162011-11-16 13:47:34 +02004721 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004722 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004723 return;
4724 }
4725
4726 /* If desktop-shell client has gone away, unlock immediately. */
4727 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004728 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004729 return;
4730 }
4731
4732 if (shell->prepare_event_sent)
4733 return;
4734
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004735 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004736 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004737}
4738
4739static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004740shell_fade_done(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004741{
4742 struct desktop_shell *shell = data;
4743
4744 shell->fade.animation = NULL;
4745
4746 switch (shell->fade.type) {
4747 case FADE_IN:
Jason Ekstranda7af7042013-10-12 22:38:11 -05004748 weston_surface_destroy(shell->fade.view->surface);
4749 shell->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004750 break;
4751 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004752 lock(shell);
4753 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004754 default:
4755 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004756 }
4757}
4758
Jason Ekstranda7af7042013-10-12 22:38:11 -05004759static struct weston_view *
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004760shell_fade_create_surface(struct desktop_shell *shell)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004761{
4762 struct weston_compositor *compositor = shell->compositor;
4763 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004764 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004765
4766 surface = weston_surface_create(compositor);
4767 if (!surface)
4768 return NULL;
4769
Jason Ekstranda7af7042013-10-12 22:38:11 -05004770 view = weston_view_create(surface);
4771 if (!view) {
4772 weston_surface_destroy(surface);
4773 return NULL;
4774 }
4775
Jason Ekstrand5c11a332013-12-04 20:32:03 -06004776 weston_surface_set_size(surface, 8192, 8192);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004777 weston_view_set_position(view, 0, 0);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004778 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004779 weston_layer_entry_insert(&compositor->fade_layer.view_list,
4780 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004781 pixman_region32_init(&surface->input);
4782
Jason Ekstranda7af7042013-10-12 22:38:11 -05004783 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004784}
4785
4786static void
4787shell_fade(struct desktop_shell *shell, enum fade_type type)
4788{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004789 float tint;
4790
4791 switch (type) {
4792 case FADE_IN:
4793 tint = 0.0;
4794 break;
4795 case FADE_OUT:
4796 tint = 1.0;
4797 break;
4798 default:
4799 weston_log("shell: invalid fade type\n");
4800 return;
4801 }
4802
4803 shell->fade.type = type;
4804
Jason Ekstranda7af7042013-10-12 22:38:11 -05004805 if (shell->fade.view == NULL) {
4806 shell->fade.view = shell_fade_create_surface(shell);
4807 if (!shell->fade.view)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004808 return;
4809
Jason Ekstranda7af7042013-10-12 22:38:11 -05004810 shell->fade.view->alpha = 1.0 - tint;
4811 weston_view_update_transform(shell->fade.view);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004812 }
4813
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004814 if (shell->fade.view->output == NULL) {
4815 /* If the black view gets a NULL output, we lost the
4816 * last output and we'll just cancel the fade. This
4817 * happens when you close the last window under the
4818 * X11 or Wayland backends. */
4819 shell->locked = false;
4820 weston_surface_destroy(shell->fade.view->surface);
4821 shell->fade.view = NULL;
4822 } else if (shell->fade.animation) {
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004823 weston_fade_update(shell->fade.animation, tint);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004824 } else {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004825 shell->fade.animation =
Jason Ekstranda7af7042013-10-12 22:38:11 -05004826 weston_fade_run(shell->fade.view,
Kristian Høgsberg5281fb12013-06-17 10:10:28 -04004827 1.0 - tint, tint, 300.0,
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004828 shell_fade_done, shell);
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004829 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004830}
4831
4832static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004833do_shell_fade_startup(void *data)
4834{
4835 struct desktop_shell *shell = data;
4836
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004837 if (shell->startup_animation_type == ANIMATION_FADE)
4838 shell_fade(shell, FADE_IN);
4839 else if (shell->startup_animation_type == ANIMATION_NONE) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004840 weston_surface_destroy(shell->fade.view->surface);
4841 shell->fade.view = NULL;
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004842 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004843}
4844
4845static void
4846shell_fade_startup(struct desktop_shell *shell)
4847{
4848 struct wl_event_loop *loop;
4849
4850 if (!shell->fade.startup_timer)
4851 return;
4852
4853 wl_event_source_remove(shell->fade.startup_timer);
4854 shell->fade.startup_timer = NULL;
4855
4856 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4857 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4858}
4859
4860static int
4861fade_startup_timeout(void *data)
4862{
4863 struct desktop_shell *shell = data;
4864
4865 shell_fade_startup(shell);
4866 return 0;
4867}
4868
4869static void
4870shell_fade_init(struct desktop_shell *shell)
4871{
4872 /* Make compositor output all black, and wait for the desktop-shell
4873 * client to signal it is ready, then fade in. The timer triggers a
4874 * fade-in, in case the desktop-shell client takes too long.
4875 */
4876
4877 struct wl_event_loop *loop;
4878
Jason Ekstranda7af7042013-10-12 22:38:11 -05004879 if (shell->fade.view != NULL) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004880 weston_log("%s: warning: fade surface already exists\n",
4881 __func__);
4882 return;
4883 }
4884
Jason Ekstranda7af7042013-10-12 22:38:11 -05004885 shell->fade.view = shell_fade_create_surface(shell);
4886 if (!shell->fade.view)
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004887 return;
4888
Jason Ekstranda7af7042013-10-12 22:38:11 -05004889 weston_view_update_transform(shell->fade.view);
4890 weston_surface_damage(shell->fade.view->surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004891
4892 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4893 shell->fade.startup_timer =
4894 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4895 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4896}
4897
4898static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004899idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004900{
4901 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004902 container_of(listener, struct desktop_shell, idle_listener);
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004903 struct weston_seat *seat;
4904
4905 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4906 if (seat->pointer)
4907 popup_grab_end(seat->pointer);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004908
4909 shell_fade(shell, FADE_OUT);
4910 /* lock() is called from shell_fade_done() */
4911}
4912
4913static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004914wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004915{
4916 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004917 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004918
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004919 unlock(shell);
4920}
4921
4922static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004923center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004924{
Giulio Camuffob8366642013-04-25 13:57:46 +03004925 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004926 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004927
Jason Ekstranda7af7042013-10-12 22:38:11 -05004928 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004929
4930 x = output->x + (output->width - width) / 2 - surf_x / 2;
4931 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004932
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004933 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004934}
4935
4936static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004937weston_view_set_initial_position(struct weston_view *view,
4938 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004939{
4940 struct weston_compositor *compositor = shell->compositor;
4941 int ix = 0, iy = 0;
4942 int range_x, range_y;
4943 int dx, dy, x, y, panel_height;
4944 struct weston_output *output, *target_output = NULL;
4945 struct weston_seat *seat;
4946
4947 /* As a heuristic place the new window on the same output as the
4948 * pointer. Falling back to the output containing 0, 0.
4949 *
4950 * TODO: Do something clever for touch too?
4951 */
4952 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04004953 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04004954 ix = wl_fixed_to_int(seat->pointer->x);
4955 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004956 break;
4957 }
4958 }
4959
4960 wl_list_for_each(output, &compositor->output_list, link) {
4961 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4962 target_output = output;
4963 break;
4964 }
4965 }
4966
4967 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004968 weston_view_set_position(view, 10 + random() % 400,
4969 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004970 return;
4971 }
4972
4973 /* Valid range within output where the surface will still be onscreen.
4974 * If this is negative it means that the surface is bigger than
4975 * output.
4976 */
4977 panel_height = get_output_panel_height(shell, target_output);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004978 range_x = target_output->width - view->surface->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004979 range_y = (target_output->height - panel_height) -
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004980 view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004981
Rob Bradford4cb88c72012-08-13 15:18:44 +01004982 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004983 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004984 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01004985 dx = 0;
4986
4987 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004988 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004989 else
4990 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004991
4992 x = target_output->x + dx;
4993 y = target_output->y + dy;
4994
Jason Ekstranda7af7042013-10-12 22:38:11 -05004995 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004996}
4997
4998static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004999map(struct desktop_shell *shell, struct shell_surface *shsurf,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005000 int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005001{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005002 struct weston_compositor *compositor = shell->compositor;
Daniel Stoneb2104682012-05-30 16:31:56 +01005003 struct weston_seat *seat;
Juan Zhao96879df2012-02-07 08:45:41 +08005004 int panel_height = 0;
Giulio Camuffob8366642013-04-25 13:57:46 +03005005 int32_t surf_x, surf_y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02005006
Pekka Paalanen77346a62011-11-30 16:26:35 +02005007 /* initial positioning, see also configure() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05005008 switch (shsurf->type) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005009 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognolli03b16592013-12-03 15:35:42 -02005010 if (shsurf->state.fullscreen) {
5011 center_on_output(shsurf->view, shsurf->fullscreen_output);
5012 shell_map_fullscreen(shsurf);
5013 } else if (shsurf->state.maximized) {
5014 /* use surface configure to set the geometry */
5015 panel_height = get_output_panel_height(shell, shsurf->output);
5016 surface_subsurfaces_boundingbox(shsurf->surface,
5017 &surf_x, &surf_y, NULL, NULL);
5018 weston_view_set_position(shsurf->view,
5019 shsurf->output->x - surf_x,
5020 shsurf->output->y +
5021 panel_height - surf_y);
Rafael Antognollied207b42013-12-03 15:35:43 -02005022 } else if (!shsurf->state.relative) {
Rafael Antognolli03b16592013-12-03 15:35:42 -02005023 weston_view_set_initial_position(shsurf->view, shell);
5024 }
Juan Zhao96879df2012-02-07 08:45:41 +08005025 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02005026 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04005027 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00005028 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02005029 case SHELL_SURFACE_NONE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005030 weston_view_set_position(shsurf->view,
5031 shsurf->view->geometry.x + sx,
5032 shsurf->view->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02005033 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005034 case SHELL_SURFACE_XWAYLAND:
Pekka Paalanen77346a62011-11-30 16:26:35 +02005035 default:
5036 ;
5037 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04005038
Philip Withnalle1d75ae2013-11-25 18:01:41 +00005039 /* Surface stacking order, see also activate(). */
5040 shell_surface_update_layer(shsurf);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02005041
Jason Ekstranda7af7042013-10-12 22:38:11 -05005042 if (shsurf->type != SHELL_SURFACE_NONE) {
5043 weston_view_update_transform(shsurf->view);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005044 if (shsurf->state.maximized) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005045 shsurf->surface->output = shsurf->output;
5046 shsurf->view->output = shsurf->output;
5047 }
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02005048 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05005049
Jason Ekstranda7af7042013-10-12 22:38:11 -05005050 switch (shsurf->type) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005051 /* XXX: xwayland's using the same fields for transient type */
5052 case SHELL_SURFACE_XWAYLAND:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03005053 if (shsurf->transient.flags ==
5054 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5055 break;
5056 case SHELL_SURFACE_TOPLEVEL:
Rafael Antognollied207b42013-12-03 15:35:43 -02005057 if (shsurf->state.relative &&
5058 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5059 break;
Rafael Antognolliba5d2d72013-12-04 17:49:55 -02005060 if (shell->locked)
Rafael Antognollied207b42013-12-03 15:35:43 -02005061 break;
5062 wl_list_for_each(seat, &compositor->seat_list, link)
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005063 activate(shell, shsurf->surface, seat, true);
Juan Zhao7bb92f02011-12-15 11:31:51 -05005064 break;
Philip Withnall0f640e22013-11-25 18:01:31 +00005065 case SHELL_SURFACE_POPUP:
5066 case SHELL_SURFACE_NONE:
Juan Zhao7bb92f02011-12-15 11:31:51 -05005067 default:
5068 break;
5069 }
5070
Rafael Antognolli03b16592013-12-03 15:35:42 -02005071 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5072 !shsurf->state.maximized && !shsurf->state.fullscreen)
Juan Zhaoe10d2792012-04-25 19:09:52 +08005073 {
5074 switch (shell->win_animation_type) {
5075 case ANIMATION_FADE:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005076 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005077 break;
5078 case ANIMATION_ZOOM:
Jason Ekstranda7af7042013-10-12 22:38:11 -05005079 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08005080 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00005081 case ANIMATION_NONE:
Juan Zhaoe10d2792012-04-25 19:09:52 +08005082 default:
5083 break;
5084 }
5085 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005086}
5087
5088static void
Tiago Vignattibe143262012-04-16 17:31:41 +03005089configure(struct desktop_shell *shell, struct weston_surface *surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005090 float x, float y)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005091{
Pekka Paalanen77346a62011-11-30 16:26:35 +02005092 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005093 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005094 int32_t mx, my, surf_x, surf_y;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005095
Pekka Paalanen77346a62011-11-30 16:26:35 +02005096 shsurf = get_shell_surface(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005097
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005098 assert(shsurf);
5099
Rafael Antognolli03b16592013-12-03 15:35:42 -02005100 if (shsurf->state.fullscreen)
Alex Wu4539b082012-03-01 12:57:46 +08005101 shell_configure_fullscreen(shsurf);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005102 else if (shsurf->state.maximized) {
Alex Wu4539b082012-03-01 12:57:46 +08005103 /* setting x, y and using configure to change that geometry */
Giulio Camuffob8366642013-04-25 13:57:46 +03005104 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
5105 NULL, NULL);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005106 mx = shsurf->output->x - surf_x;
5107 my = shsurf->output->y +
5108 get_output_panel_height(shell,shsurf->output) - surf_y;
5109 weston_view_set_position(shsurf->view, mx, my);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005110 } else {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005111 weston_view_set_position(shsurf->view, x, y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04005112 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05005113
Alex Wu4539b082012-03-01 12:57:46 +08005114 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05005115 if (surface->output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05005116 wl_list_for_each(view, &surface->views, surface_link)
5117 weston_view_update_transform(view);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005118
Rafael Antognolli03b16592013-12-03 15:35:42 -02005119 if (shsurf->state.maximized)
Juan Zhao96879df2012-02-07 08:45:41 +08005120 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005121 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04005122}
5123
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005124static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005125shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005126{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03005127 struct shell_surface *shsurf = get_shell_surface(es);
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005128 struct desktop_shell *shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03005129 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005130
U. Artie Eoffcf5737a2014-01-17 10:08:25 -08005131 assert(shsurf);
5132
5133 shell = shsurf->shell;
5134
Kristian Høgsberg8eb0f4f2013-06-17 10:33:14 -04005135 if (!weston_surface_is_mapped(es) &&
5136 !wl_list_empty(&shsurf->popup.grab_link)) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01005137 remove_popup_grab(shsurf);
5138 }
5139
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005140 if (es->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005141 return;
5142
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04005143 if (shsurf->has_next_geometry) {
5144 shsurf->geometry = shsurf->next_geometry;
5145 shsurf->has_next_geometry = false;
5146 shsurf->has_set_geometry = true;
5147 } else if (!shsurf->has_set_geometry) {
5148 surface_subsurfaces_boundingbox(shsurf->surface,
5149 &shsurf->geometry.x,
5150 &shsurf->geometry.y,
5151 &shsurf->geometry.width,
5152 &shsurf->geometry.height);
Jasper St. Pierre851799e2014-05-02 10:14:07 -04005153 }
5154
Kristian Høgsbergc8f8dd82013-12-05 23:20:33 -08005155 if (shsurf->state_changed) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005156 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005157 type_changed = 1;
5158 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04005159
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005160 if (!weston_surface_is_mapped(es)) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005161 map(shell, shsurf, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04005162 } else if (type_changed || sx != 0 || sy != 0 ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005163 shsurf->last_width != es->width ||
5164 shsurf->last_height != es->height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02005165 float from_x, from_y;
5166 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005167
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08005168 if (shsurf->resize_edges) {
5169 sx = 0;
5170 sy = 0;
5171 }
5172
5173 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5174 sx = shsurf->last_width - es->width;
5175 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5176 sy = shsurf->last_height - es->height;
5177
5178 shsurf->last_width = es->width;
5179 shsurf->last_height = es->height;
5180
Jason Ekstranda7af7042013-10-12 22:38:11 -05005181 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5182 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005183 configure(shell, es,
Jason Ekstranda7af7042013-10-12 22:38:11 -05005184 shsurf->view->geometry.x + to_x - from_x,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005185 shsurf->view->geometry.y + to_y - from_y);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03005186 }
5187}
5188
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005189static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005190
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04005191static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005192desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005193{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005194 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03005195 struct desktop_shell *shell =
5196 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005197
5198 shell->child.process.pid = 0;
5199 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005200
5201 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5202 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05005203 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005204 shell->child.deathstamp = time;
5205 shell->child.deathcount = 0;
5206 }
5207
5208 shell->child.deathcount++;
5209 if (shell->child.deathcount > 5) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005210 weston_log("%s died, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005211 return;
5212 }
5213
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005214 weston_log("%s died, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02005215 launch_desktop_shell_process(shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005216 shell_fade_startup(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005217}
5218
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005219static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005220desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5221{
5222 struct desktop_shell *shell;
5223
5224 shell = container_of(listener, struct desktop_shell,
5225 child.client_destroy_listener);
5226
5227 shell->child.client = NULL;
5228}
5229
5230static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005231launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005232{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005233 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005234
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005235 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005236 &shell->child.process,
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005237 shell->client,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02005238 desktop_shell_sigchld);
5239
5240 if (!shell->child.client)
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005241 weston_log("not able to start %s\n", shell->client);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02005242
5243 shell->child.client_destroy_listener.notify =
5244 desktop_shell_client_destroy;
5245 wl_client_add_destroy_listener(shell->child.client,
5246 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005247}
5248
5249static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005250handle_shell_client_destroy(struct wl_listener *listener, void *data)
5251{
5252 struct shell_client *sc =
5253 container_of(listener, struct shell_client, destroy_listener);
5254
5255 if (sc->ping_timer)
5256 wl_event_source_remove(sc->ping_timer);
5257 free(sc);
5258}
5259
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005260static struct shell_client *
5261shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5262 const struct wl_interface *interface, uint32_t id)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005263{
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005264 struct shell_client *sc;
Rafael Antognollie2a34552013-12-03 15:35:45 -02005265
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005266 sc = zalloc(sizeof *sc);
5267 if (sc == NULL) {
5268 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005269 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005270 }
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005271
5272 sc->resource = wl_resource_create(client, interface, 1, id);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005273 if (sc->resource == NULL) {
5274 free(sc);
5275 wl_client_post_no_memory(client);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005276 return NULL;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08005277 }
5278
5279 sc->client = client;
5280 sc->shell = shell;
5281 sc->destroy_listener.notify = handle_shell_client_destroy;
5282 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5283
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005284 return sc;
5285}
5286
5287static void
5288bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5289{
5290 struct desktop_shell *shell = data;
5291 struct shell_client *sc;
5292
5293 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5294 if (sc)
5295 wl_resource_set_implementation(sc->resource,
5296 &shell_implementation,
Jason Ekstrand9e9512f2014-04-16 21:12:10 -05005297 sc, NULL);
Kristian Høgsbergdd62aba2014-02-12 09:56:19 -08005298}
5299
5300static void
5301bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5302{
5303 struct desktop_shell *shell = data;
5304 struct shell_client *sc;
5305
5306 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5307 if (sc)
5308 wl_resource_set_dispatcher(sc->resource,
5309 xdg_shell_unversioned_dispatch,
5310 NULL, sc, NULL);
Rafael Antognollie2a34552013-12-03 15:35:45 -02005311}
5312
5313static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005314unbind_desktop_shell(struct wl_resource *resource)
5315{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005316 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05005317
5318 if (shell->locked)
5319 resume_desktop(shell);
5320
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005321 shell->child.desktop_shell = NULL;
5322 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005323}
5324
5325static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04005326bind_desktop_shell(struct wl_client *client,
5327 void *data, uint32_t version, uint32_t id)
5328{
Tiago Vignattibe143262012-04-16 17:31:41 +03005329 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005330 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04005331
Jason Ekstranda85118c2013-06-27 20:17:02 -05005332 resource = wl_resource_create(client, &desktop_shell_interface,
5333 MIN(version, 2), id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005334
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005335 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005336 wl_resource_set_implementation(resource,
5337 &desktop_shell_implementation,
5338 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005339 shell->child.desktop_shell = resource;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005340
5341 if (version < 2)
5342 shell_fade_startup(shell);
5343
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005344 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02005345 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02005346
5347 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5348 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005349 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04005350}
5351
Pekka Paalanen6e168112011-11-24 11:34:05 +02005352static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005353screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005354{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005355 struct desktop_shell *shell = surface->configure_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005356 struct weston_view *view;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005357 struct weston_layer_entry *prev;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005358
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06005359 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01005360 return;
5361
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02005362 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005363 if (!shell->locked)
5364 return;
5365
Jason Ekstranda7af7042013-10-12 22:38:11 -05005366 view = container_of(surface->views.next, struct weston_view, surface_link);
5367 center_on_output(view, surface->output);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005368
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005369 if (wl_list_empty(&view->layer_link.link)) {
5370 prev = container_of(shell->lock_layer.view_list.link.prev,
5371 struct weston_layer_entry, link);
5372 weston_layer_entry_insert(prev, &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005373 weston_view_update_transform(view);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02005374 wl_event_source_timer_update(shell->screensaver.timer,
5375 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005376 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005377 }
5378}
5379
5380static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02005381screensaver_set_surface(struct wl_client *client,
5382 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005383 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02005384 struct wl_resource *output_resource)
5385{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005386 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05005387 struct weston_surface *surface =
5388 wl_resource_get_user_data(surface_resource);
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05005389 struct weston_output *output = wl_resource_get_user_data(output_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05005390 struct weston_view *view, *next;
5391
5392 /* Make sure we only have one view */
5393 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5394 weston_view_destroy(view);
5395 weston_view_create(surface);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005396
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04005397 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01005398 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02005399 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005400}
5401
5402static const struct screensaver_interface screensaver_implementation = {
5403 screensaver_set_surface
5404};
5405
5406static void
5407unbind_screensaver(struct wl_resource *resource)
5408{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05005409 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005410
Pekka Paalanen77346a62011-11-30 16:26:35 +02005411 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005412}
5413
5414static void
5415bind_screensaver(struct wl_client *client,
5416 void *data, uint32_t version, uint32_t id)
5417{
Tiago Vignattibe143262012-04-16 17:31:41 +03005418 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005419 struct wl_resource *resource;
5420
Jason Ekstranda85118c2013-06-27 20:17:02 -05005421 resource = wl_resource_create(client, &screensaver_interface, 1, id);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005422
Pekka Paalanen77346a62011-11-30 16:26:35 +02005423 if (shell->screensaver.binding == NULL) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05005424 wl_resource_set_implementation(resource,
5425 &screensaver_implementation,
5426 shell, unbind_screensaver);
Pekka Paalanen77346a62011-11-30 16:26:35 +02005427 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02005428 return;
5429 }
5430
5431 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5432 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005433 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02005434}
5435
Kristian Høgsberg07045392012-02-19 18:52:44 -05005436struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03005437 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005438 struct weston_surface *current;
5439 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005440 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005441 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005442};
5443
5444static void
5445switcher_next(struct switcher *switcher)
5446{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005447 struct weston_view *view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005448 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005449 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005450 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005451
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005452 /* temporary re-display minimized surfaces */
5453 struct weston_view *tmp;
5454 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005455 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5456 weston_layer_entry_remove(&view->layer_link);
5457 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005458 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5459 *minimized = view;
5460 }
5461
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005462 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02005463 shsurf = get_shell_surface(view->surface);
Rafael Antognolli5031cbe2013-12-05 19:01:21 -02005464 if (shsurf &&
5465 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5466 shsurf->parent == NULL) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05005467 if (first == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005468 first = view->surface;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005469 if (prev == switcher->current)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005470 next = view->surface;
5471 prev = view->surface;
5472 view->alpha = 0.25;
5473 weston_view_geometry_dirty(view);
5474 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005475 }
Alex Wu1659daa2012-04-01 20:13:09 +08005476
Jason Ekstranda7af7042013-10-12 22:38:11 -05005477 if (is_black_surface(view->surface, NULL)) {
5478 view->alpha = 0.25;
5479 weston_view_geometry_dirty(view);
5480 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08005481 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05005482 }
5483
5484 if (next == NULL)
5485 next = first;
5486
Alex Wu07b26062012-03-12 16:06:01 +08005487 if (next == NULL)
5488 return;
5489
Kristian Høgsberg07045392012-02-19 18:52:44 -05005490 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005491 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005492
5493 switcher->current = next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05005494 wl_list_for_each(view, &next->views, surface_link)
5495 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08005496
Kristian Høgsberg32e56862012-04-02 22:18:58 -04005497 shsurf = get_shell_surface(switcher->current);
Rafael Antognolli03b16592013-12-03 15:35:42 -02005498 if (shsurf && shsurf->state.fullscreen)
Jason Ekstranda7af7042013-10-12 22:38:11 -05005499 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005500}
5501
5502static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005503switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005504{
5505 struct switcher *switcher =
5506 container_of(listener, struct switcher, listener);
5507
5508 switcher_next(switcher);
5509}
5510
5511static void
Daniel Stone351eb612012-05-31 15:27:47 -04005512switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005513{
Jason Ekstranda7af7042013-10-12 22:38:11 -05005514 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005515 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005516 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005517
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005518 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01005519 if (is_focus_view(view))
5520 continue;
5521
Jason Ekstranda7af7042013-10-12 22:38:11 -05005522 view->alpha = 1.0;
5523 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005524 }
5525
Alex Wu07b26062012-03-12 16:06:01 +08005526 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01005527 activate(switcher->shell, switcher->current,
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01005528 (struct weston_seat *) keyboard->seat, true);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005529 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005530 weston_keyboard_end_grab(keyboard);
5531 if (keyboard->input_method_resource)
5532 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005533
5534 /* re-hide surfaces that were temporary shown during the switch */
5535 struct weston_view **minimized;
5536 wl_array_for_each(minimized, &switcher->minimized_array) {
5537 /* with the exception of the current selected */
5538 if ((*minimized)->surface != switcher->current) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005539 weston_layer_entry_remove(&(*minimized)->layer_link);
5540 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005541 weston_view_damage_below(*minimized);
5542 }
5543 }
5544 wl_array_release(&switcher->minimized_array);
5545
Kristian Høgsberg07045392012-02-19 18:52:44 -05005546 free(switcher);
5547}
5548
5549static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005550switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01005551 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005552{
5553 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01005554 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04005555
Daniel Stonec9785ea2012-05-30 16:31:52 +01005556 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04005557 switcher_next(switcher);
5558}
5559
5560static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005561switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04005562 uint32_t mods_depressed, uint32_t mods_latched,
5563 uint32_t mods_locked, uint32_t group)
5564{
5565 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01005566 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005567
Daniel Stone351eb612012-05-31 15:27:47 -04005568 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5569 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04005570}
Kristian Høgsberg07045392012-02-19 18:52:44 -05005571
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005572static void
5573switcher_cancel(struct weston_keyboard_grab *grab)
5574{
5575 struct switcher *switcher = container_of(grab, struct switcher, grab);
5576
5577 switcher_destroy(switcher);
5578}
5579
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005580static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04005581 switcher_key,
5582 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005583 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05005584};
5585
5586static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005587switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005588 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05005589{
Tiago Vignattibe143262012-04-16 17:31:41 +03005590 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005591 struct switcher *switcher;
5592
5593 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005594 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005595 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04005596 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05005597 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01005598 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005599
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02005600 restore_all_output_modes(shell->compositor);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04005601 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005602 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005603 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5604 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05005605 switcher_next(switcher);
5606}
5607
Pekka Paalanen3c647232011-12-22 13:43:43 +02005608static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005609backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005610 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005611{
5612 struct weston_compositor *compositor = data;
5613 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005614 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005615
5616 /* TODO: we're limiting to simple use cases, where we assume just
5617 * control on the primary display. We'd have to extend later if we
5618 * ever get support for setting backlights on random desktop LCD
5619 * panels though */
5620 output = get_default_output(compositor);
5621 if (!output)
5622 return;
5623
5624 if (!output->set_backlight)
5625 return;
5626
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005627 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5628 backlight_new = output->backlight_current - 25;
5629 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5630 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005631
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03005632 if (backlight_new < 5)
5633 backlight_new = 5;
5634 if (backlight_new > 255)
5635 backlight_new = 255;
5636
5637 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02005638 output->set_backlight(output, output->backlight_current);
5639}
5640
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005641struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005642 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005643 struct weston_seat *seat;
5644 uint32_t key[2];
5645 int key_released[2];
5646};
5647
5648static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005649debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005650 uint32_t key, uint32_t state)
5651{
5652 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
Rob Bradford880ebc72013-07-22 17:31:38 +01005653 struct weston_compositor *ec = db->seat->compositor;
5654 struct wl_display *display = ec->wl_display;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005655 struct wl_resource *resource;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005656 uint32_t serial;
5657 int send = 0, terminate = 0;
5658 int check_binding = 1;
5659 int i;
Neil Roberts96d790e2013-09-19 17:32:00 +01005660 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005661
5662 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5663 /* Do not run bindings on key releases */
5664 check_binding = 0;
5665
5666 for (i = 0; i < 2; i++)
5667 if (key == db->key[i])
5668 db->key_released[i] = 1;
5669
5670 if (db->key_released[0] && db->key_released[1]) {
5671 /* All key releases been swalled so end the grab */
5672 terminate = 1;
5673 } else if (key != db->key[0] && key != db->key[1]) {
5674 /* Should not swallow release of other keys */
5675 send = 1;
5676 }
5677 } else if (key == db->key[0] && !db->key_released[0]) {
5678 /* Do not check bindings for the first press of the binding
5679 * key. This allows it to be used as a debug shortcut.
5680 * We still need to swallow this event. */
5681 check_binding = 0;
5682 } else if (db->key[1]) {
5683 /* If we already ran a binding don't process another one since
5684 * we can't keep track of all the binding keys that were
5685 * pressed in order to swallow the release events. */
5686 send = 1;
5687 check_binding = 0;
5688 }
5689
5690 if (check_binding) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005691 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5692 key, state)) {
5693 /* We ran a binding so swallow the press and keep the
5694 * grab to swallow the released too. */
5695 send = 0;
5696 terminate = 0;
5697 db->key[1] = key;
5698 } else {
5699 /* Terminate the grab since the key pressed is not a
5700 * debug binding key. */
5701 send = 1;
5702 terminate = 1;
5703 }
5704 }
5705
5706 if (send) {
Neil Roberts96d790e2013-09-19 17:32:00 +01005707 serial = wl_display_next_serial(display);
5708 resource_list = &grab->keyboard->focus_resource_list;
5709 wl_resource_for_each(resource, resource_list) {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005710 wl_keyboard_send_key(resource, serial, time, key, state);
5711 }
5712 }
5713
5714 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005715 weston_keyboard_end_grab(grab->keyboard);
5716 if (grab->keyboard->input_method_resource)
5717 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005718 free(db);
5719 }
5720}
5721
5722static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005723debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005724 uint32_t mods_depressed, uint32_t mods_latched,
5725 uint32_t mods_locked, uint32_t group)
5726{
5727 struct wl_resource *resource;
Neil Roberts96d790e2013-09-19 17:32:00 +01005728 struct wl_list *resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005729
Neil Roberts96d790e2013-09-19 17:32:00 +01005730 resource_list = &grab->keyboard->focus_resource_list;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005731
Neil Roberts96d790e2013-09-19 17:32:00 +01005732 wl_resource_for_each(resource, resource_list) {
5733 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5734 mods_latched, mods_locked, group);
5735 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005736}
5737
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005738static void
5739debug_binding_cancel(struct weston_keyboard_grab *grab)
5740{
5741 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5742
5743 weston_keyboard_end_grab(grab->keyboard);
5744 free(db);
5745}
5746
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005747struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005748 debug_binding_key,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02005749 debug_binding_modifiers,
5750 debug_binding_cancel,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005751};
5752
5753static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005754debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005755{
5756 struct debug_binding_grab *grab;
5757
5758 grab = calloc(1, sizeof *grab);
5759 if (!grab)
5760 return;
5761
5762 grab->seat = (struct weston_seat *) seat;
5763 grab->key[0] = key;
5764 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04005765 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005766}
5767
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05005768static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005769force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005770 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005771{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04005772 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005773 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005774 struct desktop_shell *shell = data;
5775 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005776 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005777
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02005778 focus_surface = seat->keyboard->focus;
5779 if (!focus_surface)
5780 return;
5781
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005782 wl_signal_emit(&compositor->kill_signal, focus_surface);
5783
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05005784 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03005785 wl_client_get_credentials(client, &pid, NULL, NULL);
5786
5787 /* Skip clients that we launched ourselves (the credentials of
5788 * the socketpair is ours) */
5789 if (pid == getpid())
5790 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005791
Daniel Stone325fc2d2012-05-30 16:31:58 +01005792 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04005793}
5794
5795static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005796workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005797 uint32_t key, void *data)
5798{
5799 struct desktop_shell *shell = data;
5800 unsigned int new_index = shell->workspaces.current;
5801
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005802 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005803 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005804 if (new_index != 0)
5805 new_index--;
5806
5807 change_workspace(shell, new_index);
5808}
5809
5810static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005811workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005812 uint32_t key, void *data)
5813{
5814 struct desktop_shell *shell = data;
5815 unsigned int new_index = shell->workspaces.current;
5816
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005817 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005818 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005819 if (new_index < shell->workspaces.num - 1)
5820 new_index++;
5821
5822 change_workspace(shell, new_index);
5823}
5824
5825static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005826workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005827 uint32_t key, void *data)
5828{
5829 struct desktop_shell *shell = data;
5830 unsigned int new_index;
5831
Kristian Høgsbergce345b02012-06-25 21:35:29 -04005832 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04005833 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005834 new_index = key - KEY_F1;
5835 if (new_index >= shell->workspaces.num)
5836 new_index = shell->workspaces.num - 1;
5837
5838 change_workspace(shell, new_index);
5839}
5840
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005841static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005842workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005843 uint32_t key, void *data)
5844{
5845 struct desktop_shell *shell = data;
5846 unsigned int new_index = shell->workspaces.current;
5847
5848 if (shell->locked)
5849 return;
5850
5851 if (new_index != 0)
5852 new_index--;
5853
5854 take_surface_to_workspace_by_seat(shell, seat, new_index);
5855}
5856
5857static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04005858workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005859 uint32_t key, void *data)
5860{
5861 struct desktop_shell *shell = data;
5862 unsigned int new_index = shell->workspaces.current;
5863
5864 if (shell->locked)
5865 return;
5866
5867 if (new_index < shell->workspaces.num - 1)
5868 new_index++;
5869
5870 take_surface_to_workspace_by_seat(shell, seat, new_index);
5871}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005872
5873static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005874shell_reposition_view_on_output_destroy(struct weston_view *view)
5875{
5876 struct weston_output *output, *first_output;
5877 struct weston_compositor *ec = view->surface->compositor;
5878 struct shell_surface *shsurf;
5879 float x, y;
5880 int visible;
5881
5882 x = view->geometry.x;
5883 y = view->geometry.y;
5884
5885 /* At this point the destroyed output is not in the list anymore.
5886 * If the view is still visible somewhere, we leave where it is,
5887 * otherwise, move it to the first output. */
5888 visible = 0;
5889 wl_list_for_each(output, &ec->output_list, link) {
5890 if (pixman_region32_contains_point(&output->region,
5891 x, y, NULL)) {
5892 visible = 1;
5893 break;
5894 }
5895 }
5896
5897 if (!visible) {
5898 first_output = container_of(ec->output_list.next,
5899 struct weston_output, link);
5900
5901 x = first_output->x + first_output->width / 4;
5902 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08005903
5904 weston_view_set_position(view, x, y);
5905 } else {
5906 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005907 }
5908
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005909
5910 shsurf = get_shell_surface(view->surface);
5911
5912 if (shsurf) {
5913 shsurf->saved_position_valid = false;
5914 shsurf->next_state.maximized = false;
5915 shsurf->next_state.fullscreen = false;
5916 shsurf->state_changed = true;
5917 }
5918}
5919
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005920void
5921shell_for_each_layer(struct desktop_shell *shell,
5922 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005923{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005924 struct workspace **ws;
5925
5926 func(shell, &shell->fullscreen_layer, data);
5927 func(shell, &shell->panel_layer, data);
5928 func(shell, &shell->background_layer, data);
5929 func(shell, &shell->lock_layer, data);
5930 func(shell, &shell->input_panel_layer, data);
5931
5932 wl_array_for_each(ws, &shell->workspaces.array)
5933 func(shell, &(*ws)->layer, data);
5934}
5935
5936static void
5937shell_output_destroy_move_layer(struct desktop_shell *shell,
5938 struct weston_layer *layer,
5939 void *data)
5940{
5941 struct weston_output *output = data;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005942 struct weston_view *view;
5943
Giulio Camuffo412e6a52014-07-09 22:12:56 +03005944 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005945 if (view->output != output)
5946 continue;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005947
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005948 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005949 }
5950}
5951
5952static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005953handle_output_destroy(struct wl_listener *listener, void *data)
5954{
5955 struct shell_output *output_listener =
5956 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005957 struct weston_output *output = output_listener->output;
5958 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08005959
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005960 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02005961
Xiong Zhang6b481422013-10-23 13:58:32 +08005962 wl_list_remove(&output_listener->destroy_listener.link);
5963 wl_list_remove(&output_listener->link);
5964 free(output_listener);
5965}
5966
5967static void
5968create_shell_output(struct desktop_shell *shell,
5969 struct weston_output *output)
5970{
5971 struct shell_output *shell_output;
5972
5973 shell_output = zalloc(sizeof *shell_output);
5974 if (shell_output == NULL)
5975 return;
5976
5977 shell_output->output = output;
5978 shell_output->shell = shell;
5979 shell_output->destroy_listener.notify = handle_output_destroy;
5980 wl_signal_add(&output->destroy_signal,
5981 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07005982 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005983}
5984
5985static void
5986handle_output_create(struct wl_listener *listener, void *data)
5987{
5988 struct desktop_shell *shell =
5989 container_of(listener, struct desktop_shell, output_create_listener);
5990 struct weston_output *output = (struct weston_output *)data;
5991
5992 create_shell_output(shell, output);
5993}
5994
5995static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005996handle_output_move_layer(struct desktop_shell *shell,
5997 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005998{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005999 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006000 struct weston_view *view;
6001 float x, y;
6002
Giulio Camuffo412e6a52014-07-09 22:12:56 +03006003 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006004 if (view->output != output)
6005 continue;
6006
6007 x = view->geometry.x + output->move_x;
6008 y = view->geometry.y + output->move_y;
6009 weston_view_set_position(view, x, y);
6010 }
6011}
6012
6013static void
6014handle_output_move(struct wl_listener *listener, void *data)
6015{
6016 struct desktop_shell *shell;
6017
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006018 shell = container_of(listener, struct desktop_shell,
6019 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006020
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03006021 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006022}
6023
6024static void
Xiong Zhang6b481422013-10-23 13:58:32 +08006025setup_output_destroy_handler(struct weston_compositor *ec,
6026 struct desktop_shell *shell)
6027{
6028 struct weston_output *output;
6029
6030 wl_list_init(&shell->output_list);
6031 wl_list_for_each(output, &ec->output_list, link)
6032 create_shell_output(shell, output);
6033
6034 shell->output_create_listener.notify = handle_output_create;
6035 wl_signal_add(&ec->output_created_signal,
6036 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02006037
6038 shell->output_move_listener.notify = handle_output_move;
6039 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08006040}
6041
6042static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006043shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02006044{
Tiago Vignattibe143262012-04-16 17:31:41 +03006045 struct desktop_shell *shell =
6046 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006047 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08006048 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02006049
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08006050 /* Force state to unlocked so we don't try to fade */
6051 shell->locked = false;
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02006052 if (shell->child.client)
6053 wl_client_destroy(shell->child.client);
6054
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006055 wl_list_remove(&shell->idle_listener.link);
6056 wl_list_remove(&shell->wake_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006057
6058 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04006059
Xiong Zhang6b481422013-10-23 13:58:32 +08006060 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6061 wl_list_remove(&shell_output->destroy_listener.link);
6062 wl_list_remove(&shell_output->link);
6063 free(shell_output);
6064 }
6065
6066 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07006067 wl_list_remove(&shell->output_move_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08006068
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006069 wl_array_for_each(ws, &shell->workspaces.array)
6070 workspace_destroy(*ws);
6071 wl_array_release(&shell->workspaces.array);
6072
Pekka Paalanen3c647232011-12-22 13:43:43 +02006073 free(shell->screensaver.path);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01006074 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02006075 free(shell);
6076}
6077
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006078static void
6079shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6080{
6081 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006082 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006083
6084 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01006085 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6086 MODIFIER_CTRL | MODIFIER_ALT,
6087 terminate_binding, ec);
6088 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6089 click_to_activate_binding,
6090 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07006091 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6092 click_to_activate_binding,
6093 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01006094 weston_compositor_add_touch_binding(ec, 0,
6095 touch_to_activate_binding,
6096 shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006097 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6098 MODIFIER_SUPER | MODIFIER_ALT,
6099 surface_opacity_binding, NULL);
6100 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6101 MODIFIER_SUPER, zoom_axis_binding,
6102 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006103
6104 /* configurable bindings */
6105 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01006106 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6107 zoom_key_binding, NULL);
6108 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6109 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08006110 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6111 maximize_binding, NULL);
6112 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6113 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006114 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6115 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01006116 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006117 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6118 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08006119 weston_compositor_add_button_binding(ec, BTN_LEFT,
6120 mod | MODIFIER_SHIFT,
6121 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03006122
6123 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6124 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6125 rotate_binding, NULL);
6126
Daniel Stone325fc2d2012-05-30 16:31:58 +01006127 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6128 shell);
6129 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6130 ec);
6131 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6132 backlight_binding, ec);
6133 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6134 ec);
6135 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6136 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01006137 weston_compositor_add_key_binding(ec, KEY_K, mod,
6138 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006139 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6140 workspace_up_binding, shell);
6141 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6142 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006143 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6144 workspace_move_surface_up_binding,
6145 shell);
6146 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6147 workspace_move_surface_down_binding,
6148 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006149
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -08006150 if (shell->exposay_modifier)
6151 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6152 exposay_binding, shell);
Daniel Stonedf8133b2013-11-19 11:37:14 +01006153
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006154 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6155 if (shell->workspaces.num > 1) {
6156 num_workspace_bindings = shell->workspaces.num;
6157 if (num_workspace_bindings > 6)
6158 num_workspace_bindings = 6;
6159 for (i = 0; i < num_workspace_bindings; i++)
6160 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6161 workspace_f_binding,
6162 shell);
6163 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02006164
6165 /* Debug bindings */
6166 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6167 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006168}
6169
Jason Ekstrand024177c2014-04-21 19:42:58 -05006170static void
6171handle_seat_created(struct wl_listener *listener, void *data)
6172{
6173 struct weston_seat *seat = data;
6174
6175 create_shell_seat(seat);
6176}
6177
Kristian Høgsberg1c562182011-05-02 22:09:20 -04006178WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05006179module_init(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07006180 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006181{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04006182 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03006183 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006184 struct workspace **pws;
6185 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006186 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006187
Peter Huttererf3d62272013-08-08 11:57:05 +10006188 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04006189 if (shell == NULL)
6190 return -1;
6191
Kristian Høgsberg75840622011-09-06 13:48:16 -04006192 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04006193
6194 shell->destroy_listener.notify = shell_destroy;
6195 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02006196 shell->idle_listener.notify = idle_handler;
6197 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6198 shell->wake_listener.notify = wake_handler;
6199 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006200
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04006201 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006202 ec->shell_interface.create_shell_surface = create_shell_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05006203 ec->shell_interface.get_primary_view = get_primary_view;
Tiago Vignattibc052c92012-04-19 16:18:18 +03006204 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04006205 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg47792412014-05-04 13:47:06 -07006206 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03006207 ec->shell_interface.set_xwayland = set_xwayland;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07006208 ec->shell_interface.move = shell_interface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04006209 ec->shell_interface.resize = surface_resize;
Giulio Camuffo62942ad2013-09-11 18:20:47 +02006210 ec->shell_interface.set_title = set_title;
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04006211 ec->shell_interface.set_window_geometry = set_window_geometry;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006212
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006213 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6214 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006215 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6216 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02006217 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006218
6219 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02006220 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05006221
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08006222 if (input_panel_setup(shell) < 0)
6223 return -1;
6224
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04006225 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02006226
Daniel Stonedf8133b2013-11-19 11:37:14 +01006227 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6228 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6229
Jonas Ådahle3cddce2012-06-13 00:01:22 +02006230 for (i = 0; i < shell->workspaces.num; i++) {
6231 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6232 if (pws == NULL)
6233 return -1;
6234
6235 *pws = workspace_create();
6236 if (*pws == NULL)
6237 return -1;
6238 }
6239 activate_workspace(shell, 0);
6240
Manuel Bachmann50c87db2014-02-26 15:52:13 +01006241 weston_layer_init(&shell->minimized_layer, NULL);
6242
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02006243 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02006244 wl_list_init(&shell->workspaces.animation.link);
6245 shell->workspaces.animation.frame = animate_workspace_change_frame;
6246
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006247 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04006248 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006249 return -1;
6250
Rafael Antognollie2a34552013-12-03 15:35:45 -02006251 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6252 shell, bind_xdg_shell) == NULL)
6253 return -1;
6254
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006255 if (wl_global_create(ec->wl_display,
6256 &desktop_shell_interface, 2,
6257 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04006258 return -1;
6259
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006260 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6261 shell, bind_screensaver) == NULL)
Pekka Paalanen6e168112011-11-24 11:34:05 +02006262 return -1;
6263
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04006264 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6265 shell, bind_workspace_manager) == NULL)
Jonas Ådahle9d22502012-08-29 22:13:01 +02006266 return -1;
6267
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05006268 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006269
Xiong Zhang6b481422013-10-23 13:58:32 +08006270 setup_output_destroy_handler(ec, shell);
6271
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03006272 loop = wl_display_get_event_loop(ec->wl_display);
6273 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02006274
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02006275 shell->screensaver.timer =
6276 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6277
Jason Ekstrand024177c2014-04-21 19:42:58 -05006278 wl_list_for_each(seat, &ec->seat_list, link)
6279 handle_seat_created(NULL, seat);
6280 shell->seat_create_listener.notify = handle_seat_created;
6281 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04006282
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01006283 screenshooter_create(ec);
6284
Tiago Vignatti0b52d482012-04-20 18:54:25 +03006285 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04006286
Pekka Paalanen79346ab2013-05-22 18:03:09 +03006287 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02006288
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05006289 return 0;
6290}