blob: bdb8d4c24453a53c3156003791705e0553e6e6bb [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;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400327 grab->grab.focus = shsurf->surface;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300328
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400329 weston_pointer_start_grab(pointer, &grab->grab);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300330 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400331 weston_pointer_set_focus(pointer, shell->grab_surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400332 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300333}
334
335static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300336shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300337{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400338 if (grab->shsurf)
339 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300340
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400341 weston_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300342}
343
344static void
Alex Wu4539b082012-03-01 12:57:46 +0800345center_on_output(struct weston_surface *surface,
346 struct weston_output *output);
347
Daniel Stone496ca172012-05-30 16:31:42 +0100348static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300349get_modifier(char *modifier)
350{
351 if (!modifier)
352 return MODIFIER_SUPER;
353
354 if (!strcmp("ctrl", modifier))
355 return MODIFIER_CTRL;
356 else if (!strcmp("alt", modifier))
357 return MODIFIER_ALT;
358 else if (!strcmp("super", modifier))
359 return MODIFIER_SUPER;
360 else
361 return MODIFIER_SUPER;
362}
363
Juan Zhaoe10d2792012-04-25 19:09:52 +0800364static enum animation_type
365get_animation_type(char *animation)
366{
367 if (!animation)
368 return ANIMATION_NONE;
369
370 if (!strcmp("zoom", animation))
371 return ANIMATION_ZOOM;
372 else if (!strcmp("fade", animation))
373 return ANIMATION_FADE;
374 else
375 return ANIMATION_NONE;
376}
377
Alex Wu4539b082012-03-01 12:57:46 +0800378static void
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500379shell_configuration(struct desktop_shell *shell, const char *config_file)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200380{
Pekka Paalanen7296e792011-12-07 16:22:00 +0200381 char *path = NULL;
382 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200383 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300384 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800385 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200386
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400387 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300388 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800389 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200390 { "num-workspaces",
391 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200392 };
393
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400394 struct config_key saver_keys[] = {
395 { "path", CONFIG_KEY_STRING, &path },
396 { "duration", CONFIG_KEY_INTEGER, &duration },
397 };
398
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200399 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400400 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200401 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
402 };
403
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500404 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200405
Pekka Paalanen7296e792011-12-07 16:22:00 +0200406 shell->screensaver.path = path;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200407 shell->screensaver.duration = duration * 1000;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300408 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800409 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200410 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
411}
412
413static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200414focus_state_destroy(struct focus_state *state)
415{
416 wl_list_remove(&state->seat_destroy_listener.link);
417 wl_list_remove(&state->surface_destroy_listener.link);
418 free(state);
419}
420
421static void
422focus_state_seat_destroy(struct wl_listener *listener, void *data)
423{
424 struct focus_state *state = container_of(listener,
425 struct focus_state,
426 seat_destroy_listener);
427
428 wl_list_remove(&state->link);
429 focus_state_destroy(state);
430}
431
432static void
433focus_state_surface_destroy(struct wl_listener *listener, void *data)
434{
435 struct focus_state *state = container_of(listener,
436 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400437 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400438 struct desktop_shell *shell;
439 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200440
Kristian Høgsberge3778222012-07-31 17:29:30 -0400441 next = NULL;
442 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
443 if (surface == state->keyboard_focus)
444 continue;
445
446 next = surface;
447 break;
448 }
449
450 if (next) {
451 shell = state->seat->compositor->shell_interface.shell;
452 activate(shell, next, state->seat);
453 } else {
454 wl_list_remove(&state->link);
455 focus_state_destroy(state);
456 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200457}
458
459static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400460focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200461{
Jonas Ådahl04769742012-06-13 00:01:24 +0200462 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200463
464 state = malloc(sizeof *state);
465 if (state == NULL)
466 return NULL;
467
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400468 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200469 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400470 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200471
472 state->seat_destroy_listener.notify = focus_state_seat_destroy;
473 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400474 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200475 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400476 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200477
478 return state;
479}
480
Jonas Ådahl8538b222012-08-29 22:13:03 +0200481static struct focus_state *
482ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
483{
484 struct workspace *ws = get_current_workspace(shell);
485 struct focus_state *state;
486
487 wl_list_for_each(state, &ws->focus_list, link)
488 if (state->seat == seat)
489 break;
490
491 if (&state->link == &ws->focus_list)
492 state = focus_state_create(seat, ws);
493
494 return state;
495}
496
Jonas Ådahl04769742012-06-13 00:01:24 +0200497static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400498restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200499{
500 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400501 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200502
503 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400504 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200505
Kristian Høgsberge3148752013-05-06 23:19:49 -0400506 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200507 }
508}
509
510static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200511replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
512 struct weston_seat *seat)
513{
514 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400515 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200516
517 wl_list_for_each(state, &ws->focus_list, link) {
518 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400519 surface = seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200520 state->keyboard_focus =
521 (struct weston_surface *) surface;
522 return;
523 }
524 }
525}
526
527static void
528drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
529 struct weston_surface *surface)
530{
531 struct focus_state *state;
532
533 wl_list_for_each(state, &ws->focus_list, link)
534 if (state->keyboard_focus == surface)
535 state->keyboard_focus = NULL;
536}
537
538static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200539workspace_destroy(struct workspace *ws)
540{
Jonas Ådahl04769742012-06-13 00:01:24 +0200541 struct focus_state *state, *next;
542
543 wl_list_for_each_safe(state, next, &ws->focus_list, link)
544 focus_state_destroy(state);
545
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200546 free(ws);
547}
548
Jonas Ådahl04769742012-06-13 00:01:24 +0200549static void
550seat_destroyed(struct wl_listener *listener, void *data)
551{
552 struct weston_seat *seat = data;
553 struct focus_state *state, *next;
554 struct workspace *ws = container_of(listener,
555 struct workspace,
556 seat_destroyed_listener);
557
558 wl_list_for_each_safe(state, next, &ws->focus_list, link)
559 if (state->seat == seat)
560 wl_list_remove(&state->link);
561}
562
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200563static struct workspace *
564workspace_create(void)
565{
566 struct workspace *ws = malloc(sizeof *ws);
567 if (ws == NULL)
568 return NULL;
569
570 weston_layer_init(&ws->layer, NULL);
571
Jonas Ådahl04769742012-06-13 00:01:24 +0200572 wl_list_init(&ws->focus_list);
573 wl_list_init(&ws->seat_destroyed_listener.link);
574 ws->seat_destroyed_listener.notify = seat_destroyed;
575
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200576 return ws;
577}
578
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200579static int
580workspace_is_empty(struct workspace *ws)
581{
582 return wl_list_empty(&ws->layer.surface_list);
583}
584
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200585static struct workspace *
586get_workspace(struct desktop_shell *shell, unsigned int index)
587{
588 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200589 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200590 pws += index;
591 return *pws;
592}
593
594static struct workspace *
595get_current_workspace(struct desktop_shell *shell)
596{
597 return get_workspace(shell, shell->workspaces.current);
598}
599
600static void
601activate_workspace(struct desktop_shell *shell, unsigned int index)
602{
603 struct workspace *ws;
604
605 ws = get_workspace(shell, index);
606 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
607
608 shell->workspaces.current = index;
609}
610
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200611static unsigned int
612get_output_height(struct weston_output *output)
613{
614 return abs(output->region.extents.y1 - output->region.extents.y2);
615}
616
617static void
618surface_translate(struct weston_surface *surface, double d)
619{
620 struct shell_surface *shsurf = get_shell_surface(surface);
621 struct weston_transform *transform;
622
623 transform = &shsurf->workspace_transform;
624 if (wl_list_empty(&transform->link))
625 wl_list_insert(surface->geometry.transformation_list.prev,
626 &shsurf->workspace_transform.link);
627
628 weston_matrix_init(&shsurf->workspace_transform.matrix);
629 weston_matrix_translate(&shsurf->workspace_transform.matrix,
630 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200631 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200632}
633
634static void
635workspace_translate_out(struct workspace *ws, double fraction)
636{
637 struct weston_surface *surface;
638 unsigned int height;
639 double d;
640
641 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
642 height = get_output_height(surface->output);
643 d = height * fraction;
644
645 surface_translate(surface, d);
646 }
647}
648
649static void
650workspace_translate_in(struct workspace *ws, double fraction)
651{
652 struct weston_surface *surface;
653 unsigned int height;
654 double d;
655
656 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
657 height = get_output_height(surface->output);
658
659 if (fraction > 0)
660 d = -(height - height * fraction);
661 else
662 d = height + height * fraction;
663
664 surface_translate(surface, d);
665 }
666}
667
668static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200669broadcast_current_workspace_state(struct desktop_shell *shell)
670{
671 struct wl_resource *resource;
672
673 wl_list_for_each(resource, &shell->workspaces.client_list, link)
674 workspace_manager_send_state(resource,
675 shell->workspaces.current,
676 shell->workspaces.num);
677}
678
679static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200680reverse_workspace_change_animation(struct desktop_shell *shell,
681 unsigned int index,
682 struct workspace *from,
683 struct workspace *to)
684{
685 shell->workspaces.current = index;
686
687 shell->workspaces.anim_to = to;
688 shell->workspaces.anim_from = from;
689 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
690 shell->workspaces.anim_timestamp = 0;
691
Scott Moreau4272e632012-08-13 09:58:41 -0600692 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200693}
694
695static void
696workspace_deactivate_transforms(struct workspace *ws)
697{
698 struct weston_surface *surface;
699 struct shell_surface *shsurf;
700
701 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
702 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200703 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
704 wl_list_remove(&shsurf->workspace_transform.link);
705 wl_list_init(&shsurf->workspace_transform.link);
706 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200707 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200708 }
709}
710
711static void
712finish_workspace_change_animation(struct desktop_shell *shell,
713 struct workspace *from,
714 struct workspace *to)
715{
Scott Moreau4272e632012-08-13 09:58:41 -0600716 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200717
718 wl_list_remove(&shell->workspaces.animation.link);
719 workspace_deactivate_transforms(from);
720 workspace_deactivate_transforms(to);
721 shell->workspaces.anim_to = NULL;
722
723 wl_list_remove(&shell->workspaces.anim_from->layer.link);
724}
725
726static void
727animate_workspace_change_frame(struct weston_animation *animation,
728 struct weston_output *output, uint32_t msecs)
729{
730 struct desktop_shell *shell =
731 container_of(animation, struct desktop_shell,
732 workspaces.animation);
733 struct workspace *from = shell->workspaces.anim_from;
734 struct workspace *to = shell->workspaces.anim_to;
735 uint32_t t;
736 double x, y;
737
738 if (workspace_is_empty(from) && workspace_is_empty(to)) {
739 finish_workspace_change_animation(shell, from, to);
740 return;
741 }
742
743 if (shell->workspaces.anim_timestamp == 0) {
744 if (shell->workspaces.anim_current == 0.0)
745 shell->workspaces.anim_timestamp = msecs;
746 else
747 shell->workspaces.anim_timestamp =
748 msecs -
749 /* Invers of movement function 'y' below. */
750 (asin(1.0 - shell->workspaces.anim_current) *
751 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
752 M_2_PI);
753 }
754
755 t = msecs - shell->workspaces.anim_timestamp;
756
757 /*
758 * x = [0, π/2]
759 * y(x) = sin(x)
760 */
761 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
762 y = sin(x);
763
764 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600765 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200766
767 workspace_translate_out(from, shell->workspaces.anim_dir * y);
768 workspace_translate_in(to, shell->workspaces.anim_dir * y);
769 shell->workspaces.anim_current = y;
770
Scott Moreau4272e632012-08-13 09:58:41 -0600771 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200772 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200773 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200774 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200775}
776
777static void
778animate_workspace_change(struct desktop_shell *shell,
779 unsigned int index,
780 struct workspace *from,
781 struct workspace *to)
782{
783 struct weston_output *output;
784
785 int dir;
786
787 if (index > shell->workspaces.current)
788 dir = -1;
789 else
790 dir = 1;
791
792 shell->workspaces.current = index;
793
794 shell->workspaces.anim_dir = dir;
795 shell->workspaces.anim_from = from;
796 shell->workspaces.anim_to = to;
797 shell->workspaces.anim_current = 0.0;
798 shell->workspaces.anim_timestamp = 0;
799
800 output = container_of(shell->compositor->output_list.next,
801 struct weston_output, link);
802 wl_list_insert(&output->animation_list,
803 &shell->workspaces.animation.link);
804
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200805 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200806
807 workspace_translate_in(to, 0);
808
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400809 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200810
Scott Moreau4272e632012-08-13 09:58:41 -0600811 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200812}
813
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200814static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200815update_workspace(struct desktop_shell *shell, unsigned int index,
816 struct workspace *from, struct workspace *to)
817{
818 shell->workspaces.current = index;
819 wl_list_insert(&from->layer.link, &to->layer.link);
820 wl_list_remove(&from->layer.link);
821}
822
823static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200824change_workspace(struct desktop_shell *shell, unsigned int index)
825{
826 struct workspace *from;
827 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200828
829 if (index == shell->workspaces.current)
830 return;
831
832 /* Don't change workspace when there is any fullscreen surfaces. */
833 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
834 return;
835
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200836 from = get_current_workspace(shell);
837 to = get_workspace(shell, index);
838
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200839 if (shell->workspaces.anim_from == to &&
840 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200841 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200842 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200843 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200844 return;
845 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200846
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200847 if (shell->workspaces.anim_to != NULL)
848 finish_workspace_change_animation(shell,
849 shell->workspaces.anim_from,
850 shell->workspaces.anim_to);
851
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200852 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200853
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200854 if (workspace_is_empty(to) && workspace_is_empty(from))
855 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200856 else
857 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200858
859 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200860}
861
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200862static bool
863workspace_has_only(struct workspace *ws, struct weston_surface *surface)
864{
865 struct wl_list *list = &ws->layer.surface_list;
866 struct wl_list *e;
867
868 if (wl_list_empty(list))
869 return false;
870
871 e = list->next;
872
873 if (e->next != list)
874 return false;
875
876 return container_of(e, struct weston_surface, layer_link) == surface;
877}
878
879static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200880move_surface_to_workspace(struct desktop_shell *shell,
881 struct weston_surface *surface,
882 uint32_t workspace)
883{
884 struct workspace *from;
885 struct workspace *to;
886 struct weston_seat *seat;
887
888 if (workspace == shell->workspaces.current)
889 return;
890
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200891 if (workspace >= shell->workspaces.num)
892 workspace = shell->workspaces.num - 1;
893
Jonas Ådahle9d22502012-08-29 22:13:01 +0200894 from = get_current_workspace(shell);
895 to = get_workspace(shell, workspace);
896
897 wl_list_remove(&surface->layer_link);
898 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
899
900 drop_focus_state(shell, from, surface);
901 wl_list_for_each(seat, &shell->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400902 if (seat->keyboard && seat->keyboard->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400903 weston_keyboard_set_focus(seat->keyboard, NULL);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200904
905 weston_surface_damage_below(surface);
906}
907
908static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200909take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400910 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200911 unsigned int index)
912{
913 struct weston_surface *surface =
Kristian Høgsberge3148752013-05-06 23:19:49 -0400914 (struct weston_surface *) seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200915 struct shell_surface *shsurf;
916 struct workspace *from;
917 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200918 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200919
920 if (surface == NULL ||
921 index == shell->workspaces.current)
922 return;
923
924 from = get_current_workspace(shell);
925 to = get_workspace(shell, index);
926
927 wl_list_remove(&surface->layer_link);
928 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
929
Jonas Ådahle9d22502012-08-29 22:13:01 +0200930 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200931 drop_focus_state(shell, from, surface);
932
933 if (shell->workspaces.anim_from == to &&
934 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200935 wl_list_remove(&to->layer.link);
936 wl_list_insert(from->layer.link.prev, &to->layer.link);
937
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200938 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200939 broadcast_current_workspace_state(shell);
940
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200941 return;
942 }
943
944 if (shell->workspaces.anim_to != NULL)
945 finish_workspace_change_animation(shell,
946 shell->workspaces.anim_from,
947 shell->workspaces.anim_to);
948
949 if (workspace_is_empty(from) &&
950 workspace_has_only(to, surface))
951 update_workspace(shell, index, from, to);
952 else {
953 shsurf = get_shell_surface(surface);
954 if (wl_list_empty(&shsurf->workspace_transform.link))
955 wl_list_insert(&shell->workspaces.anim_sticky_list,
956 &shsurf->workspace_transform.link);
957
958 animate_workspace_change(shell, index, from, to);
959 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200960
961 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200962
963 state = ensure_focus_state(shell, seat);
964 if (state != NULL)
965 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200966}
967
968static void
969workspace_manager_move_surface(struct wl_client *client,
970 struct wl_resource *resource,
971 struct wl_resource *surface_resource,
972 uint32_t workspace)
973{
974 struct desktop_shell *shell = resource->data;
975 struct weston_surface *surface =
976 (struct weston_surface *) surface_resource;
977
978 move_surface_to_workspace(shell, surface, workspace);
979}
980
981static const struct workspace_manager_interface workspace_manager_implementation = {
982 workspace_manager_move_surface,
983};
984
985static void
986unbind_resource(struct wl_resource *resource)
987{
988 wl_list_remove(&resource->link);
989 free(resource);
990}
991
992static void
993bind_workspace_manager(struct wl_client *client,
994 void *data, uint32_t version, uint32_t id)
995{
996 struct desktop_shell *shell = data;
997 struct wl_resource *resource;
998
999 resource = wl_client_add_object(client, &workspace_manager_interface,
1000 &workspace_manager_implementation,
1001 id, shell);
1002
1003 if (resource == NULL) {
1004 weston_log("couldn't add workspace manager object");
1005 return;
1006 }
1007
1008 resource->destroy = unbind_resource;
1009 wl_list_insert(&shell->workspaces.client_list, &resource->link);
1010
1011 workspace_manager_send_state(resource,
1012 shell->workspaces.current,
1013 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001014}
1015
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001016static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001017noop_grab_focus(struct weston_pointer_grab *grab,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001018 struct weston_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001019{
1020 grab->focus = NULL;
1021}
1022
1023static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001024move_grab_motion(struct weston_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001025 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001026{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001027 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001028 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001029 struct shell_surface *shsurf = move->base.shsurf;
1030 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001031 int dx = wl_fixed_to_int(pointer->x + move->dx);
1032 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001033
1034 if (!shsurf)
1035 return;
1036
1037 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001038
Daniel Stone103db7f2012-05-08 17:17:55 +01001039 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001040 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001041
1042 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001043}
1044
1045static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001046move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001047 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001048{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001049 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1050 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001051 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001052 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001053
Daniel Stone4dbadb12012-05-30 16:31:51 +01001054 if (pointer->button_count == 0 &&
1055 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001056 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001057 free(grab);
1058 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001059}
1060
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001061static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001062 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001063 move_grab_motion,
1064 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001065};
1066
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001067static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001068surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001069{
1070 struct weston_move_grab *move;
1071
1072 if (!shsurf)
1073 return -1;
1074
1075 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1076 return 0;
1077
1078 move = malloc(sizeof *move);
1079 if (!move)
1080 return -1;
1081
1082 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001083 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001084 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001085 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001086
1087 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001088 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001089
1090 return 0;
1091}
1092
1093static void
1094shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1095 struct wl_resource *seat_resource, uint32_t serial)
1096{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001097 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001098 struct shell_surface *shsurf = resource->data;
1099
Kristian Høgsberge3148752013-05-06 23:19:49 -04001100 if (seat->pointer->button_count == 0 ||
1101 seat->pointer->grab_serial != serial ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001102 seat->pointer->focus != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001103 return;
1104
Kristian Høgsberge3148752013-05-06 23:19:49 -04001105 if (surface_move(shsurf, seat) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001106 wl_resource_post_no_memory(resource);
1107}
1108
1109struct weston_resize_grab {
1110 struct shell_grab base;
1111 uint32_t edges;
1112 int32_t width, height;
1113};
1114
1115static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001116resize_grab_motion(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001117 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1118{
1119 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001120 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001121 struct shell_surface *shsurf = resize->base.shsurf;
1122 int32_t width, height;
1123 wl_fixed_t from_x, from_y;
1124 wl_fixed_t to_x, to_y;
1125
1126 if (!shsurf)
1127 return;
1128
1129 weston_surface_from_global_fixed(shsurf->surface,
1130 pointer->grab_x, pointer->grab_y,
1131 &from_x, &from_y);
1132 weston_surface_from_global_fixed(shsurf->surface,
1133 pointer->x, pointer->y, &to_x, &to_y);
1134
1135 width = resize->width;
1136 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1137 width += wl_fixed_to_int(from_x - to_x);
1138 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1139 width += wl_fixed_to_int(to_x - from_x);
1140 }
1141
1142 height = resize->height;
1143 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1144 height += wl_fixed_to_int(from_y - to_y);
1145 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1146 height += wl_fixed_to_int(to_y - from_y);
1147 }
1148
1149 shsurf->client->send_configure(shsurf->surface,
1150 resize->edges, width, height);
1151}
1152
1153static void
1154send_configure(struct weston_surface *surface,
1155 uint32_t edges, int32_t width, int32_t height)
1156{
1157 struct shell_surface *shsurf = get_shell_surface(surface);
1158
1159 wl_shell_surface_send_configure(&shsurf->resource,
1160 edges, width, height);
1161}
1162
1163static const struct weston_shell_client shell_client = {
1164 send_configure
1165};
1166
1167static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001168resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001169 uint32_t time, uint32_t button, uint32_t state_w)
1170{
1171 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001172 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001173 enum wl_pointer_button_state state = state_w;
1174
1175 if (pointer->button_count == 0 &&
1176 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1177 shell_grab_end(&resize->base);
1178 free(grab);
1179 }
1180}
1181
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001182static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001183 noop_grab_focus,
1184 resize_grab_motion,
1185 resize_grab_button,
1186};
1187
1188static int
1189surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001190 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001191{
1192 struct weston_resize_grab *resize;
1193
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001194 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1195 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001196 return 0;
1197
1198 if (edges == 0 || edges > 15 ||
1199 (edges & 3) == 3 || (edges & 12) == 12)
1200 return 0;
1201
1202 resize = malloc(sizeof *resize);
1203 if (!resize)
1204 return -1;
1205
1206 resize->edges = edges;
1207 resize->width = shsurf->surface->geometry.width;
1208 resize->height = shsurf->surface->geometry.height;
1209
1210 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001211 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001212
1213 return 0;
1214}
1215
1216static void
1217shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1218 struct wl_resource *seat_resource, uint32_t serial,
1219 uint32_t edges)
1220{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001221 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001222 struct shell_surface *shsurf = resource->data;
1223
1224 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1225 return;
1226
Kristian Høgsberge3148752013-05-06 23:19:49 -04001227 if (seat->pointer->button_count == 0 ||
1228 seat->pointer->grab_serial != serial ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001229 seat->pointer->focus != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001230 return;
1231
Kristian Høgsberge3148752013-05-06 23:19:49 -04001232 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001233 wl_resource_post_no_memory(resource);
1234}
1235
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001236static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001237busy_cursor_grab_focus(struct weston_pointer_grab *base,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001238 struct weston_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001239{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001240 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001241
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001242 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001243 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001244 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001245 }
1246}
1247
1248static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001249busy_cursor_grab_motion(struct weston_pointer_grab *grab,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001250 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001251{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001252}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001253
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001254static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001255busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001256 uint32_t time, uint32_t button, uint32_t state)
1257{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001258 struct shell_grab *grab = (struct shell_grab *) base;
1259 struct shell_surface *shsurf;
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001260 struct weston_surface *surface =
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001261 (struct weston_surface *) grab->grab.pointer->current;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001262 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001263
1264 shsurf = get_shell_surface(surface);
1265 if (shsurf && button == BTN_LEFT && state) {
1266 activate(shsurf->shell, shsurf->surface, seat);
1267 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001268 } else if (shsurf && button == BTN_RIGHT && state) {
1269 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001270 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001271 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001272}
1273
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001274static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001275 busy_cursor_grab_focus,
1276 busy_cursor_grab_motion,
1277 busy_cursor_grab_button,
1278};
1279
1280static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001281set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001282{
1283 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001284
1285 grab = malloc(sizeof *grab);
1286 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001287 return;
1288
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001289 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1290 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001291}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001292
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001293static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001294end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001295{
1296 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1297
1298 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001299 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001300 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001301 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001302}
1303
Scott Moreau9521d5e2012-04-19 13:06:17 -06001304static void
1305ping_timer_destroy(struct shell_surface *shsurf)
1306{
1307 if (!shsurf || !shsurf->ping_timer)
1308 return;
1309
1310 if (shsurf->ping_timer->source)
1311 wl_event_source_remove(shsurf->ping_timer->source);
1312
1313 free(shsurf->ping_timer);
1314 shsurf->ping_timer = NULL;
1315}
1316
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001317static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001318ping_timeout_handler(void *data)
1319{
1320 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001321 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001322
Scott Moreau9521d5e2012-04-19 13:06:17 -06001323 /* Client is not responding */
1324 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001325
1326 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001327 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001328 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001329
1330 return 1;
1331}
1332
1333static void
1334ping_handler(struct weston_surface *surface, uint32_t serial)
1335{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001336 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001337 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001338 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001339
1340 if (!shsurf)
1341 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001342 if (!shsurf->resource.client)
1343 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001344
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001345 if (shsurf->surface == shsurf->shell->grab_surface)
1346 return;
1347
Scott Moreauff1db4a2012-04-17 19:06:18 -06001348 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001349 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001350 if (!shsurf->ping_timer)
1351 return;
1352
1353 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001354 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1355 shsurf->ping_timer->source =
1356 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1357 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1358
1359 wl_shell_surface_send_ping(&shsurf->resource, serial);
1360 }
1361}
1362
1363static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001364handle_pointer_focus(struct wl_listener *listener, void *data)
1365{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001366 struct weston_pointer *pointer = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001367 struct weston_surface *surface =
1368 (struct weston_surface *) pointer->focus;
1369 struct weston_compositor *compositor;
1370 struct shell_surface *shsurf;
1371 uint32_t serial;
1372
1373 if (!surface)
1374 return;
1375
1376 compositor = surface->compositor;
1377 shsurf = get_shell_surface(surface);
1378
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001379 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001380 set_busy_cursor(shsurf, pointer);
1381 } else {
1382 serial = wl_display_next_serial(compositor->wl_display);
1383 ping_handler(surface, serial);
1384 }
1385}
1386
1387static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001388create_pointer_focus_listener(struct weston_seat *seat)
1389{
1390 struct wl_listener *listener;
1391
Kristian Høgsberge3148752013-05-06 23:19:49 -04001392 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001393 return;
1394
1395 listener = malloc(sizeof *listener);
1396 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001397 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001398}
1399
1400static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001401shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1402 uint32_t serial)
1403{
1404 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001405 struct desktop_shell *shell = shsurf->shell;
1406 struct weston_seat *seat;
1407 struct weston_compositor *ec = shsurf->surface->compositor;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001408 struct weston_pointer *pointer;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001409 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001410
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001411 if (shsurf->ping_timer == NULL)
1412 /* Just ignore unsolicited pong. */
1413 return;
1414
Scott Moreauff1db4a2012-04-17 19:06:18 -06001415 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001416 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001417 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001418 if (was_unresponsive) {
1419 /* Received pong from previously unresponsive client */
1420 wl_list_for_each(seat, &ec->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04001421 pointer = seat->pointer;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001422 if (pointer->focus == shell->grab_surface &&
1423 pointer->current == shsurf->surface)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001424 end_busy_cursor(shsurf, pointer);
1425 }
1426 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001427 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001428 }
1429}
1430
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001431static void
1432shell_surface_set_title(struct wl_client *client,
1433 struct wl_resource *resource, const char *title)
1434{
1435 struct shell_surface *shsurf = resource->data;
1436
1437 free(shsurf->title);
1438 shsurf->title = strdup(title);
1439}
1440
1441static void
1442shell_surface_set_class(struct wl_client *client,
1443 struct wl_resource *resource, const char *class)
1444{
1445 struct shell_surface *shsurf = resource->data;
1446
1447 free(shsurf->class);
1448 shsurf->class = strdup(class);
1449}
1450
Juan Zhao96879df2012-02-07 08:45:41 +08001451static struct weston_output *
1452get_default_output(struct weston_compositor *compositor)
1453{
1454 return container_of(compositor->output_list.next,
1455 struct weston_output, link);
1456}
1457
Alex Wu4539b082012-03-01 12:57:46 +08001458static void
1459shell_unset_fullscreen(struct shell_surface *shsurf)
1460{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001461 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001462 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001463 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1464 shell_surface_is_top_fullscreen(shsurf)) {
1465 weston_output_switch_mode(shsurf->fullscreen_output,
1466 shsurf->fullscreen_output->origin);
1467 }
Alex Wu4539b082012-03-01 12:57:46 +08001468 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1469 shsurf->fullscreen.framerate = 0;
1470 wl_list_remove(&shsurf->fullscreen.transform.link);
1471 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001472 if (shsurf->fullscreen.black_surface)
1473 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001474 shsurf->fullscreen.black_surface = NULL;
1475 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001476 weston_surface_set_position(shsurf->surface,
1477 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001478 if (shsurf->saved_rotation_valid) {
1479 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1480 &shsurf->rotation.transform.link);
1481 shsurf->saved_rotation_valid = false;
1482 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001483
1484 ws = get_current_workspace(shsurf->shell);
1485 wl_list_remove(&shsurf->surface->layer_link);
1486 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001487}
1488
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001489static void
1490shell_unset_maximized(struct shell_surface *shsurf)
1491{
1492 struct workspace *ws;
1493 /* undo all maximized things here */
1494 shsurf->output = get_default_output(shsurf->surface->compositor);
1495 weston_surface_set_position(shsurf->surface,
1496 shsurf->saved_x,
1497 shsurf->saved_y);
1498
1499 if (shsurf->saved_rotation_valid) {
1500 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1501 &shsurf->rotation.transform.link);
1502 shsurf->saved_rotation_valid = false;
1503 }
1504
1505 ws = get_current_workspace(shsurf->shell);
1506 wl_list_remove(&shsurf->surface->layer_link);
1507 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1508}
1509
Pekka Paalanen98262232011-12-01 10:42:22 +02001510static int
1511reset_shell_surface_type(struct shell_surface *surface)
1512{
1513 switch (surface->type) {
1514 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001515 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001516 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001517 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001518 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001519 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001520 case SHELL_SURFACE_NONE:
1521 case SHELL_SURFACE_TOPLEVEL:
1522 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001523 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001524 break;
1525 }
1526
1527 surface->type = SHELL_SURFACE_NONE;
1528 return 0;
1529}
1530
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001531static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001532set_surface_type(struct shell_surface *shsurf)
1533{
1534 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001535 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001536
1537 reset_shell_surface_type(shsurf);
1538
1539 shsurf->type = shsurf->next_type;
1540 shsurf->next_type = SHELL_SURFACE_NONE;
1541
1542 switch (shsurf->type) {
1543 case SHELL_SURFACE_TOPLEVEL:
1544 break;
1545 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001546 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001547 pes->geometry.x + shsurf->transient.x,
1548 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001549 break;
1550
1551 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001552 case SHELL_SURFACE_FULLSCREEN:
1553 shsurf->saved_x = surface->geometry.x;
1554 shsurf->saved_y = surface->geometry.y;
1555 shsurf->saved_position_valid = true;
1556
1557 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1558 wl_list_remove(&shsurf->rotation.transform.link);
1559 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001560 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001561 shsurf->saved_rotation_valid = true;
1562 }
1563 break;
1564
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001565 default:
1566 break;
1567 }
1568}
1569
1570static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001571set_toplevel(struct shell_surface *shsurf)
1572{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001573 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001574}
1575
1576static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001577shell_surface_set_toplevel(struct wl_client *client,
1578 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001579{
Pekka Paalanen98262232011-12-01 10:42:22 +02001580 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001581
Tiago Vignattibc052c92012-04-19 16:18:18 +03001582 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001583}
1584
1585static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001586set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001587 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001588{
1589 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001590 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001591 shsurf->transient.x = x;
1592 shsurf->transient.y = y;
1593 shsurf->transient.flags = flags;
1594 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1595}
1596
1597static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001598shell_surface_set_transient(struct wl_client *client,
1599 struct wl_resource *resource,
1600 struct wl_resource *parent_resource,
1601 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001602{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001603 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001604 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001605
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001606 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001607}
1608
Tiago Vignattibe143262012-04-16 17:31:41 +03001609static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001610shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001611{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001612 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001613}
1614
1615static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001616get_output_panel_height(struct desktop_shell *shell,
1617 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001618{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001619 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001620 int panel_height = 0;
1621
1622 if (!output)
1623 return 0;
1624
Juan Zhao4ab94682012-07-09 22:24:09 -07001625 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001626 if (surface->output == output) {
1627 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001628 break;
1629 }
1630 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001631
Juan Zhao96879df2012-02-07 08:45:41 +08001632 return panel_height;
1633}
1634
1635static void
1636shell_surface_set_maximized(struct wl_client *client,
1637 struct wl_resource *resource,
1638 struct wl_resource *output_resource )
1639{
1640 struct shell_surface *shsurf = resource->data;
1641 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001642 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001643 uint32_t edges = 0, panel_height = 0;
1644
1645 /* get the default output, if the client set it as NULL
1646 check whether the ouput is available */
1647 if (output_resource)
1648 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001649 else if (es->output)
1650 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001651 else
1652 shsurf->output = get_default_output(es->compositor);
1653
Tiago Vignattibe143262012-04-16 17:31:41 +03001654 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001655 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001656 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001657
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001658 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001659 shsurf->output->width,
1660 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001661
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001662 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001663}
1664
Alex Wu21858432012-04-01 20:13:08 +08001665static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001666black_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 +08001667
Alex Wu4539b082012-03-01 12:57:46 +08001668static struct weston_surface *
1669create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001670 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001671 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001672{
1673 struct weston_surface *surface = NULL;
1674
1675 surface = weston_surface_create(ec);
1676 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001677 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001678 return NULL;
1679 }
1680
Alex Wu21858432012-04-01 20:13:08 +08001681 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001682 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001683 weston_surface_configure(surface, x, y, w, h);
1684 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001685 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001686 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001687 pixman_region32_fini(&surface->input);
1688 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001689
Alex Wu4539b082012-03-01 12:57:46 +08001690 return surface;
1691}
1692
1693/* Create black surface and append it to the associated fullscreen surface.
1694 * Handle size dismatch and positioning according to the method. */
1695static void
1696shell_configure_fullscreen(struct shell_surface *shsurf)
1697{
1698 struct weston_output *output = shsurf->fullscreen_output;
1699 struct weston_surface *surface = shsurf->surface;
1700 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001701 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001702
Alex Wu4539b082012-03-01 12:57:46 +08001703 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001704 shsurf->fullscreen.black_surface =
1705 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001706 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001707 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001708 output->width,
1709 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001710
1711 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1712 wl_list_insert(&surface->layer_link,
1713 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001714 shsurf->fullscreen.black_surface->output = output;
1715
1716 switch (shsurf->fullscreen.type) {
1717 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001718 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001719 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001720 break;
1721 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001722 /* 1:1 mapping between surface and output dimensions */
1723 if (output->width == surface->geometry.width &&
1724 output->height == surface->geometry.height) {
1725 weston_surface_set_position(surface, output->x, output->y);
1726 break;
1727 }
1728
Alex Wu4539b082012-03-01 12:57:46 +08001729 matrix = &shsurf->fullscreen.transform.matrix;
1730 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001731
Scott Moreau1bad5db2012-08-18 01:04:05 -06001732 output_aspect = (float) output->width /
1733 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001734 surface_aspect = (float) surface->geometry.width /
1735 (float) surface->geometry.height;
1736 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001737 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001738 (float) surface->geometry.width;
1739 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001740 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001741 (float) surface->geometry.height;
1742
Alex Wu4539b082012-03-01 12:57:46 +08001743 weston_matrix_scale(matrix, scale, scale, 1);
1744 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001745 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001746 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001747 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1748 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001749 weston_surface_set_position(surface, x, y);
1750
Alex Wu4539b082012-03-01 12:57:46 +08001751 break;
1752 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001753 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001754 struct weston_mode mode = {0,
Alex Wubd3354b2012-04-17 17:20:49 +08001755 surface->geometry.width,
1756 surface->geometry.height,
1757 shsurf->fullscreen.framerate};
1758
1759 if (weston_output_switch_mode(output, &mode) == 0) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001760 weston_surface_configure(shsurf->fullscreen.black_surface,
Alex Wubd3354b2012-04-17 17:20:49 +08001761 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001762 output->width,
1763 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001764 weston_surface_set_position(surface, output->x, output->y);
1765 break;
1766 }
1767 }
Alex Wu4539b082012-03-01 12:57:46 +08001768 break;
1769 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1770 break;
1771 default:
1772 break;
1773 }
1774}
1775
1776/* make the fullscreen and black surface at the top */
1777static void
1778shell_stack_fullscreen(struct shell_surface *shsurf)
1779{
Alex Wubd3354b2012-04-17 17:20:49 +08001780 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001781 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001782 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001783
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001784 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001785 wl_list_insert(&shell->fullscreen_layer.surface_list,
1786 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001787 weston_surface_damage(surface);
1788
1789 if (!shsurf->fullscreen.black_surface)
1790 shsurf->fullscreen.black_surface =
1791 create_black_surface(surface->compositor,
1792 surface,
1793 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001794 output->width,
1795 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001796
1797 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001798 wl_list_insert(&surface->layer_link,
1799 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001800 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001801}
1802
1803static void
1804shell_map_fullscreen(struct shell_surface *shsurf)
1805{
Alex Wu4539b082012-03-01 12:57:46 +08001806 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001807 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001808}
1809
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001810static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001811set_fullscreen(struct shell_surface *shsurf,
1812 uint32_t method,
1813 uint32_t framerate,
1814 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001815{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001816 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001817
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001818 if (output)
1819 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001820 else if (es->output)
1821 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001822 else
1823 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001824
Alex Wu4539b082012-03-01 12:57:46 +08001825 shsurf->fullscreen_output = shsurf->output;
1826 shsurf->fullscreen.type = method;
1827 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001828 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001829
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001830 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001831 shsurf->output->width,
1832 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001833}
1834
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001835static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001836shell_surface_set_fullscreen(struct wl_client *client,
1837 struct wl_resource *resource,
1838 uint32_t method,
1839 uint32_t framerate,
1840 struct wl_resource *output_resource)
1841{
1842 struct shell_surface *shsurf = resource->data;
1843 struct weston_output *output;
1844
1845 if (output_resource)
1846 output = output_resource->data;
1847 else
1848 output = NULL;
1849
1850 set_fullscreen(shsurf, method, framerate, output);
1851}
1852
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001853static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001854
1855static void
1856destroy_shell_seat(struct wl_listener *listener, void *data)
1857{
1858 struct shell_seat *shseat =
1859 container_of(listener,
1860 struct shell_seat, seat_destroy_listener);
1861 struct shell_surface *shsurf, *prev = NULL;
1862
1863 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001864 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001865 shseat->popup_grab.client = NULL;
1866
1867 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1868 shsurf->popup.shseat = NULL;
1869 if (prev) {
1870 wl_list_init(&prev->popup.grab_link);
1871 }
1872 prev = shsurf;
1873 }
1874 wl_list_init(&prev->popup.grab_link);
1875 }
1876
1877 wl_list_remove(&shseat->seat_destroy_listener.link);
1878 free(shseat);
1879}
1880
1881static struct shell_seat *
1882create_shell_seat(struct weston_seat *seat)
1883{
1884 struct shell_seat *shseat;
1885
1886 shseat = calloc(1, sizeof *shseat);
1887 if (!shseat) {
1888 weston_log("no memory to allocate shell seat\n");
1889 return NULL;
1890 }
1891
1892 shseat->seat = seat;
1893 wl_list_init(&shseat->popup_grab.surfaces_list);
1894
1895 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1896 wl_signal_add(&seat->destroy_signal,
1897 &shseat->seat_destroy_listener);
1898
1899 return shseat;
1900}
1901
1902static struct shell_seat *
1903get_shell_seat(struct weston_seat *seat)
1904{
1905 struct wl_listener *listener;
1906
1907 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
1908 if (listener == NULL)
1909 return create_shell_seat(seat);
1910
1911 return container_of(listener,
1912 struct shell_seat, seat_destroy_listener);
1913}
1914
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001915static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001916popup_grab_focus(struct weston_pointer_grab *grab,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001917 struct weston_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001918 wl_fixed_t x,
1919 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001920{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001921 struct weston_pointer *pointer = grab->pointer;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001922 struct shell_seat *shseat =
1923 container_of(grab, struct shell_seat, popup_grab.grab);
1924 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001925
Pekka Paalanencb108432012-01-19 16:25:40 +02001926 if (surface && surface->resource.client == client) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001927 weston_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001928 grab->focus = surface;
1929 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001930 weston_pointer_set_focus(pointer, NULL,
1931 wl_fixed_from_int(0),
1932 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001933 grab->focus = NULL;
1934 }
1935}
1936
1937static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001938popup_grab_motion(struct weston_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001939 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001940{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001941 struct wl_resource *resource;
1942
Daniel Stone37816df2012-05-16 18:45:18 +01001943 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001944 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001945 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001946}
1947
1948static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001949popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001950 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001951{
1952 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001953 struct shell_seat *shseat =
1954 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001955 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001956 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001957 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001958
Daniel Stone37816df2012-05-16 18:45:18 +01001959 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001960 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001961 display = wl_client_get_display(resource->client);
1962 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001963 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001964 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01001965 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001966 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001967 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001968 }
1969
Daniel Stone4dbadb12012-05-30 16:31:51 +01001970 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01001971 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001972}
1973
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001974static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001975 popup_grab_focus,
1976 popup_grab_motion,
1977 popup_grab_button,
1978};
1979
1980static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001981popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001982{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001983 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001984 struct shell_seat *shseat =
1985 container_of(grab, struct shell_seat, popup_grab.grab);
1986 struct shell_surface *shsurf;
1987 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001988
1989 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001990 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001991 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02001992 shseat->popup_grab.grab.interface = NULL;
1993 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01001994 /* Send the popup_done event to all the popups open */
1995 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1996 wl_shell_surface_send_popup_done(&shsurf->resource);
1997 shsurf->popup.shseat = NULL;
1998 if (prev) {
1999 wl_list_init(&prev->popup.grab_link);
2000 }
2001 prev = shsurf;
2002 }
2003 wl_list_init(&prev->popup.grab_link);
2004 wl_list_init(&shseat->popup_grab.surfaces_list);
2005 }
2006}
2007
2008static void
2009add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2010{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002011 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002012
2013 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002014 shseat->popup_grab.client = shsurf->resource.client;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002015 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002016 /* We must make sure here that this popup was opened after
2017 * a mouse press, and not just by moving around with other
2018 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002019 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002020 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002021
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002022 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002023 }
2024 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2025}
2026
2027static void
2028remove_popup_grab(struct shell_surface *shsurf)
2029{
2030 struct shell_seat *shseat = shsurf->popup.shseat;
2031
2032 wl_list_remove(&shsurf->popup.grab_link);
2033 wl_list_init(&shsurf->popup.grab_link);
2034 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002035 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002036 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002037 }
2038}
2039
2040static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002041shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002042{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002043 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002044 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002045 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002046
2047 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002048
Pekka Paalanen483243f2013-03-08 14:56:50 +02002049 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002050 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2051 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002052
Kristian Høgsberge3148752013-05-06 23:19:49 -04002053 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002054 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002055 } else {
2056 wl_shell_surface_send_popup_done(&shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002057 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002058 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002059}
2060
2061static void
2062shell_surface_set_popup(struct wl_client *client,
2063 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002064 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002065 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002066 struct wl_resource *parent_resource,
2067 int32_t x, int32_t y, uint32_t flags)
2068{
2069 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002070
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002071 shsurf->type = SHELL_SURFACE_POPUP;
2072 shsurf->parent = parent_resource->data;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002073 shsurf->popup.shseat = get_shell_seat(seat_resource->data);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002074 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002075 shsurf->popup.x = x;
2076 shsurf->popup.y = y;
2077}
2078
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002079static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002080 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002081 shell_surface_move,
2082 shell_surface_resize,
2083 shell_surface_set_toplevel,
2084 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002085 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002086 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002087 shell_surface_set_maximized,
2088 shell_surface_set_title,
2089 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002090};
2091
2092static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002093destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002094{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002095 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2096 remove_popup_grab(shsurf);
2097 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002098
Alex Wubd3354b2012-04-17 17:20:49 +08002099 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2100 shell_surface_is_top_fullscreen(shsurf)) {
2101 weston_output_switch_mode(shsurf->fullscreen_output,
2102 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002103 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002104
Alex Wuaa08e2d2012-03-05 11:01:40 +08002105 if (shsurf->fullscreen.black_surface)
2106 weston_surface_destroy(shsurf->fullscreen.black_surface);
2107
Alex Wubd3354b2012-04-17 17:20:49 +08002108 /* As destroy_resource() use wl_list_for_each_safe(),
2109 * we can always remove the listener.
2110 */
2111 wl_list_remove(&shsurf->surface_destroy_listener.link);
2112 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002113 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002114 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002115
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002116 wl_list_remove(&shsurf->link);
2117 free(shsurf);
2118}
2119
2120static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002121shell_destroy_shell_surface(struct wl_resource *resource)
2122{
2123 struct shell_surface *shsurf = resource->data;
2124
2125 destroy_shell_surface(shsurf);
2126}
2127
2128static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002129shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002130{
2131 struct shell_surface *shsurf = container_of(listener,
2132 struct shell_surface,
2133 surface_destroy_listener);
2134
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002135 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002136 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002137 } else {
2138 wl_signal_emit(&shsurf->resource.destroy_signal,
2139 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002140 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002141 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002142}
2143
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002144static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002145shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002146
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002147static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002148get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002149{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002150 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002151 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002152 else
2153 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002154}
2155
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002156static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002157create_shell_surface(void *shell, struct weston_surface *surface,
2158 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002159{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002160 struct shell_surface *shsurf;
2161
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002162 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002163 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002164 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002165 }
2166
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002167 shsurf = calloc(1, sizeof *shsurf);
2168 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002169 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002170 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002171 }
2172
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002173 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002174 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002175
Tiago Vignattibc052c92012-04-19 16:18:18 +03002176 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002177 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002178 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002179 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002180 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002181 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2182 shsurf->fullscreen.framerate = 0;
2183 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002184 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002185 wl_list_init(&shsurf->fullscreen.transform.link);
2186
Tiago Vignattibc052c92012-04-19 16:18:18 +03002187 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002188 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002189 wl_signal_add(&surface->resource.destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002190 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002191
2192 /* init link so its safe to always remove it in destroy_shell_surface */
2193 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002194 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002195
Pekka Paalanen460099f2012-01-20 16:48:25 +02002196 /* empty when not in use */
2197 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002198 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002199
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002200 wl_list_init(&shsurf->workspace_transform.link);
2201
Pekka Paalanen98262232011-12-01 10:42:22 +02002202 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002203 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002204
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002205 shsurf->client = client;
2206
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002207 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002208}
2209
2210static void
2211shell_get_shell_surface(struct wl_client *client,
2212 struct wl_resource *resource,
2213 uint32_t id,
2214 struct wl_resource *surface_resource)
2215{
2216 struct weston_surface *surface = surface_resource->data;
2217 struct desktop_shell *shell = resource->data;
2218 struct shell_surface *shsurf;
2219
2220 if (get_shell_surface(surface)) {
2221 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002222 WL_DISPLAY_ERROR_INVALID_OBJECT,
2223 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002224 return;
2225 }
2226
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002227 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002228 if (!shsurf) {
2229 wl_resource_post_error(surface_resource,
2230 WL_DISPLAY_ERROR_INVALID_OBJECT,
2231 "surface->configure already set");
2232 return;
2233 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002234
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002235 shsurf->resource.destroy = shell_destroy_shell_surface;
2236 shsurf->resource.object.id = id;
2237 shsurf->resource.object.interface = &wl_shell_surface_interface;
2238 shsurf->resource.object.implementation =
2239 (void (**)(void)) &shell_surface_implementation;
2240 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002241
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002242 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002243}
2244
2245static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002246 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002247};
2248
Kristian Høgsberg07937562011-04-12 17:25:42 -04002249static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002250shell_fade(struct desktop_shell *shell, enum fade_type type);
2251
2252static int
2253screensaver_timeout(void *data)
2254{
2255 struct desktop_shell *shell = data;
2256
2257 shell_fade(shell, FADE_OUT);
2258
2259 return 1;
2260}
2261
2262static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002263handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002264{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002265 struct desktop_shell *shell =
2266 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002267
Pekka Paalanen18027e52011-12-02 16:31:49 +02002268 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002269
2270 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002271 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002272}
2273
2274static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002275launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002276{
2277 if (shell->screensaver.binding)
2278 return;
2279
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002280 if (!shell->screensaver.path) {
2281 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002282 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002283 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002284
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002285 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002286 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002287 return;
2288 }
2289
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002290 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002291 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002292 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002293 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002294}
2295
2296static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002297terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002298{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002299 if (shell->screensaver.process.pid == 0)
2300 return;
2301
2302 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002303}
2304
2305static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002306configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002307{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002308 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002309
Giulio Camuffo184df502013-02-21 11:29:21 +01002310 if (width == 0)
2311 return;
2312
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002313 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2314 if (s->output == es->output && s != es) {
2315 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002316 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002317 }
2318 }
2319
Giulio Camuffo184df502013-02-21 11:29:21 +01002320 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002321
2322 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002323 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002324 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002325 }
2326}
2327
2328static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002329background_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 -04002330{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002331 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002332
Giulio Camuffo184df502013-02-21 11:29:21 +01002333 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002334}
2335
2336static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002337desktop_shell_set_background(struct wl_client *client,
2338 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002339 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002340 struct wl_resource *surface_resource)
2341{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002342 struct desktop_shell *shell = resource->data;
2343 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002344
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002345 if (surface->configure) {
2346 wl_resource_post_error(surface_resource,
2347 WL_DISPLAY_ERROR_INVALID_OBJECT,
2348 "surface role already assigned");
2349 return;
2350 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002351
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002352 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002353 surface->configure_private = shell;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002354 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002355 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002356 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002357 surface->output->width,
2358 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002359}
2360
2361static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002362panel_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 -04002363{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002364 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002365
Giulio Camuffo184df502013-02-21 11:29:21 +01002366 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002367}
2368
2369static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002370desktop_shell_set_panel(struct wl_client *client,
2371 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002372 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002373 struct wl_resource *surface_resource)
2374{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002375 struct desktop_shell *shell = resource->data;
2376 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002377
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002378 if (surface->configure) {
2379 wl_resource_post_error(surface_resource,
2380 WL_DISPLAY_ERROR_INVALID_OBJECT,
2381 "surface role already assigned");
2382 return;
2383 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002384
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002385 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002386 surface->configure_private = shell;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002387 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002388 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002389 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002390 surface->output->width,
2391 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002392}
2393
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002394static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002395lock_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 -04002396{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002397 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002398
Giulio Camuffo184df502013-02-21 11:29:21 +01002399 if (width == 0)
2400 return;
2401
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002402 center_on_output(surface, get_default_output(shell->compositor));
2403
2404 if (!weston_surface_is_mapped(surface)) {
2405 wl_list_insert(&shell->lock_layer.surface_list,
2406 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002407 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002408 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002409 }
2410}
2411
2412static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002413handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002414{
Tiago Vignattibe143262012-04-16 17:31:41 +03002415 struct desktop_shell *shell =
2416 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002417
Martin Minarik6d118362012-06-07 18:01:59 +02002418 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002419 shell->lock_surface = NULL;
2420}
2421
2422static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002423desktop_shell_set_lock_surface(struct wl_client *client,
2424 struct wl_resource *resource,
2425 struct wl_resource *surface_resource)
2426{
Tiago Vignattibe143262012-04-16 17:31:41 +03002427 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002428 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002429
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002430 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002431
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002432 if (!shell->locked)
2433 return;
2434
Pekka Paalanen98262232011-12-01 10:42:22 +02002435 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002436
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002437 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2438 wl_signal_add(&surface_resource->destroy_signal,
2439 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002440
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002441 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002442 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002443}
2444
2445static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002446resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002447{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002448 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002449
Pekka Paalanen77346a62011-11-30 16:26:35 +02002450 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002451
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002452 wl_list_remove(&shell->lock_layer.link);
2453 wl_list_insert(&shell->compositor->cursor_layer.link,
2454 &shell->fullscreen_layer.link);
2455 wl_list_insert(&shell->fullscreen_layer.link,
2456 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002457 if (shell->showing_input_panels) {
2458 wl_list_insert(&shell->panel_layer.link,
2459 &shell->input_panel_layer.link);
2460 wl_list_insert(&shell->input_panel_layer.link,
2461 &ws->layer.link);
2462 } else {
2463 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2464 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002465
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002466 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002467
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002468 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002469 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002470 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002471}
2472
2473static void
2474desktop_shell_unlock(struct wl_client *client,
2475 struct wl_resource *resource)
2476{
Tiago Vignattibe143262012-04-16 17:31:41 +03002477 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002478
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002479 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002480
2481 if (shell->locked)
2482 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002483}
2484
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002485static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002486desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002487 struct wl_resource *resource,
2488 struct wl_resource *surface_resource)
2489{
2490 struct desktop_shell *shell = resource->data;
2491
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002492 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002493}
2494
Kristian Høgsberg75840622011-09-06 13:48:16 -04002495static const struct desktop_shell_interface desktop_shell_implementation = {
2496 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002497 desktop_shell_set_panel,
2498 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002499 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002500 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002501};
2502
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002503static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002504get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002505{
2506 struct shell_surface *shsurf;
2507
2508 shsurf = get_shell_surface(surface);
2509 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002510 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002511 return shsurf->type;
2512}
2513
Kristian Høgsberg75840622011-09-06 13:48:16 -04002514static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002515move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002516{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002517 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002518 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002519 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002520
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002521 if (surface == NULL)
2522 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002523
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002524 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002525 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2526 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002527 return;
2528
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002529 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002530}
2531
2532static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002533resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002534{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002535 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002536 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002537 uint32_t edges = 0;
2538 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002539 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002540
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002541 if (surface == NULL)
2542 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002543
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002544 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002545 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2546 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002547 return;
2548
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002549 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002550 wl_fixed_to_int(seat->pointer->grab_x),
2551 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002552 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002553
Pekka Paalanen60921e52012-01-25 15:55:43 +02002554 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002555 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002556 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002557 edges |= 0;
2558 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002559 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002560
Pekka Paalanen60921e52012-01-25 15:55:43 +02002561 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002562 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002563 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002564 edges |= 0;
2565 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002566 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002567
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002568 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002569}
2570
2571static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002572surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002573 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002574{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002575 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002576 struct shell_surface *shsurf;
2577 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002578 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002579
2580 if (surface == NULL)
2581 return;
2582
2583 shsurf = get_shell_surface(surface);
2584 if (!shsurf)
2585 return;
2586
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002587 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002588
Scott Moreau02709af2012-05-22 01:54:10 -06002589 if (surface->alpha > 1.0)
2590 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002591 if (surface->alpha < step)
2592 surface->alpha = step;
2593
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002594 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002595 weston_surface_damage(surface);
2596}
2597
2598static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002599do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002600 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002601{
Daniel Stone37816df2012-05-16 18:45:18 +01002602 struct weston_seat *ws = (struct weston_seat *) seat;
2603 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002604 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002605 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002606
2607 wl_list_for_each(output, &compositor->output_list, link) {
2608 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002609 wl_fixed_to_double(seat->pointer->x),
2610 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002611 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002612 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002613 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002614 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002615 increment = -output->zoom.increment;
2616 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002617 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002618 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002619 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002620 else
2621 increment = 0;
2622
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002623 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002624
Scott Moreaue6603982012-06-11 13:07:51 -06002625 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002626 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002627 else if (output->zoom.level > output->zoom.max_level)
2628 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002629 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002630 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002631 output->disable_planes++;
2632 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002633
Scott Moreaue6603982012-06-11 13:07:51 -06002634 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002635
Scott Moreau8dacaab2012-06-17 18:10:58 -06002636 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002637 }
2638 }
2639}
2640
Scott Moreauccbf29d2012-02-22 14:21:41 -07002641static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002642zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002643 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002644{
2645 do_zoom(seat, time, 0, axis, value);
2646}
2647
2648static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002649zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002650 void *data)
2651{
2652 do_zoom(seat, time, key, 0, 0);
2653}
2654
2655static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002656terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002657 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002658{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002659 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002660
Daniel Stone325fc2d2012-05-30 16:31:58 +01002661 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002662}
2663
2664static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002665rotate_grab_motion(struct weston_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002666 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002667{
2668 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002669 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002670 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002671 struct shell_surface *shsurf = rotate->base.shsurf;
2672 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002673 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002674
2675 if (!shsurf)
2676 return;
2677
2678 surface = shsurf->surface;
2679
2680 cx = 0.5f * surface->geometry.width;
2681 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002682
Daniel Stone37816df2012-05-16 18:45:18 +01002683 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2684 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002685 r = sqrtf(dx * dx + dy * dy);
2686
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002687 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002688 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002689
2690 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002691 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002692 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002693
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002694 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002695 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002696
2697 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002698 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002699 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002700 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002701 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002702
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002703 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002704 &shsurf->surface->geometry.transformation_list,
2705 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002706 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002707 wl_list_init(&shsurf->rotation.transform.link);
2708 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002709 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002710 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002711
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002712 /* We need to adjust the position of the surface
2713 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002714 cposx = surface->geometry.x + cx;
2715 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002716 dposx = rotate->center.x - cposx;
2717 dposy = rotate->center.y - cposy;
2718 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002719 weston_surface_set_position(surface,
2720 surface->geometry.x + dposx,
2721 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002722 }
2723
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002724 /* Repaint implies weston_surface_update_transform(), which
2725 * lazily applies the damage due to rotation update.
2726 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002727 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002728}
2729
2730static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002731rotate_grab_button(struct weston_pointer_grab *grab,
2732 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002733{
2734 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002735 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002736 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002737 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002738 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002739
Daniel Stone4dbadb12012-05-30 16:31:51 +01002740 if (pointer->button_count == 0 &&
2741 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002742 if (shsurf)
2743 weston_matrix_multiply(&shsurf->rotation.rotation,
2744 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002745 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002746 free(rotate);
2747 }
2748}
2749
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002750static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002751 noop_grab_focus,
2752 rotate_grab_motion,
2753 rotate_grab_button,
2754};
2755
2756static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002757surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002758{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002759 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002760 float dx, dy;
2761 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002762
Pekka Paalanen460099f2012-01-20 16:48:25 +02002763 rotate = malloc(sizeof *rotate);
2764 if (!rotate)
2765 return;
2766
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002767 weston_surface_to_global_float(surface->surface,
2768 surface->surface->geometry.width / 2,
2769 surface->surface->geometry.height / 2,
2770 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002771
Daniel Stone37816df2012-05-16 18:45:18 +01002772 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2773 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002774 r = sqrtf(dx * dx + dy * dy);
2775 if (r > 20.0f) {
2776 struct weston_matrix inverse;
2777
2778 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002779 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002780 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002781
2782 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002783 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002784 } else {
2785 weston_matrix_init(&surface->rotation.rotation);
2786 weston_matrix_init(&rotate->rotation);
2787 }
2788
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002789 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2790 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002791}
2792
2793static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002794rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002795 void *data)
2796{
2797 struct weston_surface *base_surface =
2798 (struct weston_surface *) seat->pointer->focus;
2799 struct shell_surface *surface;
2800
2801 if (base_surface == NULL)
2802 return;
2803
2804 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002805 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2806 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002807 return;
2808
2809 surface_rotate(surface, seat);
2810}
2811
2812static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002813lower_fullscreen_layer(struct desktop_shell *shell)
2814{
2815 struct workspace *ws;
2816 struct weston_surface *surface, *prev;
2817
2818 ws = get_current_workspace(shell);
2819 wl_list_for_each_reverse_safe(surface, prev,
2820 &shell->fullscreen_layer.surface_list,
2821 layer_link)
2822 weston_surface_restack(surface, &ws->layer.surface_list);
2823}
2824
2825static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002826activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002827 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002828{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002829 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002830 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002831
Daniel Stone37816df2012-05-16 18:45:18 +01002832 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002833
Jonas Ådahl8538b222012-08-29 22:13:03 +02002834 state = ensure_focus_state(shell, seat);
2835 if (state == NULL)
2836 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002837
2838 state->keyboard_focus = es;
2839 wl_list_remove(&state->surface_destroy_listener.link);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002840 wl_signal_add(&es->resource.destroy_signal,
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002841 &state->surface_destroy_listener);
2842
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002843 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002844 case SHELL_SURFACE_FULLSCREEN:
2845 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002846 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002847 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002848 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002849 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002850 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002851 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002852 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002853 }
2854}
2855
Alex Wu21858432012-04-01 20:13:08 +08002856/* no-op func for checking black surface */
2857static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002858black_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 +08002859{
2860}
2861
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002862static bool
Alex Wu21858432012-04-01 20:13:08 +08002863is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2864{
2865 if (es->configure == black_surface_configure) {
2866 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002867 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002868 return true;
2869 }
2870 return false;
2871}
2872
Kristian Høgsberg75840622011-09-06 13:48:16 -04002873static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002874click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002875 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002876{
Daniel Stone37816df2012-05-16 18:45:18 +01002877 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002878 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002879 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002880 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002881
Daniel Stone37816df2012-05-16 18:45:18 +01002882 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002883 if (!focus)
2884 return;
2885
Alex Wu21858432012-04-01 20:13:08 +08002886 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002887 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002888
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002889 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2890 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002891
Daniel Stone325fc2d2012-05-30 16:31:58 +01002892 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002893 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002894}
2895
2896static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002897lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002898{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002899 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002900
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002901 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002902 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002903 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002904 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002905
2906 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002907
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002908 /* Hide all surfaces by removing the fullscreen, panel and
2909 * toplevel layers. This way nothing else can show or receive
2910 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002911
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002912 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002913 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002914 if (shell->showing_input_panels)
2915 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002916 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002917 wl_list_insert(&shell->compositor->cursor_layer.link,
2918 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002919
Pekka Paalanen77346a62011-11-30 16:26:35 +02002920 launch_screensaver(shell);
2921
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002922 /* TODO: disable bindings that should not work while locked. */
2923
2924 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002925}
2926
2927static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002928unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002929{
Pekka Paalanend81c2162011-11-16 13:47:34 +02002930 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002931 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002932 return;
2933 }
2934
2935 /* If desktop-shell client has gone away, unlock immediately. */
2936 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002937 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002938 return;
2939 }
2940
2941 if (shell->prepare_event_sent)
2942 return;
2943
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002944 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002945 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002946}
2947
2948static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002949shell_fade_done(struct weston_surface_animation *animation, void *data)
2950{
2951 struct desktop_shell *shell = data;
2952
2953 shell->fade.animation = NULL;
2954
2955 switch (shell->fade.type) {
2956 case FADE_IN:
2957 weston_surface_destroy(shell->fade.surface);
2958 shell->fade.surface = NULL;
2959 break;
2960 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002961 lock(shell);
2962 break;
2963 }
2964}
2965
2966static void
2967shell_fade(struct desktop_shell *shell, enum fade_type type)
2968{
2969 struct weston_compositor *compositor = shell->compositor;
2970 struct weston_surface *surface;
2971 float tint;
2972
2973 switch (type) {
2974 case FADE_IN:
2975 tint = 0.0;
2976 break;
2977 case FADE_OUT:
2978 tint = 1.0;
2979 break;
2980 default:
2981 weston_log("shell: invalid fade type\n");
2982 return;
2983 }
2984
2985 shell->fade.type = type;
2986
2987 if (shell->fade.surface == NULL) {
2988 surface = weston_surface_create(compositor);
2989 if (!surface)
2990 return;
2991
2992 weston_surface_configure(surface, 0, 0, 8192, 8192);
2993 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
2994 surface->alpha = 1.0 - tint;
2995 wl_list_insert(&compositor->fade_layer.surface_list,
2996 &surface->layer_link);
2997 weston_surface_update_transform(surface);
2998 shell->fade.surface = surface;
2999 pixman_region32_init(&surface->input);
3000 }
3001
3002 if (shell->fade.animation)
3003 weston_fade_update(shell->fade.animation,
3004 shell->fade.surface->alpha, tint, 30.0);
3005 else
3006 shell->fade.animation =
3007 weston_fade_run(shell->fade.surface,
3008 1.0 - tint, tint, 30.0,
3009 shell_fade_done, shell);
3010}
3011
3012static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003013idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003014{
3015 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003016 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003017
3018 shell_fade(shell, FADE_OUT);
3019 /* lock() is called from shell_fade_done() */
3020}
3021
3022static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003023wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003024{
3025 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003026 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003027
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003028 unlock(shell);
3029}
3030
3031static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003032show_input_panels(struct wl_listener *listener, void *data)
3033{
3034 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003035 container_of(listener, struct desktop_shell,
3036 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003037 struct input_panel_surface *surface, *next;
3038 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003039
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003040 shell->text_input.surface = (struct weston_surface*)data;
3041
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003042 if (shell->showing_input_panels)
3043 return;
3044
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003045 shell->showing_input_panels = true;
3046
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003047 if (!shell->locked)
3048 wl_list_insert(&shell->panel_layer.link,
3049 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003050
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003051 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003052 &shell->input_panel.surfaces, link) {
3053 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003054 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003055 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003056 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003057 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003058 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003059 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003060 weston_surface_damage(ws);
3061 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003062 }
3063}
3064
3065static void
3066hide_input_panels(struct wl_listener *listener, void *data)
3067{
3068 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003069 container_of(listener, struct desktop_shell,
3070 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003071 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003072
Jan Arne Petersen61381972013-01-31 15:52:21 +01003073 if (!shell->showing_input_panels)
3074 return;
3075
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003076 shell->showing_input_panels = false;
3077
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003078 if (!shell->locked)
3079 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003080
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003081 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003082 &shell->input_panel_layer.surface_list, layer_link)
3083 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003084}
3085
3086static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003087update_input_panels(struct wl_listener *listener, void *data)
3088{
3089 struct desktop_shell *shell =
3090 container_of(listener, struct desktop_shell,
3091 update_input_panel_listener);
3092
3093 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3094}
3095
3096static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003097center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003098{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003099 int32_t width = weston_surface_buffer_width(surface);
3100 int32_t height = weston_surface_buffer_height(surface);
3101 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003102
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003103 x = output->x + (output->width - width) / 2;
3104 y = output->y + (output->height - height) / 2;
3105
3106 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003107}
3108
3109static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003110weston_surface_set_initial_position (struct weston_surface *surface,
3111 struct desktop_shell *shell)
3112{
3113 struct weston_compositor *compositor = shell->compositor;
3114 int ix = 0, iy = 0;
3115 int range_x, range_y;
3116 int dx, dy, x, y, panel_height;
3117 struct weston_output *output, *target_output = NULL;
3118 struct weston_seat *seat;
3119
3120 /* As a heuristic place the new window on the same output as the
3121 * pointer. Falling back to the output containing 0, 0.
3122 *
3123 * TODO: Do something clever for touch too?
3124 */
3125 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003126 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003127 ix = wl_fixed_to_int(seat->pointer->x);
3128 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003129 break;
3130 }
3131 }
3132
3133 wl_list_for_each(output, &compositor->output_list, link) {
3134 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3135 target_output = output;
3136 break;
3137 }
3138 }
3139
3140 if (!target_output) {
3141 weston_surface_set_position(surface, 10 + random() % 400,
3142 10 + random() % 400);
3143 return;
3144 }
3145
3146 /* Valid range within output where the surface will still be onscreen.
3147 * If this is negative it means that the surface is bigger than
3148 * output.
3149 */
3150 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003151 range_x = target_output->width - surface->geometry.width;
3152 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003153 surface->geometry.height;
3154
Rob Bradford4cb88c72012-08-13 15:18:44 +01003155 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003156 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003157 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003158 dx = 0;
3159
3160 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003161 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003162 else
3163 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003164
3165 x = target_output->x + dx;
3166 y = target_output->y + dy;
3167
3168 weston_surface_set_position (surface, x, y);
3169}
3170
3171static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003172map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003173 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003174{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003175 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003176 struct shell_surface *shsurf = get_shell_surface(surface);
3177 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003178 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003179 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003180 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003181 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003182
Pekka Paalanen60921e52012-01-25 15:55:43 +02003183 surface->geometry.width = width;
3184 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003185 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003186
3187 /* initial positioning, see also configure() */
3188 switch (surface_type) {
3189 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003190 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003191 break;
Alex Wu4539b082012-03-01 12:57:46 +08003192 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003193 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003194 shell_map_fullscreen(shsurf);
3195 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003196 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003197 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003198 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01003199 weston_surface_set_position(surface, shsurf->output->x,
3200 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08003201 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003202 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003203 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003204 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003205 case SHELL_SURFACE_NONE:
3206 weston_surface_set_position(surface,
3207 surface->geometry.x + sx,
3208 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003209 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003210 default:
3211 ;
3212 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003213
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003214 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003215 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003216 case SHELL_SURFACE_POPUP:
3217 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003218 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003219 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3220 break;
Alex Wu4539b082012-03-01 12:57:46 +08003221 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003222 case SHELL_SURFACE_NONE:
3223 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003224 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003225 ws = get_current_workspace(shell);
3226 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003227 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003228 }
3229
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003230 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003231 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003232 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3233 surface->output = shsurf->output;
3234 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003235
Juan Zhao7bb92f02011-12-15 11:31:51 -05003236 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05003237 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003238 if (shsurf->transient.flags ==
3239 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3240 break;
3241 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003242 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003243 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003244 if (!shell->locked) {
3245 wl_list_for_each(seat, &compositor->seat_list, link)
3246 activate(shell, surface, seat);
3247 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003248 break;
3249 default:
3250 break;
3251 }
3252
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003253 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003254 {
3255 switch (shell->win_animation_type) {
3256 case ANIMATION_FADE:
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +02003257 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003258 break;
3259 case ANIMATION_ZOOM:
3260 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3261 break;
3262 default:
3263 break;
3264 }
3265 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003266}
3267
3268static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003269configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003270 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003271{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003272 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3273 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003274
Pekka Paalanen77346a62011-11-30 16:26:35 +02003275 shsurf = get_shell_surface(surface);
3276 if (shsurf)
3277 surface_type = shsurf->type;
3278
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003279 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003280
3281 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003282 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003283 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003284 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003285 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003286 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003287 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003288 surface->geometry.x = surface->output->x;
3289 surface->geometry.y = surface->output->y +
3290 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003291 break;
Alex Wu4539b082012-03-01 12:57:46 +08003292 case SHELL_SURFACE_TOPLEVEL:
3293 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003294 default:
3295 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003296 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003297
Alex Wu4539b082012-03-01 12:57:46 +08003298 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003299 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003300 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003301
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003302 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003303 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003304 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003305}
3306
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003307static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003308shell_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 +03003309{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003310 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003311 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003312
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003313 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003314
Giulio Camuffo5085a752013-03-25 21:42:45 +01003315 if (!weston_surface_is_mapped(es) && !wl_list_empty(&shsurf->popup.grab_link)) {
3316 remove_popup_grab(shsurf);
3317 }
3318
Giulio Camuffo184df502013-02-21 11:29:21 +01003319 if (width == 0)
3320 return;
3321
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003322 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003323 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003324 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003325 type_changed = 1;
3326 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003327
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003328 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003329 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003330 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003331 es->geometry.width != width ||
3332 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003333 float from_x, from_y;
3334 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003335
3336 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3337 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3338 configure(shell, es,
3339 es->geometry.x + to_x - from_x,
3340 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003341 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003342 }
3343}
3344
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003345static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003346
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003347static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003348desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003349{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003350 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003351 struct desktop_shell *shell =
3352 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003353
3354 shell->child.process.pid = 0;
3355 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003356
3357 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3358 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003359 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003360 shell->child.deathstamp = time;
3361 shell->child.deathcount = 0;
3362 }
3363
3364 shell->child.deathcount++;
3365 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003366 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003367 return;
3368 }
3369
Martin Minarik6d118362012-06-07 18:01:59 +02003370 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003371 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003372}
3373
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003374static void
3375launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003376{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003377 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003378 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003379
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003380 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003381 &shell->child.process,
3382 shell_exe,
3383 desktop_shell_sigchld);
3384
3385 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003386 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003387}
3388
3389static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003390bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3391{
Tiago Vignattibe143262012-04-16 17:31:41 +03003392 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003393
3394 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003395 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003396}
3397
Kristian Høgsberg75840622011-09-06 13:48:16 -04003398static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003399unbind_desktop_shell(struct wl_resource *resource)
3400{
Tiago Vignattibe143262012-04-16 17:31:41 +03003401 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003402
3403 if (shell->locked)
3404 resume_desktop(shell);
3405
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003406 shell->child.desktop_shell = NULL;
3407 shell->prepare_event_sent = false;
3408 free(resource);
3409}
3410
3411static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003412bind_desktop_shell(struct wl_client *client,
3413 void *data, uint32_t version, uint32_t id)
3414{
Tiago Vignattibe143262012-04-16 17:31:41 +03003415 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003416 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003417
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003418 resource = wl_client_add_object(client, &desktop_shell_interface,
3419 &desktop_shell_implementation,
3420 id, shell);
3421
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003422 if (client == shell->child.client) {
3423 resource->destroy = unbind_desktop_shell;
3424 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003425 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003426 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003427
3428 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3429 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003430 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003431}
3432
Pekka Paalanen6e168112011-11-24 11:34:05 +02003433static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003434screensaver_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 -04003435{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003436 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003437
Giulio Camuffo184df502013-02-21 11:29:21 +01003438 if (width == 0)
3439 return;
3440
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003441 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003442 if (!shell->locked)
3443 return;
3444
3445 center_on_output(surface, surface->output);
3446
3447 if (wl_list_empty(&surface->layer_link)) {
3448 wl_list_insert(shell->lock_layer.surface_list.prev,
3449 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003450 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003451 wl_event_source_timer_update(shell->screensaver.timer,
3452 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003453 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003454 }
3455}
3456
3457static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003458screensaver_set_surface(struct wl_client *client,
3459 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003460 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003461 struct wl_resource *output_resource)
3462{
Tiago Vignattibe143262012-04-16 17:31:41 +03003463 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003464 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003465 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003466
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003467 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003468 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003469 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003470}
3471
3472static const struct screensaver_interface screensaver_implementation = {
3473 screensaver_set_surface
3474};
3475
3476static void
3477unbind_screensaver(struct wl_resource *resource)
3478{
Tiago Vignattibe143262012-04-16 17:31:41 +03003479 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003480
Pekka Paalanen77346a62011-11-30 16:26:35 +02003481 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003482 free(resource);
3483}
3484
3485static void
3486bind_screensaver(struct wl_client *client,
3487 void *data, uint32_t version, uint32_t id)
3488{
Tiago Vignattibe143262012-04-16 17:31:41 +03003489 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003490 struct wl_resource *resource;
3491
3492 resource = wl_client_add_object(client, &screensaver_interface,
3493 &screensaver_implementation,
3494 id, shell);
3495
Pekka Paalanen77346a62011-11-30 16:26:35 +02003496 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003497 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003498 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003499 return;
3500 }
3501
3502 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3503 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003504 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003505}
3506
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003507static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003508input_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 -04003509{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003510 struct input_panel_surface *ip_surface = surface->configure_private;
3511 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003512 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003513 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003514 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003515
Giulio Camuffo184df502013-02-21 11:29:21 +01003516 if (width == 0)
3517 return;
3518
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003519 if (!weston_surface_is_mapped(surface)) {
3520 if (!shell->showing_input_panels)
3521 return;
3522
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003523 show_surface = 1;
3524 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003525
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003526 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003527
3528 if (ip_surface->panel) {
3529 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3530 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3531 } else {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003532 mode = ip_surface->output->current;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003533
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003534 x = ip_surface->output->x + (mode->width - width) / 2;
3535 y = ip_surface->output->y + mode->height - height;
3536 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003537
3538 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003539 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003540 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003541
3542 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003543 wl_list_insert(&shell->input_panel_layer.surface_list,
3544 &surface->layer_link);
3545 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003546 weston_surface_damage(surface);
3547 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3548 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003549}
3550
3551static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003552destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003553{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003554 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003555 wl_list_remove(&input_panel_surface->link);
3556
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003557 input_panel_surface->surface->configure = NULL;
3558
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003559 free(input_panel_surface);
3560}
3561
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003562static struct input_panel_surface *
3563get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003564{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003565 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003566 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003567 } else {
3568 return NULL;
3569 }
3570}
3571
3572static void
3573input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3574{
3575 struct input_panel_surface *ipsurface = container_of(listener,
3576 struct input_panel_surface,
3577 surface_destroy_listener);
3578
3579 if (ipsurface->resource.client) {
3580 wl_resource_destroy(&ipsurface->resource);
3581 } else {
3582 wl_signal_emit(&ipsurface->resource.destroy_signal,
3583 &ipsurface->resource);
3584 destroy_input_panel_surface(ipsurface);
3585 }
3586}
3587static struct input_panel_surface *
3588create_input_panel_surface(struct desktop_shell *shell,
3589 struct weston_surface *surface)
3590{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003591 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003592
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003593 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3594 if (!input_panel_surface)
3595 return NULL;
3596
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003597 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003598 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003599
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003600 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003601
3602 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003603
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003604 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3605 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04003606 wl_signal_add(&surface->resource.destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003607 &input_panel_surface->surface_destroy_listener);
3608
3609 wl_list_init(&input_panel_surface->link);
3610
3611 return input_panel_surface;
3612}
3613
3614static void
3615input_panel_surface_set_toplevel(struct wl_client *client,
3616 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003617 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003618 uint32_t position)
3619{
3620 struct input_panel_surface *input_panel_surface = resource->data;
3621 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003622
3623 wl_list_insert(&shell->input_panel.surfaces,
3624 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003625
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003626 input_panel_surface->output = output_resource->data;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003627 input_panel_surface->panel = 0;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003628
3629 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__,
3630 input_panel_surface->panel,
3631 input_panel_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003632}
3633
3634static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003635input_panel_surface_set_overlay_panel(struct wl_client *client,
3636 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003637{
3638 struct input_panel_surface *input_panel_surface = resource->data;
3639 struct desktop_shell *shell = input_panel_surface->shell;
3640
3641 wl_list_insert(&shell->input_panel.surfaces,
3642 &input_panel_surface->link);
3643
3644 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003645}
3646
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003647static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003648 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003649 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003650};
3651
3652static void
3653destroy_input_panel_surface_resource(struct wl_resource *resource)
3654{
3655 struct input_panel_surface *ipsurf = resource->data;
3656
3657 destroy_input_panel_surface(ipsurf);
3658}
3659
3660static void
3661input_panel_get_input_panel_surface(struct wl_client *client,
3662 struct wl_resource *resource,
3663 uint32_t id,
3664 struct wl_resource *surface_resource)
3665{
3666 struct weston_surface *surface = surface_resource->data;
3667 struct desktop_shell *shell = resource->data;
3668 struct input_panel_surface *ipsurf;
3669
3670 if (get_input_panel_surface(surface)) {
3671 wl_resource_post_error(surface_resource,
3672 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003673 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003674 return;
3675 }
3676
3677 ipsurf = create_input_panel_surface(shell, surface);
3678 if (!ipsurf) {
3679 wl_resource_post_error(surface_resource,
3680 WL_DISPLAY_ERROR_INVALID_OBJECT,
3681 "surface->configure already set");
3682 return;
3683 }
3684
3685 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3686 ipsurf->resource.object.id = id;
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003687 ipsurf->resource.object.interface = &wl_input_panel_surface_interface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003688 ipsurf->resource.object.implementation =
3689 (void (**)(void)) &input_panel_surface_implementation;
3690 ipsurf->resource.data = ipsurf;
3691
3692 wl_client_add_resource(client, &ipsurf->resource);
3693}
3694
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003695static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003696 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003697};
3698
3699static void
3700unbind_input_panel(struct wl_resource *resource)
3701{
3702 struct desktop_shell *shell = resource->data;
3703
3704 shell->input_panel.binding = NULL;
3705 free(resource);
3706}
3707
3708static void
3709bind_input_panel(struct wl_client *client,
3710 void *data, uint32_t version, uint32_t id)
3711{
3712 struct desktop_shell *shell = data;
3713 struct wl_resource *resource;
3714
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003715 resource = wl_client_add_object(client, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003716 &input_panel_implementation,
3717 id, shell);
3718
3719 if (shell->input_panel.binding == NULL) {
3720 resource->destroy = unbind_input_panel;
3721 shell->input_panel.binding = resource;
3722 return;
3723 }
3724
3725 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3726 "interface object already bound");
3727 wl_resource_destroy(resource);
3728}
3729
Kristian Høgsberg07045392012-02-19 18:52:44 -05003730struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003731 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003732 struct weston_surface *current;
3733 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003734 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003735};
3736
3737static void
3738switcher_next(struct switcher *switcher)
3739{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003740 struct weston_surface *surface;
3741 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003742 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003743 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003744
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003745 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003746 switch (get_shell_surface_type(surface)) {
3747 case SHELL_SURFACE_TOPLEVEL:
3748 case SHELL_SURFACE_FULLSCREEN:
3749 case SHELL_SURFACE_MAXIMIZED:
3750 if (first == NULL)
3751 first = surface;
3752 if (prev == switcher->current)
3753 next = surface;
3754 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003755 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003756 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003757 weston_surface_damage(surface);
3758 break;
3759 default:
3760 break;
3761 }
Alex Wu1659daa2012-04-01 20:13:09 +08003762
3763 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003764 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003765 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003766 weston_surface_damage(surface);
3767 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003768 }
3769
3770 if (next == NULL)
3771 next = first;
3772
Alex Wu07b26062012-03-12 16:06:01 +08003773 if (next == NULL)
3774 return;
3775
Kristian Høgsberg07045392012-02-19 18:52:44 -05003776 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04003777 wl_signal_add(&next->resource.destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003778
3779 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003780 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003781
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003782 shsurf = get_shell_surface(switcher->current);
3783 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003784 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003785}
3786
3787static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003788switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003789{
3790 struct switcher *switcher =
3791 container_of(listener, struct switcher, listener);
3792
3793 switcher_next(switcher);
3794}
3795
3796static void
Daniel Stone351eb612012-05-31 15:27:47 -04003797switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003798{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003799 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003800 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003801 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003802
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003803 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003804 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003805 weston_surface_damage(surface);
3806 }
3807
Alex Wu07b26062012-03-12 16:06:01 +08003808 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003809 activate(switcher->shell, switcher->current,
3810 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003811 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003812 weston_keyboard_end_grab(keyboard);
3813 if (keyboard->input_method_resource)
3814 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003815 free(switcher);
3816}
3817
3818static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003819switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003820 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003821{
3822 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003823 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003824
Daniel Stonec9785ea2012-05-30 16:31:52 +01003825 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003826 switcher_next(switcher);
3827}
3828
3829static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003830switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04003831 uint32_t mods_depressed, uint32_t mods_latched,
3832 uint32_t mods_locked, uint32_t group)
3833{
3834 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003835 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003836
Daniel Stone351eb612012-05-31 15:27:47 -04003837 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3838 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003839}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003840
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003841static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003842 switcher_key,
3843 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003844};
3845
3846static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003847switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003848 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003849{
Tiago Vignattibe143262012-04-16 17:31:41 +03003850 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003851 struct switcher *switcher;
3852
3853 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003854 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003855 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003856 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003857 wl_list_init(&switcher->listener.link);
3858
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003859 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003860 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003861 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
3862 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003863 switcher_next(switcher);
3864}
3865
Pekka Paalanen3c647232011-12-22 13:43:43 +02003866static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003867backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003868 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003869{
3870 struct weston_compositor *compositor = data;
3871 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003872 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003873
3874 /* TODO: we're limiting to simple use cases, where we assume just
3875 * control on the primary display. We'd have to extend later if we
3876 * ever get support for setting backlights on random desktop LCD
3877 * panels though */
3878 output = get_default_output(compositor);
3879 if (!output)
3880 return;
3881
3882 if (!output->set_backlight)
3883 return;
3884
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003885 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3886 backlight_new = output->backlight_current - 25;
3887 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3888 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003889
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003890 if (backlight_new < 5)
3891 backlight_new = 5;
3892 if (backlight_new > 255)
3893 backlight_new = 255;
3894
3895 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003896 output->set_backlight(output, output->backlight_current);
3897}
3898
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003899struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003900 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003901 struct weston_seat *seat;
3902 uint32_t key[2];
3903 int key_released[2];
3904};
3905
3906static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003907debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003908 uint32_t key, uint32_t state)
3909{
3910 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3911 struct wl_resource *resource;
3912 struct wl_display *display;
3913 uint32_t serial;
3914 int send = 0, terminate = 0;
3915 int check_binding = 1;
3916 int i;
3917
3918 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3919 /* Do not run bindings on key releases */
3920 check_binding = 0;
3921
3922 for (i = 0; i < 2; i++)
3923 if (key == db->key[i])
3924 db->key_released[i] = 1;
3925
3926 if (db->key_released[0] && db->key_released[1]) {
3927 /* All key releases been swalled so end the grab */
3928 terminate = 1;
3929 } else if (key != db->key[0] && key != db->key[1]) {
3930 /* Should not swallow release of other keys */
3931 send = 1;
3932 }
3933 } else if (key == db->key[0] && !db->key_released[0]) {
3934 /* Do not check bindings for the first press of the binding
3935 * key. This allows it to be used as a debug shortcut.
3936 * We still need to swallow this event. */
3937 check_binding = 0;
3938 } else if (db->key[1]) {
3939 /* If we already ran a binding don't process another one since
3940 * we can't keep track of all the binding keys that were
3941 * pressed in order to swallow the release events. */
3942 send = 1;
3943 check_binding = 0;
3944 }
3945
3946 if (check_binding) {
3947 struct weston_compositor *ec = db->seat->compositor;
3948
3949 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3950 key, state)) {
3951 /* We ran a binding so swallow the press and keep the
3952 * grab to swallow the released too. */
3953 send = 0;
3954 terminate = 0;
3955 db->key[1] = key;
3956 } else {
3957 /* Terminate the grab since the key pressed is not a
3958 * debug binding key. */
3959 send = 1;
3960 terminate = 1;
3961 }
3962 }
3963
3964 if (send) {
3965 resource = grab->keyboard->focus_resource;
3966
3967 if (resource) {
3968 display = wl_client_get_display(resource->client);
3969 serial = wl_display_next_serial(display);
3970 wl_keyboard_send_key(resource, serial, time, key, state);
3971 }
3972 }
3973
3974 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003975 weston_keyboard_end_grab(grab->keyboard);
3976 if (grab->keyboard->input_method_resource)
3977 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003978 free(db);
3979 }
3980}
3981
3982static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003983debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003984 uint32_t mods_depressed, uint32_t mods_latched,
3985 uint32_t mods_locked, uint32_t group)
3986{
3987 struct wl_resource *resource;
3988
3989 resource = grab->keyboard->focus_resource;
3990 if (!resource)
3991 return;
3992
3993 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3994 mods_latched, mods_locked, group);
3995}
3996
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003997struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003998 debug_binding_key,
3999 debug_binding_modifiers
4000};
4001
4002static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004003debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004004{
4005 struct debug_binding_grab *grab;
4006
4007 grab = calloc(1, sizeof *grab);
4008 if (!grab)
4009 return;
4010
4011 grab->seat = (struct weston_seat *) seat;
4012 grab->key[0] = key;
4013 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004014 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004015}
4016
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004017static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004018force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004019 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004020{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004021 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004022 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004023 struct desktop_shell *shell = data;
4024 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004025 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004026
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004027 focus_surface = seat->keyboard->focus;
4028 if (!focus_surface)
4029 return;
4030
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004031 wl_signal_emit(&compositor->kill_signal, focus_surface);
4032
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004033 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03004034 wl_client_get_credentials(client, &pid, NULL, NULL);
4035
4036 /* Skip clients that we launched ourselves (the credentials of
4037 * the socketpair is ours) */
4038 if (pid == getpid())
4039 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004040
Daniel Stone325fc2d2012-05-30 16:31:58 +01004041 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004042}
4043
4044static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004045workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004046 uint32_t key, void *data)
4047{
4048 struct desktop_shell *shell = data;
4049 unsigned int new_index = shell->workspaces.current;
4050
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004051 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004052 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004053 if (new_index != 0)
4054 new_index--;
4055
4056 change_workspace(shell, new_index);
4057}
4058
4059static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004060workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004061 uint32_t key, void *data)
4062{
4063 struct desktop_shell *shell = data;
4064 unsigned int new_index = shell->workspaces.current;
4065
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004066 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004067 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004068 if (new_index < shell->workspaces.num - 1)
4069 new_index++;
4070
4071 change_workspace(shell, new_index);
4072}
4073
4074static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004075workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004076 uint32_t key, void *data)
4077{
4078 struct desktop_shell *shell = data;
4079 unsigned int new_index;
4080
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004081 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004082 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004083 new_index = key - KEY_F1;
4084 if (new_index >= shell->workspaces.num)
4085 new_index = shell->workspaces.num - 1;
4086
4087 change_workspace(shell, new_index);
4088}
4089
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004090static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004091workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004092 uint32_t key, void *data)
4093{
4094 struct desktop_shell *shell = data;
4095 unsigned int new_index = shell->workspaces.current;
4096
4097 if (shell->locked)
4098 return;
4099
4100 if (new_index != 0)
4101 new_index--;
4102
4103 take_surface_to_workspace_by_seat(shell, seat, new_index);
4104}
4105
4106static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004107workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004108 uint32_t key, void *data)
4109{
4110 struct desktop_shell *shell = data;
4111 unsigned int new_index = shell->workspaces.current;
4112
4113 if (shell->locked)
4114 return;
4115
4116 if (new_index < shell->workspaces.num - 1)
4117 new_index++;
4118
4119 take_surface_to_workspace_by_seat(shell, seat, new_index);
4120}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004121
4122static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004123shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004124{
Tiago Vignattibe143262012-04-16 17:31:41 +03004125 struct desktop_shell *shell =
4126 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004127 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004128
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004129 if (shell->child.client)
4130 wl_client_destroy(shell->child.client);
4131
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004132 wl_list_remove(&shell->idle_listener.link);
4133 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004134 wl_list_remove(&shell->show_input_panel_listener.link);
4135 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004136
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004137 wl_array_for_each(ws, &shell->workspaces.array)
4138 workspace_destroy(*ws);
4139 wl_array_release(&shell->workspaces.array);
4140
Pekka Paalanen3c647232011-12-22 13:43:43 +02004141 free(shell->screensaver.path);
4142 free(shell);
4143}
4144
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004145static void
4146shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4147{
4148 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004149 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004150
4151 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004152 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4153 MODIFIER_CTRL | MODIFIER_ALT,
4154 terminate_binding, ec);
4155 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4156 click_to_activate_binding,
4157 shell);
4158 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4159 MODIFIER_SUPER | MODIFIER_ALT,
4160 surface_opacity_binding, NULL);
4161 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4162 MODIFIER_SUPER, zoom_axis_binding,
4163 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004164
4165 /* configurable bindings */
4166 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004167 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4168 zoom_key_binding, NULL);
4169 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4170 zoom_key_binding, NULL);
4171 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4172 shell);
4173 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4174 resize_binding, shell);
4175 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4176 rotate_binding, NULL);
4177 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4178 shell);
4179 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4180 ec);
4181 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4182 backlight_binding, ec);
4183 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4184 ec);
4185 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4186 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004187 weston_compositor_add_key_binding(ec, KEY_K, mod,
4188 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004189 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4190 workspace_up_binding, shell);
4191 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4192 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004193 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4194 workspace_move_surface_up_binding,
4195 shell);
4196 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4197 workspace_move_surface_down_binding,
4198 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004199
4200 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4201 if (shell->workspaces.num > 1) {
4202 num_workspace_bindings = shell->workspaces.num;
4203 if (num_workspace_bindings > 6)
4204 num_workspace_bindings = 6;
4205 for (i = 0; i < num_workspace_bindings; i++)
4206 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4207 workspace_f_binding,
4208 shell);
4209 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004210
4211 /* Debug bindings */
4212 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4213 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004214}
4215
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004216WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004217module_init(struct weston_compositor *ec,
4218 int *argc, char *argv[], const char *config_file)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004219{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004220 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004221 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004222 struct workspace **pws;
4223 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004224 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004225
4226 shell = malloc(sizeof *shell);
4227 if (shell == NULL)
4228 return -1;
4229
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004230 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004231 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004232
4233 shell->destroy_listener.notify = shell_destroy;
4234 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004235 shell->idle_listener.notify = idle_handler;
4236 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4237 shell->wake_listener.notify = wake_handler;
4238 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004239 shell->show_input_panel_listener.notify = show_input_panels;
4240 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4241 shell->hide_input_panel_listener.notify = hide_input_panels;
4242 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004243 shell->update_input_panel_listener.notify = update_input_panels;
4244 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004245 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004246 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004247 ec->shell_interface.create_shell_surface = create_shell_surface;
4248 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004249 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004250 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004251 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004252 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004253
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004254 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004255
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004256 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4257 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004258 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4259 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004260 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004261
4262 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004263 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004264
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004265 shell_configuration(shell, config_file);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004266
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004267 for (i = 0; i < shell->workspaces.num; i++) {
4268 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4269 if (pws == NULL)
4270 return -1;
4271
4272 *pws = workspace_create();
4273 if (*pws == NULL)
4274 return -1;
4275 }
4276 activate_workspace(shell, 0);
4277
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004278 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004279 wl_list_init(&shell->workspaces.animation.link);
4280 shell->workspaces.animation.frame = animate_workspace_change_frame;
4281
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004282 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4283 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004284 return -1;
4285
Kristian Høgsberg75840622011-09-06 13:48:16 -04004286 if (wl_display_add_global(ec->wl_display,
4287 &desktop_shell_interface,
4288 shell, bind_desktop_shell) == NULL)
4289 return -1;
4290
Pekka Paalanen6e168112011-11-24 11:34:05 +02004291 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4292 shell, bind_screensaver) == NULL)
4293 return -1;
4294
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004295 if (wl_display_add_global(ec->wl_display, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004296 shell, bind_input_panel) == NULL)
4297 return -1;
4298
Jonas Ådahle9d22502012-08-29 22:13:01 +02004299 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4300 shell, bind_workspace_manager) == NULL)
4301 return -1;
4302
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004303 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004304
4305 loop = wl_display_get_event_loop(ec->wl_display);
4306 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004307
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004308 shell->screensaver.timer =
4309 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4310
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004311 wl_list_for_each(seat, &ec->seat_list, link)
4312 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004313
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004314 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004315
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004316 shell_fade(shell, FADE_IN);
4317
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004318 return 0;
4319}