blob: 55f3dccdb93ff5f043de61176ca93e01fda27f97 [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.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040033#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050034
Pekka Paalanen50719bc2011-11-22 14:18:50 +020035#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040037#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010038#include "input-method-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"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050041
Jonas Ådahle3cddce2012-06-13 00:01:22 +020042#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020043#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020044
Juan Zhaoe10d2792012-04-25 19:09:52 +080045enum animation_type {
46 ANIMATION_NONE,
47
48 ANIMATION_ZOOM,
49 ANIMATION_FADE
50};
51
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020052enum fade_type {
53 FADE_IN,
54 FADE_OUT
55};
56
Jonas Ådahl04769742012-06-13 00:01:24 +020057struct focus_state {
58 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040059 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020060 struct weston_surface *keyboard_focus;
61 struct wl_list link;
62 struct wl_listener seat_destroy_listener;
63 struct wl_listener surface_destroy_listener;
64};
65
Jonas Ådahle3cddce2012-06-13 00:01:22 +020066struct workspace {
67 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020068
69 struct wl_list focus_list;
70 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020071};
72
Philipp Brüschweiler88013572012-08-06 13:44:42 +020073struct input_panel_surface {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010074 struct wl_resource resource;
75
76 struct desktop_shell *shell;
77
Philipp Brüschweiler88013572012-08-06 13:44:42 +020078 struct wl_list link;
79 struct weston_surface *surface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010080 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020081
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +020082 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020083 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +020084};
85
Tiago Vignattibe143262012-04-16 17:31:41 +030086struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050087 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040088
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +020089 struct wl_listener idle_listener;
90 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040091 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020092 struct wl_listener show_input_panel_listener;
93 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020094 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020095
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050096 struct weston_layer fullscreen_layer;
97 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050098 struct weston_layer background_layer;
99 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200100 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500101
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400102 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300103 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400104
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200105 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500106 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200107 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200108 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200109
110 unsigned deathcount;
111 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200112 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200113
114 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200115 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200116 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200117
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200118 struct {
119 struct weston_surface *surface;
120 pixman_box32_t cursor_rectangle;
121 } text_input;
122
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400123 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500124 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100125
Pekka Paalanen77346a62011-11-30 16:26:35 +0200126 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200127 struct wl_array array;
128 unsigned int current;
129 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200130
Jonas Ådahle9d22502012-08-29 22:13:01 +0200131 struct wl_list client_list;
132
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200133 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200134 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200135 int anim_dir;
136 uint32_t anim_timestamp;
137 double anim_current;
138 struct workspace *anim_from;
139 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200140 } workspaces;
141
142 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200143 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200144 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200145 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500146 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200147 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200148 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500149
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200150 struct {
151 struct wl_resource *binding;
152 struct wl_list surfaces;
153 } input_panel;
154
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200155 struct {
156 struct weston_surface *surface;
157 struct weston_surface_animation *animation;
158 enum fade_type type;
159 } fade;
160
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300161 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800162 enum animation_type win_animation_type;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400163};
164
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500165enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200166 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500167 SHELL_SURFACE_TOPLEVEL,
168 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500169 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800170 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500171 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200172};
173
Scott Moreauff1db4a2012-04-17 19:06:18 -0600174struct ping_timer {
175 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600176 uint32_t serial;
177};
178
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200179struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200180 struct wl_resource resource;
181
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500182 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200183 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400184 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300185 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200186
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400187 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400188 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500189 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800190 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800191 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600192 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100193
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500194 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200195 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500196 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200197 } rotation;
198
199 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100200 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500201 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100202 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400203 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500204 } popup;
205
Alex Wu4539b082012-03-01 12:57:46 +0800206 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300207 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400208 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300209 } transient;
210
211 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800212 enum wl_shell_surface_fullscreen_method type;
213 struct weston_transform transform; /* matrix from x, y */
214 uint32_t framerate;
215 struct weston_surface *black_surface;
216 } fullscreen;
217
Scott Moreauff1db4a2012-04-17 19:06:18 -0600218 struct ping_timer *ping_timer;
219
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200220 struct weston_transform workspace_transform;
221
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500222 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500223 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100224 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400225
226 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200227};
228
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300229struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400230 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300231 struct shell_surface *shsurf;
232 struct wl_listener shsurf_destroy_listener;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400233 struct weston_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300234};
235
236struct weston_move_grab {
237 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100238 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500239};
240
Pekka Paalanen460099f2012-01-20 16:48:25 +0200241struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300242 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500243 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200244 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200245 float x;
246 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200247 } center;
248};
249
Giulio Camuffo5085a752013-03-25 21:42:45 +0100250struct shell_seat {
251 struct weston_seat *seat;
252 struct wl_listener seat_destroy_listener;
253
254 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400255 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100256 struct wl_list surfaces_list;
257 struct wl_client *client;
258 int32_t initial_up;
259 } popup_grab;
260};
261
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400262static void
263activate(struct desktop_shell *shell, struct weston_surface *es,
264 struct weston_seat *seat);
265
266static struct workspace *
267get_current_workspace(struct desktop_shell *shell);
268
Alex Wubd3354b2012-04-17 17:20:49 +0800269static struct shell_surface *
270get_shell_surface(struct weston_surface *surface);
271
272static struct desktop_shell *
273shell_surface_get_shell(struct shell_surface *shsurf);
274
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500275static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400276surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500277
Alex Wubd3354b2012-04-17 17:20:49 +0800278static bool
279shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
280{
281 struct desktop_shell *shell;
282 struct weston_surface *top_fs_es;
283
284 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100285
Alex Wubd3354b2012-04-17 17:20:49 +0800286 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
287 return false;
288
289 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100290 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800291 layer_link);
292 return (shsurf == get_shell_surface(top_fs_es));
293}
294
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500295static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400296destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300297{
298 struct shell_grab *grab;
299
300 grab = container_of(listener, struct shell_grab,
301 shsurf_destroy_listener);
302
303 grab->shsurf = NULL;
304}
305
306static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400307popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400308
309static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300310shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400311 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300312 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400313 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300314 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300315{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300316 struct desktop_shell *shell = shsurf->shell;
317
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400318 popup_grab_end(pointer);
319
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300320 grab->grab.interface = interface;
321 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400322 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
323 wl_signal_add(&shsurf->resource.destroy_signal,
324 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300325
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300326 grab->pointer = pointer;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300327
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400328 weston_pointer_start_grab(pointer, &grab->grab);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300329 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400330 weston_pointer_set_focus(pointer, shell->grab_surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400331 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300332}
333
334static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300335shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300336{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400337 if (grab->shsurf)
338 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300339
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400340 weston_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300341}
342
343static void
Alex Wu4539b082012-03-01 12:57:46 +0800344center_on_output(struct weston_surface *surface,
345 struct weston_output *output);
346
Daniel Stone496ca172012-05-30 16:31:42 +0100347static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300348get_modifier(char *modifier)
349{
350 if (!modifier)
351 return MODIFIER_SUPER;
352
353 if (!strcmp("ctrl", modifier))
354 return MODIFIER_CTRL;
355 else if (!strcmp("alt", modifier))
356 return MODIFIER_ALT;
357 else if (!strcmp("super", modifier))
358 return MODIFIER_SUPER;
359 else
360 return MODIFIER_SUPER;
361}
362
Juan Zhaoe10d2792012-04-25 19:09:52 +0800363static enum animation_type
364get_animation_type(char *animation)
365{
366 if (!animation)
367 return ANIMATION_NONE;
368
369 if (!strcmp("zoom", animation))
370 return ANIMATION_ZOOM;
371 else if (!strcmp("fade", animation))
372 return ANIMATION_FADE;
373 else
374 return ANIMATION_NONE;
375}
376
Alex Wu4539b082012-03-01 12:57:46 +0800377static void
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500378shell_configuration(struct desktop_shell *shell, const char *config_file)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200379{
Pekka Paalanen7296e792011-12-07 16:22:00 +0200380 char *path = NULL;
381 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200382 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300383 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800384 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200385
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400386 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300387 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800388 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200389 { "num-workspaces",
390 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200391 };
392
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400393 struct config_key saver_keys[] = {
394 { "path", CONFIG_KEY_STRING, &path },
395 { "duration", CONFIG_KEY_INTEGER, &duration },
396 };
397
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200398 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400399 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200400 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
401 };
402
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500403 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200404
Pekka Paalanen7296e792011-12-07 16:22:00 +0200405 shell->screensaver.path = path;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200406 shell->screensaver.duration = duration * 1000;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300407 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800408 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200409 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
410}
411
412static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200413focus_state_destroy(struct focus_state *state)
414{
415 wl_list_remove(&state->seat_destroy_listener.link);
416 wl_list_remove(&state->surface_destroy_listener.link);
417 free(state);
418}
419
420static void
421focus_state_seat_destroy(struct wl_listener *listener, void *data)
422{
423 struct focus_state *state = container_of(listener,
424 struct focus_state,
425 seat_destroy_listener);
426
427 wl_list_remove(&state->link);
428 focus_state_destroy(state);
429}
430
431static void
432focus_state_surface_destroy(struct wl_listener *listener, void *data)
433{
434 struct focus_state *state = container_of(listener,
435 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400436 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400437 struct desktop_shell *shell;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300438 struct weston_surface *main_surface;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400439 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200440
Pekka Paalanen01388e22013-04-25 13:57:44 +0300441 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
442
Kristian Høgsberge3778222012-07-31 17:29:30 -0400443 next = NULL;
444 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
Pekka Paalanen01388e22013-04-25 13:57:44 +0300445 if (surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400446 continue;
447
448 next = surface;
449 break;
450 }
451
Pekka Paalanen01388e22013-04-25 13:57:44 +0300452 /* if the focus was a sub-surface, activate its main surface */
453 if (main_surface != state->keyboard_focus)
454 next = main_surface;
455
Kristian Høgsberge3778222012-07-31 17:29:30 -0400456 if (next) {
457 shell = state->seat->compositor->shell_interface.shell;
458 activate(shell, next, state->seat);
459 } else {
460 wl_list_remove(&state->link);
461 focus_state_destroy(state);
462 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200463}
464
465static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400466focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200467{
Jonas Ådahl04769742012-06-13 00:01:24 +0200468 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200469
470 state = malloc(sizeof *state);
471 if (state == NULL)
472 return NULL;
473
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400474 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200475 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400476 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200477
478 state->seat_destroy_listener.notify = focus_state_seat_destroy;
479 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400480 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200481 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400482 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200483
484 return state;
485}
486
Jonas Ådahl8538b222012-08-29 22:13:03 +0200487static struct focus_state *
488ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
489{
490 struct workspace *ws = get_current_workspace(shell);
491 struct focus_state *state;
492
493 wl_list_for_each(state, &ws->focus_list, link)
494 if (state->seat == seat)
495 break;
496
497 if (&state->link == &ws->focus_list)
498 state = focus_state_create(seat, ws);
499
500 return state;
501}
502
Jonas Ådahl04769742012-06-13 00:01:24 +0200503static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400504restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200505{
506 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400507 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200508
509 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400510 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200511
Kristian Høgsberge3148752013-05-06 23:19:49 -0400512 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200513 }
514}
515
516static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200517replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
518 struct weston_seat *seat)
519{
520 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400521 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200522
523 wl_list_for_each(state, &ws->focus_list, link) {
524 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400525 surface = seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200526 state->keyboard_focus =
527 (struct weston_surface *) surface;
528 return;
529 }
530 }
531}
532
533static void
534drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
535 struct weston_surface *surface)
536{
537 struct focus_state *state;
538
539 wl_list_for_each(state, &ws->focus_list, link)
540 if (state->keyboard_focus == surface)
541 state->keyboard_focus = NULL;
542}
543
544static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200545workspace_destroy(struct workspace *ws)
546{
Jonas Ådahl04769742012-06-13 00:01:24 +0200547 struct focus_state *state, *next;
548
549 wl_list_for_each_safe(state, next, &ws->focus_list, link)
550 focus_state_destroy(state);
551
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200552 free(ws);
553}
554
Jonas Ådahl04769742012-06-13 00:01:24 +0200555static void
556seat_destroyed(struct wl_listener *listener, void *data)
557{
558 struct weston_seat *seat = data;
559 struct focus_state *state, *next;
560 struct workspace *ws = container_of(listener,
561 struct workspace,
562 seat_destroyed_listener);
563
564 wl_list_for_each_safe(state, next, &ws->focus_list, link)
565 if (state->seat == seat)
566 wl_list_remove(&state->link);
567}
568
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200569static struct workspace *
570workspace_create(void)
571{
572 struct workspace *ws = malloc(sizeof *ws);
573 if (ws == NULL)
574 return NULL;
575
576 weston_layer_init(&ws->layer, NULL);
577
Jonas Ådahl04769742012-06-13 00:01:24 +0200578 wl_list_init(&ws->focus_list);
579 wl_list_init(&ws->seat_destroyed_listener.link);
580 ws->seat_destroyed_listener.notify = seat_destroyed;
581
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200582 return ws;
583}
584
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200585static int
586workspace_is_empty(struct workspace *ws)
587{
588 return wl_list_empty(&ws->layer.surface_list);
589}
590
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200591static struct workspace *
592get_workspace(struct desktop_shell *shell, unsigned int index)
593{
594 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200595 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200596 pws += index;
597 return *pws;
598}
599
600static struct workspace *
601get_current_workspace(struct desktop_shell *shell)
602{
603 return get_workspace(shell, shell->workspaces.current);
604}
605
606static void
607activate_workspace(struct desktop_shell *shell, unsigned int index)
608{
609 struct workspace *ws;
610
611 ws = get_workspace(shell, index);
612 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
613
614 shell->workspaces.current = index;
615}
616
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200617static unsigned int
618get_output_height(struct weston_output *output)
619{
620 return abs(output->region.extents.y1 - output->region.extents.y2);
621}
622
623static void
624surface_translate(struct weston_surface *surface, double d)
625{
626 struct shell_surface *shsurf = get_shell_surface(surface);
627 struct weston_transform *transform;
628
629 transform = &shsurf->workspace_transform;
630 if (wl_list_empty(&transform->link))
631 wl_list_insert(surface->geometry.transformation_list.prev,
632 &shsurf->workspace_transform.link);
633
634 weston_matrix_init(&shsurf->workspace_transform.matrix);
635 weston_matrix_translate(&shsurf->workspace_transform.matrix,
636 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200637 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200638}
639
640static void
641workspace_translate_out(struct workspace *ws, double fraction)
642{
643 struct weston_surface *surface;
644 unsigned int height;
645 double d;
646
647 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
648 height = get_output_height(surface->output);
649 d = height * fraction;
650
651 surface_translate(surface, d);
652 }
653}
654
655static void
656workspace_translate_in(struct workspace *ws, double fraction)
657{
658 struct weston_surface *surface;
659 unsigned int height;
660 double d;
661
662 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
663 height = get_output_height(surface->output);
664
665 if (fraction > 0)
666 d = -(height - height * fraction);
667 else
668 d = height + height * fraction;
669
670 surface_translate(surface, d);
671 }
672}
673
674static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200675broadcast_current_workspace_state(struct desktop_shell *shell)
676{
677 struct wl_resource *resource;
678
679 wl_list_for_each(resource, &shell->workspaces.client_list, link)
680 workspace_manager_send_state(resource,
681 shell->workspaces.current,
682 shell->workspaces.num);
683}
684
685static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200686reverse_workspace_change_animation(struct desktop_shell *shell,
687 unsigned int index,
688 struct workspace *from,
689 struct workspace *to)
690{
691 shell->workspaces.current = index;
692
693 shell->workspaces.anim_to = to;
694 shell->workspaces.anim_from = from;
695 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
696 shell->workspaces.anim_timestamp = 0;
697
Scott Moreau4272e632012-08-13 09:58:41 -0600698 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200699}
700
701static void
702workspace_deactivate_transforms(struct workspace *ws)
703{
704 struct weston_surface *surface;
705 struct shell_surface *shsurf;
706
707 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
708 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200709 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
710 wl_list_remove(&shsurf->workspace_transform.link);
711 wl_list_init(&shsurf->workspace_transform.link);
712 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200713 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200714 }
715}
716
717static void
718finish_workspace_change_animation(struct desktop_shell *shell,
719 struct workspace *from,
720 struct workspace *to)
721{
Scott Moreau4272e632012-08-13 09:58:41 -0600722 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200723
724 wl_list_remove(&shell->workspaces.animation.link);
725 workspace_deactivate_transforms(from);
726 workspace_deactivate_transforms(to);
727 shell->workspaces.anim_to = NULL;
728
729 wl_list_remove(&shell->workspaces.anim_from->layer.link);
730}
731
732static void
733animate_workspace_change_frame(struct weston_animation *animation,
734 struct weston_output *output, uint32_t msecs)
735{
736 struct desktop_shell *shell =
737 container_of(animation, struct desktop_shell,
738 workspaces.animation);
739 struct workspace *from = shell->workspaces.anim_from;
740 struct workspace *to = shell->workspaces.anim_to;
741 uint32_t t;
742 double x, y;
743
744 if (workspace_is_empty(from) && workspace_is_empty(to)) {
745 finish_workspace_change_animation(shell, from, to);
746 return;
747 }
748
749 if (shell->workspaces.anim_timestamp == 0) {
750 if (shell->workspaces.anim_current == 0.0)
751 shell->workspaces.anim_timestamp = msecs;
752 else
753 shell->workspaces.anim_timestamp =
754 msecs -
755 /* Invers of movement function 'y' below. */
756 (asin(1.0 - shell->workspaces.anim_current) *
757 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
758 M_2_PI);
759 }
760
761 t = msecs - shell->workspaces.anim_timestamp;
762
763 /*
764 * x = [0, π/2]
765 * y(x) = sin(x)
766 */
767 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
768 y = sin(x);
769
770 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600771 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200772
773 workspace_translate_out(from, shell->workspaces.anim_dir * y);
774 workspace_translate_in(to, shell->workspaces.anim_dir * y);
775 shell->workspaces.anim_current = y;
776
Scott Moreau4272e632012-08-13 09:58:41 -0600777 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200778 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200779 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200780 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200781}
782
783static void
784animate_workspace_change(struct desktop_shell *shell,
785 unsigned int index,
786 struct workspace *from,
787 struct workspace *to)
788{
789 struct weston_output *output;
790
791 int dir;
792
793 if (index > shell->workspaces.current)
794 dir = -1;
795 else
796 dir = 1;
797
798 shell->workspaces.current = index;
799
800 shell->workspaces.anim_dir = dir;
801 shell->workspaces.anim_from = from;
802 shell->workspaces.anim_to = to;
803 shell->workspaces.anim_current = 0.0;
804 shell->workspaces.anim_timestamp = 0;
805
806 output = container_of(shell->compositor->output_list.next,
807 struct weston_output, link);
808 wl_list_insert(&output->animation_list,
809 &shell->workspaces.animation.link);
810
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200811 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200812
813 workspace_translate_in(to, 0);
814
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400815 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200816
Scott Moreau4272e632012-08-13 09:58:41 -0600817 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200818}
819
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200820static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200821update_workspace(struct desktop_shell *shell, unsigned int index,
822 struct workspace *from, struct workspace *to)
823{
824 shell->workspaces.current = index;
825 wl_list_insert(&from->layer.link, &to->layer.link);
826 wl_list_remove(&from->layer.link);
827}
828
829static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200830change_workspace(struct desktop_shell *shell, unsigned int index)
831{
832 struct workspace *from;
833 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200834
835 if (index == shell->workspaces.current)
836 return;
837
838 /* Don't change workspace when there is any fullscreen surfaces. */
839 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
840 return;
841
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200842 from = get_current_workspace(shell);
843 to = get_workspace(shell, index);
844
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200845 if (shell->workspaces.anim_from == to &&
846 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200847 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200848 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200849 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200850 return;
851 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200852
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200853 if (shell->workspaces.anim_to != NULL)
854 finish_workspace_change_animation(shell,
855 shell->workspaces.anim_from,
856 shell->workspaces.anim_to);
857
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200858 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200859
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200860 if (workspace_is_empty(to) && workspace_is_empty(from))
861 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200862 else
863 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200864
865 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200866}
867
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200868static bool
869workspace_has_only(struct workspace *ws, struct weston_surface *surface)
870{
871 struct wl_list *list = &ws->layer.surface_list;
872 struct wl_list *e;
873
874 if (wl_list_empty(list))
875 return false;
876
877 e = list->next;
878
879 if (e->next != list)
880 return false;
881
882 return container_of(e, struct weston_surface, layer_link) == surface;
883}
884
885static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200886move_surface_to_workspace(struct desktop_shell *shell,
887 struct weston_surface *surface,
888 uint32_t workspace)
889{
890 struct workspace *from;
891 struct workspace *to;
892 struct weston_seat *seat;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300893 struct weston_surface *focus;
894
895 assert(weston_surface_get_main_surface(surface) == surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200896
897 if (workspace == shell->workspaces.current)
898 return;
899
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200900 if (workspace >= shell->workspaces.num)
901 workspace = shell->workspaces.num - 1;
902
Jonas Ådahle9d22502012-08-29 22:13:01 +0200903 from = get_current_workspace(shell);
904 to = get_workspace(shell, workspace);
905
906 wl_list_remove(&surface->layer_link);
907 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
908
909 drop_focus_state(shell, from, surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300910 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
911 if (!seat->keyboard)
912 continue;
913
914 focus = weston_surface_get_main_surface(seat->keyboard->focus);
915 if (focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400916 weston_keyboard_set_focus(seat->keyboard, NULL);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300917 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200918
919 weston_surface_damage_below(surface);
920}
921
922static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200923take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400924 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200925 unsigned int index)
926{
Pekka Paalanen01388e22013-04-25 13:57:44 +0300927 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200928 struct shell_surface *shsurf;
929 struct workspace *from;
930 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200931 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200932
Pekka Paalanen01388e22013-04-25 13:57:44 +0300933 surface = weston_surface_get_main_surface(seat->keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200934 if (surface == NULL ||
935 index == shell->workspaces.current)
936 return;
937
938 from = get_current_workspace(shell);
939 to = get_workspace(shell, index);
940
941 wl_list_remove(&surface->layer_link);
942 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
943
Jonas Ådahle9d22502012-08-29 22:13:01 +0200944 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200945 drop_focus_state(shell, from, surface);
946
947 if (shell->workspaces.anim_from == to &&
948 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200949 wl_list_remove(&to->layer.link);
950 wl_list_insert(from->layer.link.prev, &to->layer.link);
951
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200952 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200953 broadcast_current_workspace_state(shell);
954
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200955 return;
956 }
957
958 if (shell->workspaces.anim_to != NULL)
959 finish_workspace_change_animation(shell,
960 shell->workspaces.anim_from,
961 shell->workspaces.anim_to);
962
963 if (workspace_is_empty(from) &&
964 workspace_has_only(to, surface))
965 update_workspace(shell, index, from, to);
966 else {
967 shsurf = get_shell_surface(surface);
968 if (wl_list_empty(&shsurf->workspace_transform.link))
969 wl_list_insert(&shell->workspaces.anim_sticky_list,
970 &shsurf->workspace_transform.link);
971
972 animate_workspace_change(shell, index, from, to);
973 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200974
975 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200976
977 state = ensure_focus_state(shell, seat);
978 if (state != NULL)
979 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200980}
981
982static void
983workspace_manager_move_surface(struct wl_client *client,
984 struct wl_resource *resource,
985 struct wl_resource *surface_resource,
986 uint32_t workspace)
987{
988 struct desktop_shell *shell = resource->data;
989 struct weston_surface *surface =
990 (struct weston_surface *) surface_resource;
Pekka Paalanen01388e22013-04-25 13:57:44 +0300991 struct weston_surface *main_surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200992
Pekka Paalanen01388e22013-04-25 13:57:44 +0300993 main_surface = weston_surface_get_main_surface(surface);
994 move_surface_to_workspace(shell, main_surface, workspace);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200995}
996
997static const struct workspace_manager_interface workspace_manager_implementation = {
998 workspace_manager_move_surface,
999};
1000
1001static void
1002unbind_resource(struct wl_resource *resource)
1003{
1004 wl_list_remove(&resource->link);
1005 free(resource);
1006}
1007
1008static void
1009bind_workspace_manager(struct wl_client *client,
1010 void *data, uint32_t version, uint32_t id)
1011{
1012 struct desktop_shell *shell = data;
1013 struct wl_resource *resource;
1014
1015 resource = wl_client_add_object(client, &workspace_manager_interface,
1016 &workspace_manager_implementation,
1017 id, shell);
1018
1019 if (resource == NULL) {
1020 weston_log("couldn't add workspace manager object");
1021 return;
1022 }
1023
1024 resource->destroy = unbind_resource;
1025 wl_list_insert(&shell->workspaces.client_list, &resource->link);
1026
1027 workspace_manager_send_state(resource,
1028 shell->workspaces.current,
1029 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001030}
1031
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001032static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001033noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001034{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001035}
1036
1037static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001038move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001039{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001040 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001041 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001042 struct shell_surface *shsurf = move->base.shsurf;
1043 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001044 int dx = wl_fixed_to_int(pointer->x + move->dx);
1045 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001046
1047 if (!shsurf)
1048 return;
1049
1050 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001051
Daniel Stone103db7f2012-05-08 17:17:55 +01001052 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001053 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001054
1055 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001056}
1057
1058static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001059move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001060 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001061{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001062 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1063 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001064 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001065 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001066
Daniel Stone4dbadb12012-05-30 16:31:51 +01001067 if (pointer->button_count == 0 &&
1068 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001069 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001070 free(grab);
1071 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001072}
1073
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001074static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001075 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001076 move_grab_motion,
1077 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001078};
1079
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001080static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001081surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001082{
1083 struct weston_move_grab *move;
1084
1085 if (!shsurf)
1086 return -1;
1087
1088 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1089 return 0;
1090
1091 move = malloc(sizeof *move);
1092 if (!move)
1093 return -1;
1094
1095 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001096 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001097 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001098 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001099
1100 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001101 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001102
1103 return 0;
1104}
1105
1106static void
1107shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1108 struct wl_resource *seat_resource, uint32_t serial)
1109{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001110 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001111 struct shell_surface *shsurf = resource->data;
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001112 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001113
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001114 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001115 if (seat->pointer->button_count == 0 ||
1116 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001117 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001118 return;
1119
Kristian Høgsberge3148752013-05-06 23:19:49 -04001120 if (surface_move(shsurf, seat) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001121 wl_resource_post_no_memory(resource);
1122}
1123
1124struct weston_resize_grab {
1125 struct shell_grab base;
1126 uint32_t edges;
1127 int32_t width, height;
1128};
1129
1130static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001131resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001132{
1133 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001134 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001135 struct shell_surface *shsurf = resize->base.shsurf;
1136 int32_t width, height;
1137 wl_fixed_t from_x, from_y;
1138 wl_fixed_t to_x, to_y;
1139
1140 if (!shsurf)
1141 return;
1142
1143 weston_surface_from_global_fixed(shsurf->surface,
1144 pointer->grab_x, pointer->grab_y,
1145 &from_x, &from_y);
1146 weston_surface_from_global_fixed(shsurf->surface,
1147 pointer->x, pointer->y, &to_x, &to_y);
1148
1149 width = resize->width;
1150 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1151 width += wl_fixed_to_int(from_x - to_x);
1152 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1153 width += wl_fixed_to_int(to_x - from_x);
1154 }
1155
1156 height = resize->height;
1157 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1158 height += wl_fixed_to_int(from_y - to_y);
1159 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1160 height += wl_fixed_to_int(to_y - from_y);
1161 }
1162
1163 shsurf->client->send_configure(shsurf->surface,
1164 resize->edges, width, height);
1165}
1166
1167static void
1168send_configure(struct weston_surface *surface,
1169 uint32_t edges, int32_t width, int32_t height)
1170{
1171 struct shell_surface *shsurf = get_shell_surface(surface);
1172
1173 wl_shell_surface_send_configure(&shsurf->resource,
1174 edges, width, height);
1175}
1176
1177static const struct weston_shell_client shell_client = {
1178 send_configure
1179};
1180
1181static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001182resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001183 uint32_t time, uint32_t button, uint32_t state_w)
1184{
1185 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001186 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001187 enum wl_pointer_button_state state = state_w;
1188
1189 if (pointer->button_count == 0 &&
1190 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1191 shell_grab_end(&resize->base);
1192 free(grab);
1193 }
1194}
1195
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001196static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001197 noop_grab_focus,
1198 resize_grab_motion,
1199 resize_grab_button,
1200};
1201
1202static int
1203surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001204 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001205{
1206 struct weston_resize_grab *resize;
1207
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001208 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1209 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001210 return 0;
1211
1212 if (edges == 0 || edges > 15 ||
1213 (edges & 3) == 3 || (edges & 12) == 12)
1214 return 0;
1215
1216 resize = malloc(sizeof *resize);
1217 if (!resize)
1218 return -1;
1219
1220 resize->edges = edges;
1221 resize->width = shsurf->surface->geometry.width;
1222 resize->height = shsurf->surface->geometry.height;
1223
1224 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001225 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001226
1227 return 0;
1228}
1229
1230static void
1231shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1232 struct wl_resource *seat_resource, uint32_t serial,
1233 uint32_t edges)
1234{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001235 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001236 struct shell_surface *shsurf = resource->data;
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001237 struct weston_surface *surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001238
1239 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1240 return;
1241
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001242 surface = weston_surface_get_main_surface(seat->pointer->focus);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001243 if (seat->pointer->button_count == 0 ||
1244 seat->pointer->grab_serial != serial ||
Giulio Camuffo61da3fc2013-04-25 13:57:45 +03001245 surface != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001246 return;
1247
Kristian Høgsberge3148752013-05-06 23:19:49 -04001248 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001249 wl_resource_post_no_memory(resource);
1250}
1251
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001252static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001253busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001254{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001255 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001256 struct weston_pointer *pointer = base->pointer;
1257 struct weston_surface *surface;
1258 wl_fixed_t sx, sy;
1259
1260 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1261 pointer->x, pointer->y,
1262 &sx, &sy);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001263
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001264 if (grab->shsurf->surface != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001265 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001266 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001267 }
1268}
1269
1270static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001271busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001272{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001273}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001274
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001275static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001276busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001277 uint32_t time, uint32_t button, uint32_t state)
1278{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001279 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001280 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001281 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001282
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001283 if (shsurf && button == BTN_LEFT && state) {
1284 activate(shsurf->shell, shsurf->surface, seat);
1285 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001286 } else if (shsurf && button == BTN_RIGHT && state) {
1287 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001288 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001289 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001290}
1291
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001292static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001293 busy_cursor_grab_focus,
1294 busy_cursor_grab_motion,
1295 busy_cursor_grab_button,
1296};
1297
1298static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001299set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001300{
1301 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001302
1303 grab = malloc(sizeof *grab);
1304 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001305 return;
1306
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001307 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1308 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001309}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001310
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001311static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001312end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001313{
1314 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1315
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001316 if (grab->grab.interface == &busy_cursor_grab_interface &&
1317 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001318 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001319 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001320 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001321}
1322
Scott Moreau9521d5e2012-04-19 13:06:17 -06001323static void
1324ping_timer_destroy(struct shell_surface *shsurf)
1325{
1326 if (!shsurf || !shsurf->ping_timer)
1327 return;
1328
1329 if (shsurf->ping_timer->source)
1330 wl_event_source_remove(shsurf->ping_timer->source);
1331
1332 free(shsurf->ping_timer);
1333 shsurf->ping_timer = NULL;
1334}
1335
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001336static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001337ping_timeout_handler(void *data)
1338{
1339 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001340 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001341
Scott Moreau9521d5e2012-04-19 13:06:17 -06001342 /* Client is not responding */
1343 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001344
1345 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001346 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001347 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001348
1349 return 1;
1350}
1351
1352static void
1353ping_handler(struct weston_surface *surface, uint32_t serial)
1354{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001355 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001356 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001357 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001358
1359 if (!shsurf)
1360 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001361 if (!shsurf->resource.client)
1362 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001363
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001364 if (shsurf->surface == shsurf->shell->grab_surface)
1365 return;
1366
Scott Moreauff1db4a2012-04-17 19:06:18 -06001367 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001368 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001369 if (!shsurf->ping_timer)
1370 return;
1371
1372 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001373 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1374 shsurf->ping_timer->source =
1375 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1376 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1377
1378 wl_shell_surface_send_ping(&shsurf->resource, serial);
1379 }
1380}
1381
1382static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001383handle_pointer_focus(struct wl_listener *listener, void *data)
1384{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001385 struct weston_pointer *pointer = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001386 struct weston_surface *surface =
1387 (struct weston_surface *) pointer->focus;
1388 struct weston_compositor *compositor;
1389 struct shell_surface *shsurf;
1390 uint32_t serial;
1391
1392 if (!surface)
1393 return;
1394
1395 compositor = surface->compositor;
1396 shsurf = get_shell_surface(surface);
1397
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001398 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001399 set_busy_cursor(shsurf, pointer);
1400 } else {
1401 serial = wl_display_next_serial(compositor->wl_display);
1402 ping_handler(surface, serial);
1403 }
1404}
1405
1406static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001407create_pointer_focus_listener(struct weston_seat *seat)
1408{
1409 struct wl_listener *listener;
1410
Kristian Høgsberge3148752013-05-06 23:19:49 -04001411 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001412 return;
1413
1414 listener = malloc(sizeof *listener);
1415 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001416 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001417}
1418
1419static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001420shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1421 uint32_t serial)
1422{
1423 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001424 struct weston_seat *seat;
1425 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001426
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001427 if (shsurf->ping_timer == NULL)
1428 /* Just ignore unsolicited pong. */
1429 return;
1430
Scott Moreauff1db4a2012-04-17 19:06:18 -06001431 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001432 shsurf->unresponsive = 0;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001433 wl_list_for_each(seat, &ec->seat_list, link)
1434 end_busy_cursor(shsurf, seat->pointer);
Scott Moreau9521d5e2012-04-19 13:06:17 -06001435 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001436 }
1437}
1438
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001439static void
1440shell_surface_set_title(struct wl_client *client,
1441 struct wl_resource *resource, const char *title)
1442{
1443 struct shell_surface *shsurf = resource->data;
1444
1445 free(shsurf->title);
1446 shsurf->title = strdup(title);
1447}
1448
1449static void
1450shell_surface_set_class(struct wl_client *client,
1451 struct wl_resource *resource, const char *class)
1452{
1453 struct shell_surface *shsurf = resource->data;
1454
1455 free(shsurf->class);
1456 shsurf->class = strdup(class);
1457}
1458
Juan Zhao96879df2012-02-07 08:45:41 +08001459static struct weston_output *
1460get_default_output(struct weston_compositor *compositor)
1461{
1462 return container_of(compositor->output_list.next,
1463 struct weston_output, link);
1464}
1465
Alex Wu4539b082012-03-01 12:57:46 +08001466static void
1467shell_unset_fullscreen(struct shell_surface *shsurf)
1468{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001469 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001470 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001471 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1472 shell_surface_is_top_fullscreen(shsurf)) {
1473 weston_output_switch_mode(shsurf->fullscreen_output,
1474 shsurf->fullscreen_output->origin);
1475 }
Alex Wu4539b082012-03-01 12:57:46 +08001476 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1477 shsurf->fullscreen.framerate = 0;
1478 wl_list_remove(&shsurf->fullscreen.transform.link);
1479 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001480 if (shsurf->fullscreen.black_surface)
1481 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001482 shsurf->fullscreen.black_surface = NULL;
1483 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001484 weston_surface_set_position(shsurf->surface,
1485 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001486 if (shsurf->saved_rotation_valid) {
1487 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1488 &shsurf->rotation.transform.link);
1489 shsurf->saved_rotation_valid = false;
1490 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001491
1492 ws = get_current_workspace(shsurf->shell);
1493 wl_list_remove(&shsurf->surface->layer_link);
1494 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001495}
1496
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001497static void
1498shell_unset_maximized(struct shell_surface *shsurf)
1499{
1500 struct workspace *ws;
1501 /* undo all maximized things here */
1502 shsurf->output = get_default_output(shsurf->surface->compositor);
1503 weston_surface_set_position(shsurf->surface,
1504 shsurf->saved_x,
1505 shsurf->saved_y);
1506
1507 if (shsurf->saved_rotation_valid) {
1508 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1509 &shsurf->rotation.transform.link);
1510 shsurf->saved_rotation_valid = false;
1511 }
1512
1513 ws = get_current_workspace(shsurf->shell);
1514 wl_list_remove(&shsurf->surface->layer_link);
1515 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1516}
1517
Pekka Paalanen98262232011-12-01 10:42:22 +02001518static int
1519reset_shell_surface_type(struct shell_surface *surface)
1520{
1521 switch (surface->type) {
1522 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001523 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001524 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001525 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001526 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001527 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001528 case SHELL_SURFACE_NONE:
1529 case SHELL_SURFACE_TOPLEVEL:
1530 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001531 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001532 break;
1533 }
1534
1535 surface->type = SHELL_SURFACE_NONE;
1536 return 0;
1537}
1538
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001539static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001540set_surface_type(struct shell_surface *shsurf)
1541{
1542 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001543 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001544
1545 reset_shell_surface_type(shsurf);
1546
1547 shsurf->type = shsurf->next_type;
1548 shsurf->next_type = SHELL_SURFACE_NONE;
1549
1550 switch (shsurf->type) {
1551 case SHELL_SURFACE_TOPLEVEL:
1552 break;
1553 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001554 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001555 pes->geometry.x + shsurf->transient.x,
1556 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001557 break;
1558
1559 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001560 case SHELL_SURFACE_FULLSCREEN:
1561 shsurf->saved_x = surface->geometry.x;
1562 shsurf->saved_y = surface->geometry.y;
1563 shsurf->saved_position_valid = true;
1564
1565 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1566 wl_list_remove(&shsurf->rotation.transform.link);
1567 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001568 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001569 shsurf->saved_rotation_valid = true;
1570 }
1571 break;
1572
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001573 default:
1574 break;
1575 }
1576}
1577
1578static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001579set_toplevel(struct shell_surface *shsurf)
1580{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001581 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001582}
1583
1584static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001585shell_surface_set_toplevel(struct wl_client *client,
1586 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001587{
Pekka Paalanen98262232011-12-01 10:42:22 +02001588 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001589
Tiago Vignattibc052c92012-04-19 16:18:18 +03001590 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001591}
1592
1593static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001594set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001595 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001596{
1597 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001598 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001599 shsurf->transient.x = x;
1600 shsurf->transient.y = y;
1601 shsurf->transient.flags = flags;
1602 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1603}
1604
1605static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001606shell_surface_set_transient(struct wl_client *client,
1607 struct wl_resource *resource,
1608 struct wl_resource *parent_resource,
1609 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001610{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001611 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001612 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001613
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001614 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001615}
1616
Tiago Vignattibe143262012-04-16 17:31:41 +03001617static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001618shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001619{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001620 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001621}
1622
1623static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001624get_output_panel_height(struct desktop_shell *shell,
1625 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001626{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001627 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001628 int panel_height = 0;
1629
1630 if (!output)
1631 return 0;
1632
Juan Zhao4ab94682012-07-09 22:24:09 -07001633 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001634 if (surface->output == output) {
1635 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001636 break;
1637 }
1638 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001639
Juan Zhao96879df2012-02-07 08:45:41 +08001640 return panel_height;
1641}
1642
1643static void
1644shell_surface_set_maximized(struct wl_client *client,
1645 struct wl_resource *resource,
1646 struct wl_resource *output_resource )
1647{
1648 struct shell_surface *shsurf = resource->data;
1649 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001650 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001651 uint32_t edges = 0, panel_height = 0;
1652
1653 /* get the default output, if the client set it as NULL
1654 check whether the ouput is available */
1655 if (output_resource)
1656 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001657 else if (es->output)
1658 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001659 else
1660 shsurf->output = get_default_output(es->compositor);
1661
Tiago Vignattibe143262012-04-16 17:31:41 +03001662 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001663 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001664 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001665
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001666 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001667 shsurf->output->width,
1668 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001669
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001670 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001671}
1672
Alex Wu21858432012-04-01 20:13:08 +08001673static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001674black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height);
Alex Wu21858432012-04-01 20:13:08 +08001675
Alex Wu4539b082012-03-01 12:57:46 +08001676static struct weston_surface *
1677create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001678 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001679 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001680{
1681 struct weston_surface *surface = NULL;
1682
1683 surface = weston_surface_create(ec);
1684 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001685 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001686 return NULL;
1687 }
1688
Alex Wu21858432012-04-01 20:13:08 +08001689 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001690 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001691 weston_surface_configure(surface, x, y, w, h);
1692 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001693 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001694 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001695 pixman_region32_fini(&surface->input);
1696 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001697
Alex Wu4539b082012-03-01 12:57:46 +08001698 return surface;
1699}
1700
1701/* Create black surface and append it to the associated fullscreen surface.
1702 * Handle size dismatch and positioning according to the method. */
1703static void
1704shell_configure_fullscreen(struct shell_surface *shsurf)
1705{
1706 struct weston_output *output = shsurf->fullscreen_output;
1707 struct weston_surface *surface = shsurf->surface;
1708 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001709 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001710
Alex Wu4539b082012-03-01 12:57:46 +08001711 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001712 shsurf->fullscreen.black_surface =
1713 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001714 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001715 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001716 output->width,
1717 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001718
1719 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1720 wl_list_insert(&surface->layer_link,
1721 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001722 shsurf->fullscreen.black_surface->output = output;
1723
1724 switch (shsurf->fullscreen.type) {
1725 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001726 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001727 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001728 break;
1729 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001730 /* 1:1 mapping between surface and output dimensions */
1731 if (output->width == surface->geometry.width &&
1732 output->height == surface->geometry.height) {
1733 weston_surface_set_position(surface, output->x, output->y);
1734 break;
1735 }
1736
Alex Wu4539b082012-03-01 12:57:46 +08001737 matrix = &shsurf->fullscreen.transform.matrix;
1738 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001739
Scott Moreau1bad5db2012-08-18 01:04:05 -06001740 output_aspect = (float) output->width /
1741 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001742 surface_aspect = (float) surface->geometry.width /
1743 (float) surface->geometry.height;
1744 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001745 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001746 (float) surface->geometry.width;
1747 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001748 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001749 (float) surface->geometry.height;
1750
Alex Wu4539b082012-03-01 12:57:46 +08001751 weston_matrix_scale(matrix, scale, scale, 1);
1752 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001753 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001754 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001755 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1756 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001757 weston_surface_set_position(surface, x, y);
1758
Alex Wu4539b082012-03-01 12:57:46 +08001759 break;
1760 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001761 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001762 struct weston_mode mode = {0,
Alex Wubd3354b2012-04-17 17:20:49 +08001763 surface->geometry.width,
1764 surface->geometry.height,
1765 shsurf->fullscreen.framerate};
1766
1767 if (weston_output_switch_mode(output, &mode) == 0) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001768 weston_surface_configure(shsurf->fullscreen.black_surface,
Alex Wubd3354b2012-04-17 17:20:49 +08001769 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001770 output->width,
1771 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001772 weston_surface_set_position(surface, output->x, output->y);
1773 break;
1774 }
1775 }
Alex Wu4539b082012-03-01 12:57:46 +08001776 break;
1777 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1778 break;
1779 default:
1780 break;
1781 }
1782}
1783
1784/* make the fullscreen and black surface at the top */
1785static void
1786shell_stack_fullscreen(struct shell_surface *shsurf)
1787{
Alex Wubd3354b2012-04-17 17:20:49 +08001788 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001789 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001790 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001791
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001792 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001793 wl_list_insert(&shell->fullscreen_layer.surface_list,
1794 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001795 weston_surface_damage(surface);
1796
1797 if (!shsurf->fullscreen.black_surface)
1798 shsurf->fullscreen.black_surface =
1799 create_black_surface(surface->compositor,
1800 surface,
1801 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001802 output->width,
1803 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001804
1805 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001806 wl_list_insert(&surface->layer_link,
1807 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001808 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001809}
1810
1811static void
1812shell_map_fullscreen(struct shell_surface *shsurf)
1813{
Alex Wu4539b082012-03-01 12:57:46 +08001814 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001815 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001816}
1817
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001818static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001819set_fullscreen(struct shell_surface *shsurf,
1820 uint32_t method,
1821 uint32_t framerate,
1822 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001823{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001824 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001825
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001826 if (output)
1827 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001828 else if (es->output)
1829 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001830 else
1831 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001832
Alex Wu4539b082012-03-01 12:57:46 +08001833 shsurf->fullscreen_output = shsurf->output;
1834 shsurf->fullscreen.type = method;
1835 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001836 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001837
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001838 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001839 shsurf->output->width,
1840 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001841}
1842
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001843static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001844shell_surface_set_fullscreen(struct wl_client *client,
1845 struct wl_resource *resource,
1846 uint32_t method,
1847 uint32_t framerate,
1848 struct wl_resource *output_resource)
1849{
1850 struct shell_surface *shsurf = resource->data;
1851 struct weston_output *output;
1852
1853 if (output_resource)
1854 output = output_resource->data;
1855 else
1856 output = NULL;
1857
1858 set_fullscreen(shsurf, method, framerate, output);
1859}
1860
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001861static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001862
1863static void
1864destroy_shell_seat(struct wl_listener *listener, void *data)
1865{
1866 struct shell_seat *shseat =
1867 container_of(listener,
1868 struct shell_seat, seat_destroy_listener);
1869 struct shell_surface *shsurf, *prev = NULL;
1870
1871 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001872 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001873 shseat->popup_grab.client = NULL;
1874
1875 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1876 shsurf->popup.shseat = NULL;
1877 if (prev) {
1878 wl_list_init(&prev->popup.grab_link);
1879 }
1880 prev = shsurf;
1881 }
1882 wl_list_init(&prev->popup.grab_link);
1883 }
1884
1885 wl_list_remove(&shseat->seat_destroy_listener.link);
1886 free(shseat);
1887}
1888
1889static struct shell_seat *
1890create_shell_seat(struct weston_seat *seat)
1891{
1892 struct shell_seat *shseat;
1893
1894 shseat = calloc(1, sizeof *shseat);
1895 if (!shseat) {
1896 weston_log("no memory to allocate shell seat\n");
1897 return NULL;
1898 }
1899
1900 shseat->seat = seat;
1901 wl_list_init(&shseat->popup_grab.surfaces_list);
1902
1903 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1904 wl_signal_add(&seat->destroy_signal,
1905 &shseat->seat_destroy_listener);
1906
1907 return shseat;
1908}
1909
1910static struct shell_seat *
1911get_shell_seat(struct weston_seat *seat)
1912{
1913 struct wl_listener *listener;
1914
1915 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
1916 if (listener == NULL)
1917 return create_shell_seat(seat);
1918
1919 return container_of(listener,
1920 struct shell_seat, seat_destroy_listener);
1921}
1922
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001923static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001924popup_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001925{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001926 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001927 struct weston_surface *surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001928 struct shell_seat *shseat =
1929 container_of(grab, struct shell_seat, popup_grab.grab);
1930 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001931 wl_fixed_t sx, sy;
1932
1933 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1934 pointer->x, pointer->y,
1935 &sx, &sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001936
Pekka Paalanencb108432012-01-19 16:25:40 +02001937 if (surface && surface->resource.client == client) {
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001938 weston_pointer_set_focus(pointer, surface, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001939 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001940 weston_pointer_set_focus(pointer, NULL,
1941 wl_fixed_from_int(0),
1942 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001943 }
1944}
1945
1946static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001947popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001948{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001949 struct weston_pointer *pointer = grab->pointer;
1950 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001951
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001952 if (pointer->focus_resource) {
1953 weston_surface_from_global_fixed(pointer->focus,
1954 pointer->x, pointer->y,
1955 &sx, &sy);
1956 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
1957 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001958}
1959
1960static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001961popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001962 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001963{
1964 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001965 struct shell_seat *shseat =
1966 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001967 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001968 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001969 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001970
Daniel Stone37816df2012-05-16 18:45:18 +01001971 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001972 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001973 display = wl_client_get_display(resource->client);
1974 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001975 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001976 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01001977 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001978 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001979 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001980 }
1981
Daniel Stone4dbadb12012-05-30 16:31:51 +01001982 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01001983 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001984}
1985
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001986static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001987 popup_grab_focus,
1988 popup_grab_motion,
1989 popup_grab_button,
1990};
1991
1992static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001993popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001994{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001995 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001996 struct shell_seat *shseat =
1997 container_of(grab, struct shell_seat, popup_grab.grab);
1998 struct shell_surface *shsurf;
1999 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002000
2001 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002002 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002003 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002004 shseat->popup_grab.grab.interface = NULL;
2005 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01002006 /* Send the popup_done event to all the popups open */
2007 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2008 wl_shell_surface_send_popup_done(&shsurf->resource);
2009 shsurf->popup.shseat = NULL;
2010 if (prev) {
2011 wl_list_init(&prev->popup.grab_link);
2012 }
2013 prev = shsurf;
2014 }
2015 wl_list_init(&prev->popup.grab_link);
2016 wl_list_init(&shseat->popup_grab.surfaces_list);
2017 }
2018}
2019
2020static void
2021add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2022{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002023 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002024
2025 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002026 shseat->popup_grab.client = shsurf->resource.client;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002027 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002028 /* We must make sure here that this popup was opened after
2029 * a mouse press, and not just by moving around with other
2030 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002031 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002032 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002033
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002034 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002035 }
2036 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2037}
2038
2039static void
2040remove_popup_grab(struct shell_surface *shsurf)
2041{
2042 struct shell_seat *shseat = shsurf->popup.shseat;
2043
2044 wl_list_remove(&shsurf->popup.grab_link);
2045 wl_list_init(&shsurf->popup.grab_link);
2046 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002047 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002048 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002049 }
2050}
2051
2052static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002053shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002054{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002055 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002056 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002057 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002058
2059 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002060
Pekka Paalanen483243f2013-03-08 14:56:50 +02002061 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002062 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2063 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002064
Kristian Høgsberge3148752013-05-06 23:19:49 -04002065 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002066 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002067 } else {
2068 wl_shell_surface_send_popup_done(&shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002069 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002070 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002071}
2072
2073static void
2074shell_surface_set_popup(struct wl_client *client,
2075 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002076 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002077 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002078 struct wl_resource *parent_resource,
2079 int32_t x, int32_t y, uint32_t flags)
2080{
2081 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002082
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002083 shsurf->type = SHELL_SURFACE_POPUP;
2084 shsurf->parent = parent_resource->data;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002085 shsurf->popup.shseat = get_shell_seat(seat_resource->data);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002086 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002087 shsurf->popup.x = x;
2088 shsurf->popup.y = y;
2089}
2090
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002091static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002092 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002093 shell_surface_move,
2094 shell_surface_resize,
2095 shell_surface_set_toplevel,
2096 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002097 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002098 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002099 shell_surface_set_maximized,
2100 shell_surface_set_title,
2101 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002102};
2103
2104static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002105destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002106{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002107 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2108 remove_popup_grab(shsurf);
2109 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002110
Alex Wubd3354b2012-04-17 17:20:49 +08002111 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2112 shell_surface_is_top_fullscreen(shsurf)) {
2113 weston_output_switch_mode(shsurf->fullscreen_output,
2114 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002115 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002116
Alex Wuaa08e2d2012-03-05 11:01:40 +08002117 if (shsurf->fullscreen.black_surface)
2118 weston_surface_destroy(shsurf->fullscreen.black_surface);
2119
Alex Wubd3354b2012-04-17 17:20:49 +08002120 /* As destroy_resource() use wl_list_for_each_safe(),
2121 * we can always remove the listener.
2122 */
2123 wl_list_remove(&shsurf->surface_destroy_listener.link);
2124 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002125 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002126 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002127
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002128 wl_list_remove(&shsurf->link);
2129 free(shsurf);
2130}
2131
2132static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002133shell_destroy_shell_surface(struct wl_resource *resource)
2134{
2135 struct shell_surface *shsurf = resource->data;
2136
2137 destroy_shell_surface(shsurf);
2138}
2139
2140static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002141shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002142{
2143 struct shell_surface *shsurf = container_of(listener,
2144 struct shell_surface,
2145 surface_destroy_listener);
2146
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002147 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002148 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002149 } else {
2150 wl_signal_emit(&shsurf->resource.destroy_signal,
2151 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002152 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002153 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002154}
2155
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002156static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002157shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002158
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002159static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002160get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002161{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002162 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002163 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002164 else
2165 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002166}
2167
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002168static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002169create_shell_surface(void *shell, struct weston_surface *surface,
2170 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002171{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002172 struct shell_surface *shsurf;
2173
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002174 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002175 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002176 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002177 }
2178
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002179 shsurf = calloc(1, sizeof *shsurf);
2180 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002181 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002182 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002183 }
2184
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002185 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002186 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002187
Tiago Vignattibc052c92012-04-19 16:18:18 +03002188 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002189 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002190 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002191 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002192 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002193 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2194 shsurf->fullscreen.framerate = 0;
2195 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002196 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002197 wl_list_init(&shsurf->fullscreen.transform.link);
2198
Tiago Vignattibc052c92012-04-19 16:18:18 +03002199 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002200 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002201 wl_signal_add(&surface->resource.destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002202 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002203
2204 /* init link so its safe to always remove it in destroy_shell_surface */
2205 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002206 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002207
Pekka Paalanen460099f2012-01-20 16:48:25 +02002208 /* empty when not in use */
2209 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002210 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002211
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002212 wl_list_init(&shsurf->workspace_transform.link);
2213
Pekka Paalanen98262232011-12-01 10:42:22 +02002214 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002215 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002216
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002217 shsurf->client = client;
2218
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002219 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002220}
2221
2222static void
2223shell_get_shell_surface(struct wl_client *client,
2224 struct wl_resource *resource,
2225 uint32_t id,
2226 struct wl_resource *surface_resource)
2227{
2228 struct weston_surface *surface = surface_resource->data;
2229 struct desktop_shell *shell = resource->data;
2230 struct shell_surface *shsurf;
2231
2232 if (get_shell_surface(surface)) {
2233 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002234 WL_DISPLAY_ERROR_INVALID_OBJECT,
2235 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002236 return;
2237 }
2238
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002239 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002240 if (!shsurf) {
2241 wl_resource_post_error(surface_resource,
2242 WL_DISPLAY_ERROR_INVALID_OBJECT,
2243 "surface->configure already set");
2244 return;
2245 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002246
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002247 shsurf->resource.destroy = shell_destroy_shell_surface;
2248 shsurf->resource.object.id = id;
2249 shsurf->resource.object.interface = &wl_shell_surface_interface;
2250 shsurf->resource.object.implementation =
2251 (void (**)(void)) &shell_surface_implementation;
2252 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002253
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002254 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002255}
2256
2257static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002258 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002259};
2260
Kristian Høgsberg07937562011-04-12 17:25:42 -04002261static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002262shell_fade(struct desktop_shell *shell, enum fade_type type);
2263
2264static int
2265screensaver_timeout(void *data)
2266{
2267 struct desktop_shell *shell = data;
2268
2269 shell_fade(shell, FADE_OUT);
2270
2271 return 1;
2272}
2273
2274static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002275handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002276{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002277 struct desktop_shell *shell =
2278 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002279
Pekka Paalanen18027e52011-12-02 16:31:49 +02002280 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002281
2282 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002283 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002284}
2285
2286static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002287launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002288{
2289 if (shell->screensaver.binding)
2290 return;
2291
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002292 if (!shell->screensaver.path) {
2293 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002294 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002295 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002296
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002297 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002298 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002299 return;
2300 }
2301
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002302 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002303 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002304 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002305 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002306}
2307
2308static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002309terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002310{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002311 if (shell->screensaver.process.pid == 0)
2312 return;
2313
2314 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002315}
2316
2317static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002318configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002319{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002320 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002321
Giulio Camuffo184df502013-02-21 11:29:21 +01002322 if (width == 0)
2323 return;
2324
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002325 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2326 if (s->output == es->output && s != es) {
2327 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002328 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002329 }
2330 }
2331
Giulio Camuffo184df502013-02-21 11:29:21 +01002332 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002333
2334 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002335 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002336 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002337 }
2338}
2339
2340static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002341background_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002342{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002343 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002344
Giulio Camuffo184df502013-02-21 11:29:21 +01002345 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002346}
2347
2348static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002349desktop_shell_set_background(struct wl_client *client,
2350 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002351 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002352 struct wl_resource *surface_resource)
2353{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002354 struct desktop_shell *shell = resource->data;
2355 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002356
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002357 if (surface->configure) {
2358 wl_resource_post_error(surface_resource,
2359 WL_DISPLAY_ERROR_INVALID_OBJECT,
2360 "surface role already assigned");
2361 return;
2362 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002363
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002364 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002365 surface->configure_private = shell;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002366 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002367 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002368 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002369 surface->output->width,
2370 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002371}
2372
2373static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002374panel_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002375{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002376 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002377
Giulio Camuffo184df502013-02-21 11:29:21 +01002378 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002379}
2380
2381static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002382desktop_shell_set_panel(struct wl_client *client,
2383 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002384 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002385 struct wl_resource *surface_resource)
2386{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002387 struct desktop_shell *shell = resource->data;
2388 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002389
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002390 if (surface->configure) {
2391 wl_resource_post_error(surface_resource,
2392 WL_DISPLAY_ERROR_INVALID_OBJECT,
2393 "surface role already assigned");
2394 return;
2395 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002396
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002397 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002398 surface->configure_private = shell;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002399 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002400 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002401 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002402 surface->output->width,
2403 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002404}
2405
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002406static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002407lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002408{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002409 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002410
Giulio Camuffo184df502013-02-21 11:29:21 +01002411 if (width == 0)
2412 return;
2413
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002414 center_on_output(surface, get_default_output(shell->compositor));
2415
2416 if (!weston_surface_is_mapped(surface)) {
2417 wl_list_insert(&shell->lock_layer.surface_list,
2418 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002419 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002420 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002421 }
2422}
2423
2424static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002425handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002426{
Tiago Vignattibe143262012-04-16 17:31:41 +03002427 struct desktop_shell *shell =
2428 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002429
Martin Minarik6d118362012-06-07 18:01:59 +02002430 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002431 shell->lock_surface = NULL;
2432}
2433
2434static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002435desktop_shell_set_lock_surface(struct wl_client *client,
2436 struct wl_resource *resource,
2437 struct wl_resource *surface_resource)
2438{
Tiago Vignattibe143262012-04-16 17:31:41 +03002439 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002440 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002441
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002442 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002443
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002444 if (!shell->locked)
2445 return;
2446
Pekka Paalanen98262232011-12-01 10:42:22 +02002447 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002448
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002449 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2450 wl_signal_add(&surface_resource->destroy_signal,
2451 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002452
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002453 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002454 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002455}
2456
2457static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002458resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002459{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002460 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002461
Pekka Paalanen77346a62011-11-30 16:26:35 +02002462 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002463
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002464 wl_list_remove(&shell->lock_layer.link);
2465 wl_list_insert(&shell->compositor->cursor_layer.link,
2466 &shell->fullscreen_layer.link);
2467 wl_list_insert(&shell->fullscreen_layer.link,
2468 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002469 if (shell->showing_input_panels) {
2470 wl_list_insert(&shell->panel_layer.link,
2471 &shell->input_panel_layer.link);
2472 wl_list_insert(&shell->input_panel_layer.link,
2473 &ws->layer.link);
2474 } else {
2475 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2476 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002477
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002478 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002479
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002480 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002481 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002482 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002483}
2484
2485static void
2486desktop_shell_unlock(struct wl_client *client,
2487 struct wl_resource *resource)
2488{
Tiago Vignattibe143262012-04-16 17:31:41 +03002489 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002490
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002491 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002492
2493 if (shell->locked)
2494 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002495}
2496
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002497static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002498desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002499 struct wl_resource *resource,
2500 struct wl_resource *surface_resource)
2501{
2502 struct desktop_shell *shell = resource->data;
2503
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002504 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002505}
2506
Kristian Høgsberg75840622011-09-06 13:48:16 -04002507static const struct desktop_shell_interface desktop_shell_implementation = {
2508 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002509 desktop_shell_set_panel,
2510 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002511 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002512 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002513};
2514
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002515static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002516get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002517{
2518 struct shell_surface *shsurf;
2519
2520 shsurf = get_shell_surface(surface);
2521 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002522 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002523 return shsurf->type;
2524}
2525
Kristian Høgsberg75840622011-09-06 13:48:16 -04002526static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002527move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002528{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002529 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002530 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002531 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002532 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002533
Pekka Paalanen01388e22013-04-25 13:57:44 +03002534 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002535 if (surface == NULL)
2536 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002537
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002538 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002539 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2540 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002541 return;
2542
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002543 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002544}
2545
2546static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002547resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002548{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002549 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002550 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002551 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002552 uint32_t edges = 0;
2553 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002554 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002555
Pekka Paalanen01388e22013-04-25 13:57:44 +03002556 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002557 if (surface == NULL)
2558 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002559
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002560 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002561 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2562 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002563 return;
2564
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002565 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002566 wl_fixed_to_int(seat->pointer->grab_x),
2567 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002568 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002569
Pekka Paalanen60921e52012-01-25 15:55:43 +02002570 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002571 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002572 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002573 edges |= 0;
2574 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002575 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002576
Pekka Paalanen60921e52012-01-25 15:55:43 +02002577 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002578 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002579 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002580 edges |= 0;
2581 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002582 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002583
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002584 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002585}
2586
2587static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002588surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002589 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002590{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002591 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002592 struct shell_surface *shsurf;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002593 struct weston_surface *focus =
Daniel Stone37816df2012-05-16 18:45:18 +01002594 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002595 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002596
Pekka Paalanen01388e22013-04-25 13:57:44 +03002597 /* XXX: broken for windows containing sub-surfaces */
2598 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002599 if (surface == NULL)
2600 return;
2601
2602 shsurf = get_shell_surface(surface);
2603 if (!shsurf)
2604 return;
2605
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002606 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002607
Scott Moreau02709af2012-05-22 01:54:10 -06002608 if (surface->alpha > 1.0)
2609 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002610 if (surface->alpha < step)
2611 surface->alpha = step;
2612
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002613 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002614 weston_surface_damage(surface);
2615}
2616
2617static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002618do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002619 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002620{
Daniel Stone37816df2012-05-16 18:45:18 +01002621 struct weston_seat *ws = (struct weston_seat *) seat;
2622 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002623 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002624 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002625
2626 wl_list_for_each(output, &compositor->output_list, link) {
2627 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002628 wl_fixed_to_double(seat->pointer->x),
2629 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002630 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002631 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002632 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002633 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002634 increment = -output->zoom.increment;
2635 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002636 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002637 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002638 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002639 else
2640 increment = 0;
2641
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002642 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002643
Scott Moreaue6603982012-06-11 13:07:51 -06002644 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002645 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002646 else if (output->zoom.level > output->zoom.max_level)
2647 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002648 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002649 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002650 output->disable_planes++;
2651 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002652
Scott Moreaue6603982012-06-11 13:07:51 -06002653 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002654
Scott Moreau8dacaab2012-06-17 18:10:58 -06002655 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002656 }
2657 }
2658}
2659
Scott Moreauccbf29d2012-02-22 14:21:41 -07002660static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002661zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002662 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002663{
2664 do_zoom(seat, time, 0, axis, value);
2665}
2666
2667static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002668zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002669 void *data)
2670{
2671 do_zoom(seat, time, key, 0, 0);
2672}
2673
2674static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002675terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002676 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002677{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002678 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002679
Daniel Stone325fc2d2012-05-30 16:31:58 +01002680 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002681}
2682
2683static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002684rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002685{
2686 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002687 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002688 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002689 struct shell_surface *shsurf = rotate->base.shsurf;
2690 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002691 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002692
2693 if (!shsurf)
2694 return;
2695
2696 surface = shsurf->surface;
2697
2698 cx = 0.5f * surface->geometry.width;
2699 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002700
Daniel Stone37816df2012-05-16 18:45:18 +01002701 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2702 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002703 r = sqrtf(dx * dx + dy * dy);
2704
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002705 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002706 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002707
2708 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002709 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002710 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002711
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002712 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002713 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002714
2715 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002716 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002717 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002718 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002719 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002720
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002721 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002722 &shsurf->surface->geometry.transformation_list,
2723 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002724 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002725 wl_list_init(&shsurf->rotation.transform.link);
2726 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002727 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002728 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002729
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002730 /* We need to adjust the position of the surface
2731 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002732 cposx = surface->geometry.x + cx;
2733 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002734 dposx = rotate->center.x - cposx;
2735 dposy = rotate->center.y - cposy;
2736 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002737 weston_surface_set_position(surface,
2738 surface->geometry.x + dposx,
2739 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002740 }
2741
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002742 /* Repaint implies weston_surface_update_transform(), which
2743 * lazily applies the damage due to rotation update.
2744 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002745 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002746}
2747
2748static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002749rotate_grab_button(struct weston_pointer_grab *grab,
2750 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002751{
2752 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002753 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002754 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002755 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002756 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002757
Daniel Stone4dbadb12012-05-30 16:31:51 +01002758 if (pointer->button_count == 0 &&
2759 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002760 if (shsurf)
2761 weston_matrix_multiply(&shsurf->rotation.rotation,
2762 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002763 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002764 free(rotate);
2765 }
2766}
2767
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002768static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002769 noop_grab_focus,
2770 rotate_grab_motion,
2771 rotate_grab_button,
2772};
2773
2774static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002775surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002776{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002777 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002778 float dx, dy;
2779 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002780
Pekka Paalanen460099f2012-01-20 16:48:25 +02002781 rotate = malloc(sizeof *rotate);
2782 if (!rotate)
2783 return;
2784
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002785 weston_surface_to_global_float(surface->surface,
2786 surface->surface->geometry.width / 2,
2787 surface->surface->geometry.height / 2,
2788 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002789
Daniel Stone37816df2012-05-16 18:45:18 +01002790 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2791 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002792 r = sqrtf(dx * dx + dy * dy);
2793 if (r > 20.0f) {
2794 struct weston_matrix inverse;
2795
2796 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002797 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002798 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002799
2800 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002801 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002802 } else {
2803 weston_matrix_init(&surface->rotation.rotation);
2804 weston_matrix_init(&rotate->rotation);
2805 }
2806
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002807 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2808 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002809}
2810
2811static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002812rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002813 void *data)
2814{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002815 struct weston_surface *focus =
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002816 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002817 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002818 struct shell_surface *surface;
2819
Pekka Paalanen01388e22013-04-25 13:57:44 +03002820 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002821 if (base_surface == NULL)
2822 return;
2823
2824 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002825 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2826 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002827 return;
2828
2829 surface_rotate(surface, seat);
2830}
2831
2832static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002833lower_fullscreen_layer(struct desktop_shell *shell)
2834{
2835 struct workspace *ws;
2836 struct weston_surface *surface, *prev;
2837
2838 ws = get_current_workspace(shell);
2839 wl_list_for_each_reverse_safe(surface, prev,
2840 &shell->fullscreen_layer.surface_list,
2841 layer_link)
2842 weston_surface_restack(surface, &ws->layer.surface_list);
2843}
2844
2845static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002846activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002847 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002848{
Pekka Paalanen01388e22013-04-25 13:57:44 +03002849 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002850 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002851 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002852
Pekka Paalanen01388e22013-04-25 13:57:44 +03002853 main_surface = weston_surface_get_main_surface(es);
2854
Daniel Stone37816df2012-05-16 18:45:18 +01002855 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002856
Jonas Ådahl8538b222012-08-29 22:13:03 +02002857 state = ensure_focus_state(shell, seat);
2858 if (state == NULL)
2859 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002860
2861 state->keyboard_focus = es;
2862 wl_list_remove(&state->surface_destroy_listener.link);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002863 wl_signal_add(&es->resource.destroy_signal,
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002864 &state->surface_destroy_listener);
2865
Pekka Paalanen01388e22013-04-25 13:57:44 +03002866 switch (get_shell_surface_type(main_surface)) {
Alex Wu4539b082012-03-01 12:57:46 +08002867 case SHELL_SURFACE_FULLSCREEN:
2868 /* should on top of panels */
Pekka Paalanen01388e22013-04-25 13:57:44 +03002869 shell_stack_fullscreen(get_shell_surface(main_surface));
2870 shell_configure_fullscreen(get_shell_surface(main_surface));
Alex Wu4539b082012-03-01 12:57:46 +08002871 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002872 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002873 ws = get_current_workspace(shell);
Pekka Paalanen01388e22013-04-25 13:57:44 +03002874 weston_surface_restack(main_surface, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002875 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002876 }
2877}
2878
Alex Wu21858432012-04-01 20:13:08 +08002879/* no-op func for checking black surface */
2880static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002881black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Alex Wu21858432012-04-01 20:13:08 +08002882{
2883}
2884
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002885static bool
Alex Wu21858432012-04-01 20:13:08 +08002886is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2887{
2888 if (es->configure == black_surface_configure) {
2889 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002890 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002891 return true;
2892 }
2893 return false;
2894}
2895
Kristian Høgsberg75840622011-09-06 13:48:16 -04002896static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002897click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002898 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002899{
Daniel Stone37816df2012-05-16 18:45:18 +01002900 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002901 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002902 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03002903 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002904
Daniel Stone37816df2012-05-16 18:45:18 +01002905 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002906 if (!focus)
2907 return;
2908
Pekka Paalanen01388e22013-04-25 13:57:44 +03002909 if (is_black_surface(focus, &main_surface))
2910 focus = main_surface;
Alex Wu4539b082012-03-01 12:57:46 +08002911
Pekka Paalanen01388e22013-04-25 13:57:44 +03002912 main_surface = weston_surface_get_main_surface(focus);
2913 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002914 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002915
Daniel Stone325fc2d2012-05-30 16:31:58 +01002916 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002917 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002918}
2919
2920static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002921lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002922{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002923 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002924
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002925 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002926 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002927 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002928 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002929
2930 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002931
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002932 /* Hide all surfaces by removing the fullscreen, panel and
2933 * toplevel layers. This way nothing else can show or receive
2934 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002935
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002936 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002937 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002938 if (shell->showing_input_panels)
2939 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002940 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002941 wl_list_insert(&shell->compositor->cursor_layer.link,
2942 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002943
Pekka Paalanen77346a62011-11-30 16:26:35 +02002944 launch_screensaver(shell);
2945
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002946 /* TODO: disable bindings that should not work while locked. */
2947
2948 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002949}
2950
2951static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002952unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002953{
Pekka Paalanend81c2162011-11-16 13:47:34 +02002954 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002955 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002956 return;
2957 }
2958
2959 /* If desktop-shell client has gone away, unlock immediately. */
2960 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002961 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002962 return;
2963 }
2964
2965 if (shell->prepare_event_sent)
2966 return;
2967
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002968 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002969 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002970}
2971
2972static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002973shell_fade_done(struct weston_surface_animation *animation, void *data)
2974{
2975 struct desktop_shell *shell = data;
2976
2977 shell->fade.animation = NULL;
2978
2979 switch (shell->fade.type) {
2980 case FADE_IN:
2981 weston_surface_destroy(shell->fade.surface);
2982 shell->fade.surface = NULL;
2983 break;
2984 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002985 lock(shell);
2986 break;
2987 }
2988}
2989
2990static void
2991shell_fade(struct desktop_shell *shell, enum fade_type type)
2992{
2993 struct weston_compositor *compositor = shell->compositor;
2994 struct weston_surface *surface;
2995 float tint;
2996
2997 switch (type) {
2998 case FADE_IN:
2999 tint = 0.0;
3000 break;
3001 case FADE_OUT:
3002 tint = 1.0;
3003 break;
3004 default:
3005 weston_log("shell: invalid fade type\n");
3006 return;
3007 }
3008
3009 shell->fade.type = type;
3010
3011 if (shell->fade.surface == NULL) {
3012 surface = weston_surface_create(compositor);
3013 if (!surface)
3014 return;
3015
3016 weston_surface_configure(surface, 0, 0, 8192, 8192);
3017 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3018 surface->alpha = 1.0 - tint;
3019 wl_list_insert(&compositor->fade_layer.surface_list,
3020 &surface->layer_link);
3021 weston_surface_update_transform(surface);
3022 shell->fade.surface = surface;
3023 pixman_region32_init(&surface->input);
3024 }
3025
3026 if (shell->fade.animation)
3027 weston_fade_update(shell->fade.animation,
3028 shell->fade.surface->alpha, tint, 30.0);
3029 else
3030 shell->fade.animation =
3031 weston_fade_run(shell->fade.surface,
3032 1.0 - tint, tint, 30.0,
3033 shell_fade_done, shell);
3034}
3035
3036static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003037idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003038{
3039 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003040 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003041
3042 shell_fade(shell, FADE_OUT);
3043 /* lock() is called from shell_fade_done() */
3044}
3045
3046static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003047wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003048{
3049 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003050 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003051
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003052 unlock(shell);
3053}
3054
3055static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003056show_input_panels(struct wl_listener *listener, void *data)
3057{
3058 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003059 container_of(listener, struct desktop_shell,
3060 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003061 struct input_panel_surface *surface, *next;
3062 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003063
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003064 shell->text_input.surface = (struct weston_surface*)data;
3065
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003066 if (shell->showing_input_panels)
3067 return;
3068
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003069 shell->showing_input_panels = true;
3070
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003071 if (!shell->locked)
3072 wl_list_insert(&shell->panel_layer.link,
3073 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003074
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003075 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003076 &shell->input_panel.surfaces, link) {
3077 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003078 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003079 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003080 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003081 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003082 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003083 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003084 weston_surface_damage(ws);
3085 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003086 }
3087}
3088
3089static void
3090hide_input_panels(struct wl_listener *listener, void *data)
3091{
3092 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003093 container_of(listener, struct desktop_shell,
3094 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003095 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003096
Jan Arne Petersen61381972013-01-31 15:52:21 +01003097 if (!shell->showing_input_panels)
3098 return;
3099
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003100 shell->showing_input_panels = false;
3101
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003102 if (!shell->locked)
3103 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003104
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003105 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003106 &shell->input_panel_layer.surface_list, layer_link)
3107 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003108}
3109
3110static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003111update_input_panels(struct wl_listener *listener, void *data)
3112{
3113 struct desktop_shell *shell =
3114 container_of(listener, struct desktop_shell,
3115 update_input_panel_listener);
3116
3117 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3118}
3119
3120static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003121center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003122{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003123 int32_t width = weston_surface_buffer_width(surface);
3124 int32_t height = weston_surface_buffer_height(surface);
3125 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003126
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003127 x = output->x + (output->width - width) / 2;
3128 y = output->y + (output->height - height) / 2;
3129
3130 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003131}
3132
3133static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003134weston_surface_set_initial_position (struct weston_surface *surface,
3135 struct desktop_shell *shell)
3136{
3137 struct weston_compositor *compositor = shell->compositor;
3138 int ix = 0, iy = 0;
3139 int range_x, range_y;
3140 int dx, dy, x, y, panel_height;
3141 struct weston_output *output, *target_output = NULL;
3142 struct weston_seat *seat;
3143
3144 /* As a heuristic place the new window on the same output as the
3145 * pointer. Falling back to the output containing 0, 0.
3146 *
3147 * TODO: Do something clever for touch too?
3148 */
3149 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003150 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003151 ix = wl_fixed_to_int(seat->pointer->x);
3152 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003153 break;
3154 }
3155 }
3156
3157 wl_list_for_each(output, &compositor->output_list, link) {
3158 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3159 target_output = output;
3160 break;
3161 }
3162 }
3163
3164 if (!target_output) {
3165 weston_surface_set_position(surface, 10 + random() % 400,
3166 10 + random() % 400);
3167 return;
3168 }
3169
3170 /* Valid range within output where the surface will still be onscreen.
3171 * If this is negative it means that the surface is bigger than
3172 * output.
3173 */
3174 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003175 range_x = target_output->width - surface->geometry.width;
3176 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003177 surface->geometry.height;
3178
Rob Bradford4cb88c72012-08-13 15:18:44 +01003179 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003180 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003181 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003182 dx = 0;
3183
3184 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003185 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003186 else
3187 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003188
3189 x = target_output->x + dx;
3190 y = target_output->y + dy;
3191
3192 weston_surface_set_position (surface, x, y);
3193}
3194
3195static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003196map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003197 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003198{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003199 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003200 struct shell_surface *shsurf = get_shell_surface(surface);
3201 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003202 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003203 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003204 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003205 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003206
Pekka Paalanen60921e52012-01-25 15:55:43 +02003207 surface->geometry.width = width;
3208 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003209 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003210
3211 /* initial positioning, see also configure() */
3212 switch (surface_type) {
3213 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003214 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003215 break;
Alex Wu4539b082012-03-01 12:57:46 +08003216 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003217 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003218 shell_map_fullscreen(shsurf);
3219 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003220 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003221 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003222 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01003223 weston_surface_set_position(surface, shsurf->output->x,
3224 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08003225 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003226 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003227 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003228 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003229 case SHELL_SURFACE_NONE:
3230 weston_surface_set_position(surface,
3231 surface->geometry.x + sx,
3232 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003233 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003234 default:
3235 ;
3236 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003237
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003238 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003239 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003240 case SHELL_SURFACE_POPUP:
3241 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003242 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003243 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3244 break;
Alex Wu4539b082012-03-01 12:57:46 +08003245 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003246 case SHELL_SURFACE_NONE:
3247 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003248 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003249 ws = get_current_workspace(shell);
3250 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003251 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003252 }
3253
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003254 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003255 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003256 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3257 surface->output = shsurf->output;
3258 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003259
Juan Zhao7bb92f02011-12-15 11:31:51 -05003260 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05003261 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003262 if (shsurf->transient.flags ==
3263 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3264 break;
3265 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003266 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003267 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003268 if (!shell->locked) {
3269 wl_list_for_each(seat, &compositor->seat_list, link)
3270 activate(shell, surface, seat);
3271 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003272 break;
3273 default:
3274 break;
3275 }
3276
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003277 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003278 {
3279 switch (shell->win_animation_type) {
3280 case ANIMATION_FADE:
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +02003281 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003282 break;
3283 case ANIMATION_ZOOM:
3284 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3285 break;
3286 default:
3287 break;
3288 }
3289 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003290}
3291
3292static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003293configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003294 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003295{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003296 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3297 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003298
Pekka Paalanen77346a62011-11-30 16:26:35 +02003299 shsurf = get_shell_surface(surface);
3300 if (shsurf)
3301 surface_type = shsurf->type;
3302
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003303 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003304
3305 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003306 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003307 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003308 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003309 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003310 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003311 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003312 surface->geometry.x = surface->output->x;
3313 surface->geometry.y = surface->output->y +
3314 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003315 break;
Alex Wu4539b082012-03-01 12:57:46 +08003316 case SHELL_SURFACE_TOPLEVEL:
3317 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003318 default:
3319 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003320 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003321
Alex Wu4539b082012-03-01 12:57:46 +08003322 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003323 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003324 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003325
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003326 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003327 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003328 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003329}
3330
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003331static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003332shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003333{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003334 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003335 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003336
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003337 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003338
Giulio Camuffo5085a752013-03-25 21:42:45 +01003339 if (!weston_surface_is_mapped(es) && !wl_list_empty(&shsurf->popup.grab_link)) {
3340 remove_popup_grab(shsurf);
3341 }
3342
Giulio Camuffo184df502013-02-21 11:29:21 +01003343 if (width == 0)
3344 return;
3345
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003346 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003347 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003348 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003349 type_changed = 1;
3350 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003351
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003352 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003353 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003354 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003355 es->geometry.width != width ||
3356 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003357 float from_x, from_y;
3358 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003359
3360 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3361 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3362 configure(shell, es,
3363 es->geometry.x + to_x - from_x,
3364 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003365 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003366 }
3367}
3368
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003369static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003370
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003371static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003372desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003373{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003374 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003375 struct desktop_shell *shell =
3376 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003377
3378 shell->child.process.pid = 0;
3379 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003380
3381 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3382 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003383 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003384 shell->child.deathstamp = time;
3385 shell->child.deathcount = 0;
3386 }
3387
3388 shell->child.deathcount++;
3389 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003390 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003391 return;
3392 }
3393
Martin Minarik6d118362012-06-07 18:01:59 +02003394 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003395 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003396}
3397
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003398static void
3399launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003400{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003401 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003402 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003403
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003404 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003405 &shell->child.process,
3406 shell_exe,
3407 desktop_shell_sigchld);
3408
3409 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003410 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003411}
3412
3413static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003414bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3415{
Tiago Vignattibe143262012-04-16 17:31:41 +03003416 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003417
3418 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003419 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003420}
3421
Kristian Høgsberg75840622011-09-06 13:48:16 -04003422static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003423unbind_desktop_shell(struct wl_resource *resource)
3424{
Tiago Vignattibe143262012-04-16 17:31:41 +03003425 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003426
3427 if (shell->locked)
3428 resume_desktop(shell);
3429
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003430 shell->child.desktop_shell = NULL;
3431 shell->prepare_event_sent = false;
3432 free(resource);
3433}
3434
3435static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003436bind_desktop_shell(struct wl_client *client,
3437 void *data, uint32_t version, uint32_t id)
3438{
Tiago Vignattibe143262012-04-16 17:31:41 +03003439 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003440 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003441
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003442 resource = wl_client_add_object(client, &desktop_shell_interface,
3443 &desktop_shell_implementation,
3444 id, shell);
3445
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003446 if (client == shell->child.client) {
3447 resource->destroy = unbind_desktop_shell;
3448 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003449 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003450 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003451
3452 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3453 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003454 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003455}
3456
Pekka Paalanen6e168112011-11-24 11:34:05 +02003457static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003458screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003459{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003460 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003461
Giulio Camuffo184df502013-02-21 11:29:21 +01003462 if (width == 0)
3463 return;
3464
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003465 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003466 if (!shell->locked)
3467 return;
3468
3469 center_on_output(surface, surface->output);
3470
3471 if (wl_list_empty(&surface->layer_link)) {
3472 wl_list_insert(shell->lock_layer.surface_list.prev,
3473 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003474 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003475 wl_event_source_timer_update(shell->screensaver.timer,
3476 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003477 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003478 }
3479}
3480
3481static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003482screensaver_set_surface(struct wl_client *client,
3483 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003484 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003485 struct wl_resource *output_resource)
3486{
Tiago Vignattibe143262012-04-16 17:31:41 +03003487 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003488 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003489 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003490
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003491 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003492 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003493 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003494}
3495
3496static const struct screensaver_interface screensaver_implementation = {
3497 screensaver_set_surface
3498};
3499
3500static void
3501unbind_screensaver(struct wl_resource *resource)
3502{
Tiago Vignattibe143262012-04-16 17:31:41 +03003503 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003504
Pekka Paalanen77346a62011-11-30 16:26:35 +02003505 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003506 free(resource);
3507}
3508
3509static void
3510bind_screensaver(struct wl_client *client,
3511 void *data, uint32_t version, uint32_t id)
3512{
Tiago Vignattibe143262012-04-16 17:31:41 +03003513 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003514 struct wl_resource *resource;
3515
3516 resource = wl_client_add_object(client, &screensaver_interface,
3517 &screensaver_implementation,
3518 id, shell);
3519
Pekka Paalanen77346a62011-11-30 16:26:35 +02003520 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003521 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003522 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003523 return;
3524 }
3525
3526 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3527 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003528 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003529}
3530
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003531static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003532input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003533{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003534 struct input_panel_surface *ip_surface = surface->configure_private;
3535 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003536 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003537 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003538 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003539
Giulio Camuffo184df502013-02-21 11:29:21 +01003540 if (width == 0)
3541 return;
3542
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003543 if (!weston_surface_is_mapped(surface)) {
3544 if (!shell->showing_input_panels)
3545 return;
3546
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003547 show_surface = 1;
3548 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003549
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003550 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003551
3552 if (ip_surface->panel) {
3553 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3554 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3555 } else {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003556 mode = ip_surface->output->current;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003557
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003558 x = ip_surface->output->x + (mode->width - width) / 2;
3559 y = ip_surface->output->y + mode->height - height;
3560 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003561
3562 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003563 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003564 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003565
3566 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003567 wl_list_insert(&shell->input_panel_layer.surface_list,
3568 &surface->layer_link);
3569 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003570 weston_surface_damage(surface);
3571 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3572 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003573}
3574
3575static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003576destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003577{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003578 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003579 wl_list_remove(&input_panel_surface->link);
3580
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003581 input_panel_surface->surface->configure = NULL;
3582
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003583 free(input_panel_surface);
3584}
3585
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003586static struct input_panel_surface *
3587get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003588{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003589 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003590 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003591 } else {
3592 return NULL;
3593 }
3594}
3595
3596static void
3597input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3598{
3599 struct input_panel_surface *ipsurface = container_of(listener,
3600 struct input_panel_surface,
3601 surface_destroy_listener);
3602
3603 if (ipsurface->resource.client) {
3604 wl_resource_destroy(&ipsurface->resource);
3605 } else {
3606 wl_signal_emit(&ipsurface->resource.destroy_signal,
3607 &ipsurface->resource);
3608 destroy_input_panel_surface(ipsurface);
3609 }
3610}
3611static struct input_panel_surface *
3612create_input_panel_surface(struct desktop_shell *shell,
3613 struct weston_surface *surface)
3614{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003615 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003616
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003617 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3618 if (!input_panel_surface)
3619 return NULL;
3620
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003621 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003622 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003623
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003624 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003625
3626 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003627
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003628 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3629 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04003630 wl_signal_add(&surface->resource.destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003631 &input_panel_surface->surface_destroy_listener);
3632
3633 wl_list_init(&input_panel_surface->link);
3634
3635 return input_panel_surface;
3636}
3637
3638static void
3639input_panel_surface_set_toplevel(struct wl_client *client,
3640 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003641 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003642 uint32_t position)
3643{
3644 struct input_panel_surface *input_panel_surface = resource->data;
3645 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003646
3647 wl_list_insert(&shell->input_panel.surfaces,
3648 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003649
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003650 input_panel_surface->output = output_resource->data;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003651 input_panel_surface->panel = 0;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003652
3653 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__,
3654 input_panel_surface->panel,
3655 input_panel_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003656}
3657
3658static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003659input_panel_surface_set_overlay_panel(struct wl_client *client,
3660 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003661{
3662 struct input_panel_surface *input_panel_surface = resource->data;
3663 struct desktop_shell *shell = input_panel_surface->shell;
3664
3665 wl_list_insert(&shell->input_panel.surfaces,
3666 &input_panel_surface->link);
3667
3668 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003669}
3670
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003671static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003672 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003673 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003674};
3675
3676static void
3677destroy_input_panel_surface_resource(struct wl_resource *resource)
3678{
3679 struct input_panel_surface *ipsurf = resource->data;
3680
3681 destroy_input_panel_surface(ipsurf);
3682}
3683
3684static void
3685input_panel_get_input_panel_surface(struct wl_client *client,
3686 struct wl_resource *resource,
3687 uint32_t id,
3688 struct wl_resource *surface_resource)
3689{
3690 struct weston_surface *surface = surface_resource->data;
3691 struct desktop_shell *shell = resource->data;
3692 struct input_panel_surface *ipsurf;
3693
3694 if (get_input_panel_surface(surface)) {
3695 wl_resource_post_error(surface_resource,
3696 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003697 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003698 return;
3699 }
3700
3701 ipsurf = create_input_panel_surface(shell, surface);
3702 if (!ipsurf) {
3703 wl_resource_post_error(surface_resource,
3704 WL_DISPLAY_ERROR_INVALID_OBJECT,
3705 "surface->configure already set");
3706 return;
3707 }
3708
3709 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3710 ipsurf->resource.object.id = id;
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003711 ipsurf->resource.object.interface = &wl_input_panel_surface_interface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003712 ipsurf->resource.object.implementation =
3713 (void (**)(void)) &input_panel_surface_implementation;
3714 ipsurf->resource.data = ipsurf;
3715
3716 wl_client_add_resource(client, &ipsurf->resource);
3717}
3718
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003719static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003720 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003721};
3722
3723static void
3724unbind_input_panel(struct wl_resource *resource)
3725{
3726 struct desktop_shell *shell = resource->data;
3727
3728 shell->input_panel.binding = NULL;
3729 free(resource);
3730}
3731
3732static void
3733bind_input_panel(struct wl_client *client,
3734 void *data, uint32_t version, uint32_t id)
3735{
3736 struct desktop_shell *shell = data;
3737 struct wl_resource *resource;
3738
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003739 resource = wl_client_add_object(client, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003740 &input_panel_implementation,
3741 id, shell);
3742
3743 if (shell->input_panel.binding == NULL) {
3744 resource->destroy = unbind_input_panel;
3745 shell->input_panel.binding = resource;
3746 return;
3747 }
3748
3749 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3750 "interface object already bound");
3751 wl_resource_destroy(resource);
3752}
3753
Kristian Høgsberg07045392012-02-19 18:52:44 -05003754struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003755 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003756 struct weston_surface *current;
3757 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003758 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003759};
3760
3761static void
3762switcher_next(struct switcher *switcher)
3763{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003764 struct weston_surface *surface;
3765 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003766 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003767 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003768
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003769 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003770 switch (get_shell_surface_type(surface)) {
3771 case SHELL_SURFACE_TOPLEVEL:
3772 case SHELL_SURFACE_FULLSCREEN:
3773 case SHELL_SURFACE_MAXIMIZED:
3774 if (first == NULL)
3775 first = surface;
3776 if (prev == switcher->current)
3777 next = surface;
3778 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003779 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003780 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003781 weston_surface_damage(surface);
3782 break;
3783 default:
3784 break;
3785 }
Alex Wu1659daa2012-04-01 20:13:09 +08003786
3787 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003788 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003789 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003790 weston_surface_damage(surface);
3791 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003792 }
3793
3794 if (next == NULL)
3795 next = first;
3796
Alex Wu07b26062012-03-12 16:06:01 +08003797 if (next == NULL)
3798 return;
3799
Kristian Høgsberg07045392012-02-19 18:52:44 -05003800 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04003801 wl_signal_add(&next->resource.destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003802
3803 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003804 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003805
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003806 shsurf = get_shell_surface(switcher->current);
3807 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003808 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003809}
3810
3811static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003812switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003813{
3814 struct switcher *switcher =
3815 container_of(listener, struct switcher, listener);
3816
3817 switcher_next(switcher);
3818}
3819
3820static void
Daniel Stone351eb612012-05-31 15:27:47 -04003821switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003822{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003823 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003824 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003825 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003826
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003827 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003828 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003829 weston_surface_damage(surface);
3830 }
3831
Alex Wu07b26062012-03-12 16:06:01 +08003832 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003833 activate(switcher->shell, switcher->current,
3834 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003835 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003836 weston_keyboard_end_grab(keyboard);
3837 if (keyboard->input_method_resource)
3838 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003839 free(switcher);
3840}
3841
3842static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003843switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003844 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003845{
3846 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003847 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003848
Daniel Stonec9785ea2012-05-30 16:31:52 +01003849 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003850 switcher_next(switcher);
3851}
3852
3853static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003854switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04003855 uint32_t mods_depressed, uint32_t mods_latched,
3856 uint32_t mods_locked, uint32_t group)
3857{
3858 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003859 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003860
Daniel Stone351eb612012-05-31 15:27:47 -04003861 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3862 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003863}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003864
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003865static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003866 switcher_key,
3867 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003868};
3869
3870static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003871switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003872 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003873{
Tiago Vignattibe143262012-04-16 17:31:41 +03003874 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003875 struct switcher *switcher;
3876
3877 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003878 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003879 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003880 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003881 wl_list_init(&switcher->listener.link);
3882
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003883 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003884 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003885 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
3886 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003887 switcher_next(switcher);
3888}
3889
Pekka Paalanen3c647232011-12-22 13:43:43 +02003890static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003891backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003892 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003893{
3894 struct weston_compositor *compositor = data;
3895 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003896 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003897
3898 /* TODO: we're limiting to simple use cases, where we assume just
3899 * control on the primary display. We'd have to extend later if we
3900 * ever get support for setting backlights on random desktop LCD
3901 * panels though */
3902 output = get_default_output(compositor);
3903 if (!output)
3904 return;
3905
3906 if (!output->set_backlight)
3907 return;
3908
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003909 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3910 backlight_new = output->backlight_current - 25;
3911 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3912 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003913
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003914 if (backlight_new < 5)
3915 backlight_new = 5;
3916 if (backlight_new > 255)
3917 backlight_new = 255;
3918
3919 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003920 output->set_backlight(output, output->backlight_current);
3921}
3922
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003923struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003924 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003925 struct weston_seat *seat;
3926 uint32_t key[2];
3927 int key_released[2];
3928};
3929
3930static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003931debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003932 uint32_t key, uint32_t state)
3933{
3934 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3935 struct wl_resource *resource;
3936 struct wl_display *display;
3937 uint32_t serial;
3938 int send = 0, terminate = 0;
3939 int check_binding = 1;
3940 int i;
3941
3942 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3943 /* Do not run bindings on key releases */
3944 check_binding = 0;
3945
3946 for (i = 0; i < 2; i++)
3947 if (key == db->key[i])
3948 db->key_released[i] = 1;
3949
3950 if (db->key_released[0] && db->key_released[1]) {
3951 /* All key releases been swalled so end the grab */
3952 terminate = 1;
3953 } else if (key != db->key[0] && key != db->key[1]) {
3954 /* Should not swallow release of other keys */
3955 send = 1;
3956 }
3957 } else if (key == db->key[0] && !db->key_released[0]) {
3958 /* Do not check bindings for the first press of the binding
3959 * key. This allows it to be used as a debug shortcut.
3960 * We still need to swallow this event. */
3961 check_binding = 0;
3962 } else if (db->key[1]) {
3963 /* If we already ran a binding don't process another one since
3964 * we can't keep track of all the binding keys that were
3965 * pressed in order to swallow the release events. */
3966 send = 1;
3967 check_binding = 0;
3968 }
3969
3970 if (check_binding) {
3971 struct weston_compositor *ec = db->seat->compositor;
3972
3973 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3974 key, state)) {
3975 /* We ran a binding so swallow the press and keep the
3976 * grab to swallow the released too. */
3977 send = 0;
3978 terminate = 0;
3979 db->key[1] = key;
3980 } else {
3981 /* Terminate the grab since the key pressed is not a
3982 * debug binding key. */
3983 send = 1;
3984 terminate = 1;
3985 }
3986 }
3987
3988 if (send) {
3989 resource = grab->keyboard->focus_resource;
3990
3991 if (resource) {
3992 display = wl_client_get_display(resource->client);
3993 serial = wl_display_next_serial(display);
3994 wl_keyboard_send_key(resource, serial, time, key, state);
3995 }
3996 }
3997
3998 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003999 weston_keyboard_end_grab(grab->keyboard);
4000 if (grab->keyboard->input_method_resource)
4001 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004002 free(db);
4003 }
4004}
4005
4006static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004007debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004008 uint32_t mods_depressed, uint32_t mods_latched,
4009 uint32_t mods_locked, uint32_t group)
4010{
4011 struct wl_resource *resource;
4012
4013 resource = grab->keyboard->focus_resource;
4014 if (!resource)
4015 return;
4016
4017 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4018 mods_latched, mods_locked, group);
4019}
4020
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004021struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004022 debug_binding_key,
4023 debug_binding_modifiers
4024};
4025
4026static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004027debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004028{
4029 struct debug_binding_grab *grab;
4030
4031 grab = calloc(1, sizeof *grab);
4032 if (!grab)
4033 return;
4034
4035 grab->seat = (struct weston_seat *) seat;
4036 grab->key[0] = key;
4037 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004038 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004039}
4040
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004041static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004042force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004043 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004044{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004045 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004046 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004047 struct desktop_shell *shell = data;
4048 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004049 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004050
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004051 focus_surface = seat->keyboard->focus;
4052 if (!focus_surface)
4053 return;
4054
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004055 wl_signal_emit(&compositor->kill_signal, focus_surface);
4056
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004057 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03004058 wl_client_get_credentials(client, &pid, NULL, NULL);
4059
4060 /* Skip clients that we launched ourselves (the credentials of
4061 * the socketpair is ours) */
4062 if (pid == getpid())
4063 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004064
Daniel Stone325fc2d2012-05-30 16:31:58 +01004065 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004066}
4067
4068static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004069workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004070 uint32_t key, void *data)
4071{
4072 struct desktop_shell *shell = data;
4073 unsigned int new_index = shell->workspaces.current;
4074
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004075 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004076 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004077 if (new_index != 0)
4078 new_index--;
4079
4080 change_workspace(shell, new_index);
4081}
4082
4083static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004084workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004085 uint32_t key, void *data)
4086{
4087 struct desktop_shell *shell = data;
4088 unsigned int new_index = shell->workspaces.current;
4089
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004090 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004091 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004092 if (new_index < shell->workspaces.num - 1)
4093 new_index++;
4094
4095 change_workspace(shell, new_index);
4096}
4097
4098static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004099workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004100 uint32_t key, void *data)
4101{
4102 struct desktop_shell *shell = data;
4103 unsigned int new_index;
4104
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004105 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004106 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004107 new_index = key - KEY_F1;
4108 if (new_index >= shell->workspaces.num)
4109 new_index = shell->workspaces.num - 1;
4110
4111 change_workspace(shell, new_index);
4112}
4113
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004114static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004115workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004116 uint32_t key, void *data)
4117{
4118 struct desktop_shell *shell = data;
4119 unsigned int new_index = shell->workspaces.current;
4120
4121 if (shell->locked)
4122 return;
4123
4124 if (new_index != 0)
4125 new_index--;
4126
4127 take_surface_to_workspace_by_seat(shell, seat, new_index);
4128}
4129
4130static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004131workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004132 uint32_t key, void *data)
4133{
4134 struct desktop_shell *shell = data;
4135 unsigned int new_index = shell->workspaces.current;
4136
4137 if (shell->locked)
4138 return;
4139
4140 if (new_index < shell->workspaces.num - 1)
4141 new_index++;
4142
4143 take_surface_to_workspace_by_seat(shell, seat, new_index);
4144}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004145
4146static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004147shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004148{
Tiago Vignattibe143262012-04-16 17:31:41 +03004149 struct desktop_shell *shell =
4150 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004151 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004152
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004153 if (shell->child.client)
4154 wl_client_destroy(shell->child.client);
4155
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004156 wl_list_remove(&shell->idle_listener.link);
4157 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004158 wl_list_remove(&shell->show_input_panel_listener.link);
4159 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004160
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004161 wl_array_for_each(ws, &shell->workspaces.array)
4162 workspace_destroy(*ws);
4163 wl_array_release(&shell->workspaces.array);
4164
Pekka Paalanen3c647232011-12-22 13:43:43 +02004165 free(shell->screensaver.path);
4166 free(shell);
4167}
4168
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004169static void
4170shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4171{
4172 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004173 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004174
4175 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004176 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4177 MODIFIER_CTRL | MODIFIER_ALT,
4178 terminate_binding, ec);
4179 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4180 click_to_activate_binding,
4181 shell);
4182 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4183 MODIFIER_SUPER | MODIFIER_ALT,
4184 surface_opacity_binding, NULL);
4185 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4186 MODIFIER_SUPER, zoom_axis_binding,
4187 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004188
4189 /* configurable bindings */
4190 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004191 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4192 zoom_key_binding, NULL);
4193 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4194 zoom_key_binding, NULL);
4195 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4196 shell);
4197 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4198 resize_binding, shell);
4199 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4200 rotate_binding, NULL);
4201 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4202 shell);
4203 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4204 ec);
4205 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4206 backlight_binding, ec);
4207 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4208 ec);
4209 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4210 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004211 weston_compositor_add_key_binding(ec, KEY_K, mod,
4212 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004213 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4214 workspace_up_binding, shell);
4215 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4216 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004217 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4218 workspace_move_surface_up_binding,
4219 shell);
4220 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4221 workspace_move_surface_down_binding,
4222 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004223
4224 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4225 if (shell->workspaces.num > 1) {
4226 num_workspace_bindings = shell->workspaces.num;
4227 if (num_workspace_bindings > 6)
4228 num_workspace_bindings = 6;
4229 for (i = 0; i < num_workspace_bindings; i++)
4230 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4231 workspace_f_binding,
4232 shell);
4233 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004234
4235 /* Debug bindings */
4236 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4237 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004238}
4239
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004240WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004241module_init(struct weston_compositor *ec,
4242 int *argc, char *argv[], const char *config_file)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004243{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004244 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004245 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004246 struct workspace **pws;
4247 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004248 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004249
4250 shell = malloc(sizeof *shell);
4251 if (shell == NULL)
4252 return -1;
4253
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004254 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004255 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004256
4257 shell->destroy_listener.notify = shell_destroy;
4258 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004259 shell->idle_listener.notify = idle_handler;
4260 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4261 shell->wake_listener.notify = wake_handler;
4262 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004263 shell->show_input_panel_listener.notify = show_input_panels;
4264 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4265 shell->hide_input_panel_listener.notify = hide_input_panels;
4266 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004267 shell->update_input_panel_listener.notify = update_input_panels;
4268 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004269 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004270 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004271 ec->shell_interface.create_shell_surface = create_shell_surface;
4272 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004273 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004274 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004275 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004276 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004277
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004278 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004279
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004280 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4281 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004282 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4283 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004284 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004285
4286 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004287 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004288
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004289 shell_configuration(shell, config_file);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004290
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004291 for (i = 0; i < shell->workspaces.num; i++) {
4292 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4293 if (pws == NULL)
4294 return -1;
4295
4296 *pws = workspace_create();
4297 if (*pws == NULL)
4298 return -1;
4299 }
4300 activate_workspace(shell, 0);
4301
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004302 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004303 wl_list_init(&shell->workspaces.animation.link);
4304 shell->workspaces.animation.frame = animate_workspace_change_frame;
4305
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004306 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4307 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004308 return -1;
4309
Kristian Høgsberg75840622011-09-06 13:48:16 -04004310 if (wl_display_add_global(ec->wl_display,
4311 &desktop_shell_interface,
4312 shell, bind_desktop_shell) == NULL)
4313 return -1;
4314
Pekka Paalanen6e168112011-11-24 11:34:05 +02004315 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4316 shell, bind_screensaver) == NULL)
4317 return -1;
4318
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004319 if (wl_display_add_global(ec->wl_display, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004320 shell, bind_input_panel) == NULL)
4321 return -1;
4322
Jonas Ådahle9d22502012-08-29 22:13:01 +02004323 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4324 shell, bind_workspace_manager) == NULL)
4325 return -1;
4326
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004327 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004328
4329 loop = wl_display_get_event_loop(ec->wl_display);
4330 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004331
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004332 shell->screensaver.timer =
4333 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4334
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004335 wl_list_for_each(seat, &ec->seat_list, link)
4336 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004337
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004338 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004339
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004340 shell_fade(shell, FADE_IN);
4341
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004342 return 0;
4343}