blob: 4021e7d0144886f1e5aed37748564b65c324cb60 [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;
327 grab->grab.focus = &shsurf->surface->surface;
328
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øgsberg02bbabb2013-05-06 22:15:05 -0400331 weston_pointer_set_focus(pointer, &shell->grab_surface->surface,
332 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;
Jonas Ådahl56899442012-08-29 22:12:59 +0200501 struct wl_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200502
503 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Jonas Ådahl56899442012-08-29 22:12:59 +0200504 surface = state->keyboard_focus ?
505 &state->keyboard_focus->surface : NULL;
506
Kristian Høgsberge3148752013-05-06 23:19:49 -0400507 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200508 }
509}
510
511static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200512replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
513 struct weston_seat *seat)
514{
515 struct focus_state *state;
516 struct wl_surface *surface;
517
518 wl_list_for_each(state, &ws->focus_list, link) {
519 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400520 surface = seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200521 state->keyboard_focus =
522 (struct weston_surface *) surface;
523 return;
524 }
525 }
526}
527
528static void
529drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
530 struct weston_surface *surface)
531{
532 struct focus_state *state;
533
534 wl_list_for_each(state, &ws->focus_list, link)
535 if (state->keyboard_focus == surface)
536 state->keyboard_focus = NULL;
537}
538
539static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200540workspace_destroy(struct workspace *ws)
541{
Jonas Ådahl04769742012-06-13 00:01:24 +0200542 struct focus_state *state, *next;
543
544 wl_list_for_each_safe(state, next, &ws->focus_list, link)
545 focus_state_destroy(state);
546
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200547 free(ws);
548}
549
Jonas Ådahl04769742012-06-13 00:01:24 +0200550static void
551seat_destroyed(struct wl_listener *listener, void *data)
552{
553 struct weston_seat *seat = data;
554 struct focus_state *state, *next;
555 struct workspace *ws = container_of(listener,
556 struct workspace,
557 seat_destroyed_listener);
558
559 wl_list_for_each_safe(state, next, &ws->focus_list, link)
560 if (state->seat == seat)
561 wl_list_remove(&state->link);
562}
563
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200564static struct workspace *
565workspace_create(void)
566{
567 struct workspace *ws = malloc(sizeof *ws);
568 if (ws == NULL)
569 return NULL;
570
571 weston_layer_init(&ws->layer, NULL);
572
Jonas Ådahl04769742012-06-13 00:01:24 +0200573 wl_list_init(&ws->focus_list);
574 wl_list_init(&ws->seat_destroyed_listener.link);
575 ws->seat_destroyed_listener.notify = seat_destroyed;
576
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200577 return ws;
578}
579
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200580static int
581workspace_is_empty(struct workspace *ws)
582{
583 return wl_list_empty(&ws->layer.surface_list);
584}
585
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200586static struct workspace *
587get_workspace(struct desktop_shell *shell, unsigned int index)
588{
589 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200590 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200591 pws += index;
592 return *pws;
593}
594
595static struct workspace *
596get_current_workspace(struct desktop_shell *shell)
597{
598 return get_workspace(shell, shell->workspaces.current);
599}
600
601static void
602activate_workspace(struct desktop_shell *shell, unsigned int index)
603{
604 struct workspace *ws;
605
606 ws = get_workspace(shell, index);
607 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
608
609 shell->workspaces.current = index;
610}
611
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200612static unsigned int
613get_output_height(struct weston_output *output)
614{
615 return abs(output->region.extents.y1 - output->region.extents.y2);
616}
617
618static void
619surface_translate(struct weston_surface *surface, double d)
620{
621 struct shell_surface *shsurf = get_shell_surface(surface);
622 struct weston_transform *transform;
623
624 transform = &shsurf->workspace_transform;
625 if (wl_list_empty(&transform->link))
626 wl_list_insert(surface->geometry.transformation_list.prev,
627 &shsurf->workspace_transform.link);
628
629 weston_matrix_init(&shsurf->workspace_transform.matrix);
630 weston_matrix_translate(&shsurf->workspace_transform.matrix,
631 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200632 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200633}
634
635static void
636workspace_translate_out(struct workspace *ws, double fraction)
637{
638 struct weston_surface *surface;
639 unsigned int height;
640 double d;
641
642 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
643 height = get_output_height(surface->output);
644 d = height * fraction;
645
646 surface_translate(surface, d);
647 }
648}
649
650static void
651workspace_translate_in(struct workspace *ws, double fraction)
652{
653 struct weston_surface *surface;
654 unsigned int height;
655 double d;
656
657 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
658 height = get_output_height(surface->output);
659
660 if (fraction > 0)
661 d = -(height - height * fraction);
662 else
663 d = height + height * fraction;
664
665 surface_translate(surface, d);
666 }
667}
668
669static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200670broadcast_current_workspace_state(struct desktop_shell *shell)
671{
672 struct wl_resource *resource;
673
674 wl_list_for_each(resource, &shell->workspaces.client_list, link)
675 workspace_manager_send_state(resource,
676 shell->workspaces.current,
677 shell->workspaces.num);
678}
679
680static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200681reverse_workspace_change_animation(struct desktop_shell *shell,
682 unsigned int index,
683 struct workspace *from,
684 struct workspace *to)
685{
686 shell->workspaces.current = index;
687
688 shell->workspaces.anim_to = to;
689 shell->workspaces.anim_from = from;
690 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
691 shell->workspaces.anim_timestamp = 0;
692
Scott Moreau4272e632012-08-13 09:58:41 -0600693 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200694}
695
696static void
697workspace_deactivate_transforms(struct workspace *ws)
698{
699 struct weston_surface *surface;
700 struct shell_surface *shsurf;
701
702 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
703 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200704 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
705 wl_list_remove(&shsurf->workspace_transform.link);
706 wl_list_init(&shsurf->workspace_transform.link);
707 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200708 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200709 }
710}
711
712static void
713finish_workspace_change_animation(struct desktop_shell *shell,
714 struct workspace *from,
715 struct workspace *to)
716{
Scott Moreau4272e632012-08-13 09:58:41 -0600717 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200718
719 wl_list_remove(&shell->workspaces.animation.link);
720 workspace_deactivate_transforms(from);
721 workspace_deactivate_transforms(to);
722 shell->workspaces.anim_to = NULL;
723
724 wl_list_remove(&shell->workspaces.anim_from->layer.link);
725}
726
727static void
728animate_workspace_change_frame(struct weston_animation *animation,
729 struct weston_output *output, uint32_t msecs)
730{
731 struct desktop_shell *shell =
732 container_of(animation, struct desktop_shell,
733 workspaces.animation);
734 struct workspace *from = shell->workspaces.anim_from;
735 struct workspace *to = shell->workspaces.anim_to;
736 uint32_t t;
737 double x, y;
738
739 if (workspace_is_empty(from) && workspace_is_empty(to)) {
740 finish_workspace_change_animation(shell, from, to);
741 return;
742 }
743
744 if (shell->workspaces.anim_timestamp == 0) {
745 if (shell->workspaces.anim_current == 0.0)
746 shell->workspaces.anim_timestamp = msecs;
747 else
748 shell->workspaces.anim_timestamp =
749 msecs -
750 /* Invers of movement function 'y' below. */
751 (asin(1.0 - shell->workspaces.anim_current) *
752 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
753 M_2_PI);
754 }
755
756 t = msecs - shell->workspaces.anim_timestamp;
757
758 /*
759 * x = [0, π/2]
760 * y(x) = sin(x)
761 */
762 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
763 y = sin(x);
764
765 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600766 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200767
768 workspace_translate_out(from, shell->workspaces.anim_dir * y);
769 workspace_translate_in(to, shell->workspaces.anim_dir * y);
770 shell->workspaces.anim_current = y;
771
Scott Moreau4272e632012-08-13 09:58:41 -0600772 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200773 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200774 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200775 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200776}
777
778static void
779animate_workspace_change(struct desktop_shell *shell,
780 unsigned int index,
781 struct workspace *from,
782 struct workspace *to)
783{
784 struct weston_output *output;
785
786 int dir;
787
788 if (index > shell->workspaces.current)
789 dir = -1;
790 else
791 dir = 1;
792
793 shell->workspaces.current = index;
794
795 shell->workspaces.anim_dir = dir;
796 shell->workspaces.anim_from = from;
797 shell->workspaces.anim_to = to;
798 shell->workspaces.anim_current = 0.0;
799 shell->workspaces.anim_timestamp = 0;
800
801 output = container_of(shell->compositor->output_list.next,
802 struct weston_output, link);
803 wl_list_insert(&output->animation_list,
804 &shell->workspaces.animation.link);
805
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200806 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200807
808 workspace_translate_in(to, 0);
809
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400810 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200811
Scott Moreau4272e632012-08-13 09:58:41 -0600812 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200813}
814
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200815static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200816update_workspace(struct desktop_shell *shell, unsigned int index,
817 struct workspace *from, struct workspace *to)
818{
819 shell->workspaces.current = index;
820 wl_list_insert(&from->layer.link, &to->layer.link);
821 wl_list_remove(&from->layer.link);
822}
823
824static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200825change_workspace(struct desktop_shell *shell, unsigned int index)
826{
827 struct workspace *from;
828 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200829
830 if (index == shell->workspaces.current)
831 return;
832
833 /* Don't change workspace when there is any fullscreen surfaces. */
834 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
835 return;
836
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200837 from = get_current_workspace(shell);
838 to = get_workspace(shell, index);
839
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200840 if (shell->workspaces.anim_from == to &&
841 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200842 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200843 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200844 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200845 return;
846 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200847
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200848 if (shell->workspaces.anim_to != NULL)
849 finish_workspace_change_animation(shell,
850 shell->workspaces.anim_from,
851 shell->workspaces.anim_to);
852
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200853 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200854
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200855 if (workspace_is_empty(to) && workspace_is_empty(from))
856 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200857 else
858 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200859
860 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200861}
862
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200863static bool
864workspace_has_only(struct workspace *ws, struct weston_surface *surface)
865{
866 struct wl_list *list = &ws->layer.surface_list;
867 struct wl_list *e;
868
869 if (wl_list_empty(list))
870 return false;
871
872 e = list->next;
873
874 if (e->next != list)
875 return false;
876
877 return container_of(e, struct weston_surface, layer_link) == surface;
878}
879
880static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200881move_surface_to_workspace(struct desktop_shell *shell,
882 struct weston_surface *surface,
883 uint32_t workspace)
884{
885 struct workspace *from;
886 struct workspace *to;
887 struct weston_seat *seat;
888
889 if (workspace == shell->workspaces.current)
890 return;
891
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200892 if (workspace >= shell->workspaces.num)
893 workspace = shell->workspaces.num - 1;
894
Jonas Ådahle9d22502012-08-29 22:13:01 +0200895 from = get_current_workspace(shell);
896 to = get_workspace(shell, workspace);
897
898 wl_list_remove(&surface->layer_link);
899 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
900
901 drop_focus_state(shell, from, surface);
902 wl_list_for_each(seat, &shell->compositor->seat_list, link)
Kristian Høgsberg2bf87622013-05-07 23:17:41 -0400903 if (seat->keyboard &&
Kristian Høgsberge3148752013-05-06 23:19:49 -0400904 seat->keyboard->focus == &surface->surface)
905 weston_keyboard_set_focus(seat->keyboard, NULL);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200906
907 weston_surface_damage_below(surface);
908}
909
910static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200911take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400912 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200913 unsigned int index)
914{
915 struct weston_surface *surface =
Kristian Høgsberge3148752013-05-06 23:19:49 -0400916 (struct weston_surface *) seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200917 struct shell_surface *shsurf;
918 struct workspace *from;
919 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200920 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200921
922 if (surface == NULL ||
923 index == shell->workspaces.current)
924 return;
925
926 from = get_current_workspace(shell);
927 to = get_workspace(shell, index);
928
929 wl_list_remove(&surface->layer_link);
930 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
931
Jonas Ådahle9d22502012-08-29 22:13:01 +0200932 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200933 drop_focus_state(shell, from, surface);
934
935 if (shell->workspaces.anim_from == to &&
936 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200937 wl_list_remove(&to->layer.link);
938 wl_list_insert(from->layer.link.prev, &to->layer.link);
939
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200940 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200941 broadcast_current_workspace_state(shell);
942
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200943 return;
944 }
945
946 if (shell->workspaces.anim_to != NULL)
947 finish_workspace_change_animation(shell,
948 shell->workspaces.anim_from,
949 shell->workspaces.anim_to);
950
951 if (workspace_is_empty(from) &&
952 workspace_has_only(to, surface))
953 update_workspace(shell, index, from, to);
954 else {
955 shsurf = get_shell_surface(surface);
956 if (wl_list_empty(&shsurf->workspace_transform.link))
957 wl_list_insert(&shell->workspaces.anim_sticky_list,
958 &shsurf->workspace_transform.link);
959
960 animate_workspace_change(shell, index, from, to);
961 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200962
963 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200964
965 state = ensure_focus_state(shell, seat);
966 if (state != NULL)
967 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200968}
969
970static void
971workspace_manager_move_surface(struct wl_client *client,
972 struct wl_resource *resource,
973 struct wl_resource *surface_resource,
974 uint32_t workspace)
975{
976 struct desktop_shell *shell = resource->data;
977 struct weston_surface *surface =
978 (struct weston_surface *) surface_resource;
979
980 move_surface_to_workspace(shell, surface, workspace);
981}
982
983static const struct workspace_manager_interface workspace_manager_implementation = {
984 workspace_manager_move_surface,
985};
986
987static void
988unbind_resource(struct wl_resource *resource)
989{
990 wl_list_remove(&resource->link);
991 free(resource);
992}
993
994static void
995bind_workspace_manager(struct wl_client *client,
996 void *data, uint32_t version, uint32_t id)
997{
998 struct desktop_shell *shell = data;
999 struct wl_resource *resource;
1000
1001 resource = wl_client_add_object(client, &workspace_manager_interface,
1002 &workspace_manager_implementation,
1003 id, shell);
1004
1005 if (resource == NULL) {
1006 weston_log("couldn't add workspace manager object");
1007 return;
1008 }
1009
1010 resource->destroy = unbind_resource;
1011 wl_list_insert(&shell->workspaces.client_list, &resource->link);
1012
1013 workspace_manager_send_state(resource,
1014 shell->workspaces.current,
1015 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001016}
1017
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001018static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001019noop_grab_focus(struct weston_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001020 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001021{
1022 grab->focus = NULL;
1023}
1024
1025static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001026move_grab_motion(struct weston_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001027 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001028{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001029 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001030 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001031 struct shell_surface *shsurf = move->base.shsurf;
1032 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001033 int dx = wl_fixed_to_int(pointer->x + move->dx);
1034 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001035
1036 if (!shsurf)
1037 return;
1038
1039 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001040
Daniel Stone103db7f2012-05-08 17:17:55 +01001041 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001042 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001043
1044 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001045}
1046
1047static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001048move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001049 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001050{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001051 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1052 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001053 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001054 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001055
Daniel Stone4dbadb12012-05-30 16:31:51 +01001056 if (pointer->button_count == 0 &&
1057 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001058 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001059 free(grab);
1060 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001061}
1062
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001063static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001064 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001065 move_grab_motion,
1066 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001067};
1068
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001069static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001070surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001071{
1072 struct weston_move_grab *move;
1073
1074 if (!shsurf)
1075 return -1;
1076
1077 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1078 return 0;
1079
1080 move = malloc(sizeof *move);
1081 if (!move)
1082 return -1;
1083
1084 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001085 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001086 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001087 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001088
1089 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001090 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001091
1092 return 0;
1093}
1094
1095static void
1096shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1097 struct wl_resource *seat_resource, uint32_t serial)
1098{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001099 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001100 struct shell_surface *shsurf = resource->data;
1101
Kristian Høgsberge3148752013-05-06 23:19:49 -04001102 if (seat->pointer->button_count == 0 ||
1103 seat->pointer->grab_serial != serial ||
1104 seat->pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001105 return;
1106
Kristian Høgsberge3148752013-05-06 23:19:49 -04001107 if (surface_move(shsurf, seat) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001108 wl_resource_post_no_memory(resource);
1109}
1110
1111struct weston_resize_grab {
1112 struct shell_grab base;
1113 uint32_t edges;
1114 int32_t width, height;
1115};
1116
1117static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001118resize_grab_motion(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001119 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1120{
1121 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001122 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001123 struct shell_surface *shsurf = resize->base.shsurf;
1124 int32_t width, height;
1125 wl_fixed_t from_x, from_y;
1126 wl_fixed_t to_x, to_y;
1127
1128 if (!shsurf)
1129 return;
1130
1131 weston_surface_from_global_fixed(shsurf->surface,
1132 pointer->grab_x, pointer->grab_y,
1133 &from_x, &from_y);
1134 weston_surface_from_global_fixed(shsurf->surface,
1135 pointer->x, pointer->y, &to_x, &to_y);
1136
1137 width = resize->width;
1138 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1139 width += wl_fixed_to_int(from_x - to_x);
1140 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1141 width += wl_fixed_to_int(to_x - from_x);
1142 }
1143
1144 height = resize->height;
1145 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1146 height += wl_fixed_to_int(from_y - to_y);
1147 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1148 height += wl_fixed_to_int(to_y - from_y);
1149 }
1150
1151 shsurf->client->send_configure(shsurf->surface,
1152 resize->edges, width, height);
1153}
1154
1155static void
1156send_configure(struct weston_surface *surface,
1157 uint32_t edges, int32_t width, int32_t height)
1158{
1159 struct shell_surface *shsurf = get_shell_surface(surface);
1160
1161 wl_shell_surface_send_configure(&shsurf->resource,
1162 edges, width, height);
1163}
1164
1165static const struct weston_shell_client shell_client = {
1166 send_configure
1167};
1168
1169static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001170resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001171 uint32_t time, uint32_t button, uint32_t state_w)
1172{
1173 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001174 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001175 enum wl_pointer_button_state state = state_w;
1176
1177 if (pointer->button_count == 0 &&
1178 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1179 shell_grab_end(&resize->base);
1180 free(grab);
1181 }
1182}
1183
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001184static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001185 noop_grab_focus,
1186 resize_grab_motion,
1187 resize_grab_button,
1188};
1189
1190static int
1191surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001192 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001193{
1194 struct weston_resize_grab *resize;
1195
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001196 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1197 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001198 return 0;
1199
1200 if (edges == 0 || edges > 15 ||
1201 (edges & 3) == 3 || (edges & 12) == 12)
1202 return 0;
1203
1204 resize = malloc(sizeof *resize);
1205 if (!resize)
1206 return -1;
1207
1208 resize->edges = edges;
1209 resize->width = shsurf->surface->geometry.width;
1210 resize->height = shsurf->surface->geometry.height;
1211
1212 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001213 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001214
1215 return 0;
1216}
1217
1218static void
1219shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1220 struct wl_resource *seat_resource, uint32_t serial,
1221 uint32_t edges)
1222{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001223 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001224 struct shell_surface *shsurf = resource->data;
1225
1226 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1227 return;
1228
Kristian Høgsberge3148752013-05-06 23:19:49 -04001229 if (seat->pointer->button_count == 0 ||
1230 seat->pointer->grab_serial != serial ||
1231 seat->pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001232 return;
1233
Kristian Høgsberge3148752013-05-06 23:19:49 -04001234 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001235 wl_resource_post_no_memory(resource);
1236}
1237
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001238static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001239busy_cursor_grab_focus(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001240 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001241{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001242 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001243
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001244 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001245 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001246 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001247 }
1248}
1249
1250static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001251busy_cursor_grab_motion(struct weston_pointer_grab *grab,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001252 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001253{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001254}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001255
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001256static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001257busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001258 uint32_t time, uint32_t button, uint32_t state)
1259{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001260 struct shell_grab *grab = (struct shell_grab *) base;
1261 struct shell_surface *shsurf;
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001262 struct weston_surface *surface =
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001263 (struct weston_surface *) grab->grab.pointer->current;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001264 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001265
1266 shsurf = get_shell_surface(surface);
1267 if (shsurf && button == BTN_LEFT && state) {
1268 activate(shsurf->shell, shsurf->surface, seat);
1269 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001270 } else if (shsurf && button == BTN_RIGHT && state) {
1271 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001272 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001273 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001274}
1275
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001276static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001277 busy_cursor_grab_focus,
1278 busy_cursor_grab_motion,
1279 busy_cursor_grab_button,
1280};
1281
1282static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001283set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001284{
1285 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001286
1287 grab = malloc(sizeof *grab);
1288 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001289 return;
1290
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001291 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1292 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001293}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001294
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001295static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001296end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001297{
1298 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1299
1300 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001301 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001302 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001303 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001304}
1305
Scott Moreau9521d5e2012-04-19 13:06:17 -06001306static void
1307ping_timer_destroy(struct shell_surface *shsurf)
1308{
1309 if (!shsurf || !shsurf->ping_timer)
1310 return;
1311
1312 if (shsurf->ping_timer->source)
1313 wl_event_source_remove(shsurf->ping_timer->source);
1314
1315 free(shsurf->ping_timer);
1316 shsurf->ping_timer = NULL;
1317}
1318
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001319static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001320ping_timeout_handler(void *data)
1321{
1322 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001323 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001324
Scott Moreau9521d5e2012-04-19 13:06:17 -06001325 /* Client is not responding */
1326 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001327
1328 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001329 if (seat->pointer->focus == &shsurf->surface->surface)
1330 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001331
1332 return 1;
1333}
1334
1335static void
1336ping_handler(struct weston_surface *surface, uint32_t serial)
1337{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001338 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001339 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001340 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001341
1342 if (!shsurf)
1343 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001344 if (!shsurf->resource.client)
1345 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001346
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001347 if (shsurf->surface == shsurf->shell->grab_surface)
1348 return;
1349
Scott Moreauff1db4a2012-04-17 19:06:18 -06001350 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001351 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001352 if (!shsurf->ping_timer)
1353 return;
1354
1355 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001356 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1357 shsurf->ping_timer->source =
1358 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1359 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1360
1361 wl_shell_surface_send_ping(&shsurf->resource, serial);
1362 }
1363}
1364
1365static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001366handle_pointer_focus(struct wl_listener *listener, void *data)
1367{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001368 struct weston_pointer *pointer = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001369 struct weston_surface *surface =
1370 (struct weston_surface *) pointer->focus;
1371 struct weston_compositor *compositor;
1372 struct shell_surface *shsurf;
1373 uint32_t serial;
1374
1375 if (!surface)
1376 return;
1377
1378 compositor = surface->compositor;
1379 shsurf = get_shell_surface(surface);
1380
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001381 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001382 set_busy_cursor(shsurf, pointer);
1383 } else {
1384 serial = wl_display_next_serial(compositor->wl_display);
1385 ping_handler(surface, serial);
1386 }
1387}
1388
1389static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001390create_pointer_focus_listener(struct weston_seat *seat)
1391{
1392 struct wl_listener *listener;
1393
Kristian Høgsberge3148752013-05-06 23:19:49 -04001394 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001395 return;
1396
1397 listener = malloc(sizeof *listener);
1398 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001399 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001400}
1401
1402static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001403shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1404 uint32_t serial)
1405{
1406 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001407 struct desktop_shell *shell = shsurf->shell;
1408 struct weston_seat *seat;
1409 struct weston_compositor *ec = shsurf->surface->compositor;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001410 struct weston_pointer *pointer;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001411 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001412
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001413 if (shsurf->ping_timer == NULL)
1414 /* Just ignore unsolicited pong. */
1415 return;
1416
Scott Moreauff1db4a2012-04-17 19:06:18 -06001417 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001418 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001419 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001420 if (was_unresponsive) {
1421 /* Received pong from previously unresponsive client */
1422 wl_list_for_each(seat, &ec->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04001423 pointer = seat->pointer;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001424 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001425 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001426 pointer->current ==
1427 &shsurf->surface->surface)
1428 end_busy_cursor(shsurf, pointer);
1429 }
1430 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001431 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001432 }
1433}
1434
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001435static void
1436shell_surface_set_title(struct wl_client *client,
1437 struct wl_resource *resource, const char *title)
1438{
1439 struct shell_surface *shsurf = resource->data;
1440
1441 free(shsurf->title);
1442 shsurf->title = strdup(title);
1443}
1444
1445static void
1446shell_surface_set_class(struct wl_client *client,
1447 struct wl_resource *resource, const char *class)
1448{
1449 struct shell_surface *shsurf = resource->data;
1450
1451 free(shsurf->class);
1452 shsurf->class = strdup(class);
1453}
1454
Juan Zhao96879df2012-02-07 08:45:41 +08001455static struct weston_output *
1456get_default_output(struct weston_compositor *compositor)
1457{
1458 return container_of(compositor->output_list.next,
1459 struct weston_output, link);
1460}
1461
Alex Wu4539b082012-03-01 12:57:46 +08001462static void
1463shell_unset_fullscreen(struct shell_surface *shsurf)
1464{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001465 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001466 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001467 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1468 shell_surface_is_top_fullscreen(shsurf)) {
1469 weston_output_switch_mode(shsurf->fullscreen_output,
1470 shsurf->fullscreen_output->origin);
1471 }
Alex Wu4539b082012-03-01 12:57:46 +08001472 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1473 shsurf->fullscreen.framerate = 0;
1474 wl_list_remove(&shsurf->fullscreen.transform.link);
1475 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001476 if (shsurf->fullscreen.black_surface)
1477 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001478 shsurf->fullscreen.black_surface = NULL;
1479 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001480 weston_surface_set_position(shsurf->surface,
1481 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001482 if (shsurf->saved_rotation_valid) {
1483 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1484 &shsurf->rotation.transform.link);
1485 shsurf->saved_rotation_valid = false;
1486 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001487
1488 ws = get_current_workspace(shsurf->shell);
1489 wl_list_remove(&shsurf->surface->layer_link);
1490 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001491}
1492
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001493static void
1494shell_unset_maximized(struct shell_surface *shsurf)
1495{
1496 struct workspace *ws;
1497 /* undo all maximized things here */
1498 shsurf->output = get_default_output(shsurf->surface->compositor);
1499 weston_surface_set_position(shsurf->surface,
1500 shsurf->saved_x,
1501 shsurf->saved_y);
1502
1503 if (shsurf->saved_rotation_valid) {
1504 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1505 &shsurf->rotation.transform.link);
1506 shsurf->saved_rotation_valid = false;
1507 }
1508
1509 ws = get_current_workspace(shsurf->shell);
1510 wl_list_remove(&shsurf->surface->layer_link);
1511 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1512}
1513
Pekka Paalanen98262232011-12-01 10:42:22 +02001514static int
1515reset_shell_surface_type(struct shell_surface *surface)
1516{
1517 switch (surface->type) {
1518 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001519 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001520 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001521 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001522 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001523 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001524 case SHELL_SURFACE_NONE:
1525 case SHELL_SURFACE_TOPLEVEL:
1526 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001527 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001528 break;
1529 }
1530
1531 surface->type = SHELL_SURFACE_NONE;
1532 return 0;
1533}
1534
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001535static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001536set_surface_type(struct shell_surface *shsurf)
1537{
1538 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001539 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001540
1541 reset_shell_surface_type(shsurf);
1542
1543 shsurf->type = shsurf->next_type;
1544 shsurf->next_type = SHELL_SURFACE_NONE;
1545
1546 switch (shsurf->type) {
1547 case SHELL_SURFACE_TOPLEVEL:
1548 break;
1549 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001550 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001551 pes->geometry.x + shsurf->transient.x,
1552 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001553 break;
1554
1555 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001556 case SHELL_SURFACE_FULLSCREEN:
1557 shsurf->saved_x = surface->geometry.x;
1558 shsurf->saved_y = surface->geometry.y;
1559 shsurf->saved_position_valid = true;
1560
1561 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1562 wl_list_remove(&shsurf->rotation.transform.link);
1563 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001564 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001565 shsurf->saved_rotation_valid = true;
1566 }
1567 break;
1568
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001569 default:
1570 break;
1571 }
1572}
1573
1574static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001575set_toplevel(struct shell_surface *shsurf)
1576{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001577 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001578}
1579
1580static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001581shell_surface_set_toplevel(struct wl_client *client,
1582 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001583{
Pekka Paalanen98262232011-12-01 10:42:22 +02001584 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001585
Tiago Vignattibc052c92012-04-19 16:18:18 +03001586 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001587}
1588
1589static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001590set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001591 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001592{
1593 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001594 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001595 shsurf->transient.x = x;
1596 shsurf->transient.y = y;
1597 shsurf->transient.flags = flags;
1598 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1599}
1600
1601static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001602shell_surface_set_transient(struct wl_client *client,
1603 struct wl_resource *resource,
1604 struct wl_resource *parent_resource,
1605 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001606{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001607 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001608 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001609
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001610 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001611}
1612
Tiago Vignattibe143262012-04-16 17:31:41 +03001613static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001614shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001615{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001616 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001617}
1618
1619static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001620get_output_panel_height(struct desktop_shell *shell,
1621 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001622{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001623 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001624 int panel_height = 0;
1625
1626 if (!output)
1627 return 0;
1628
Juan Zhao4ab94682012-07-09 22:24:09 -07001629 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001630 if (surface->output == output) {
1631 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001632 break;
1633 }
1634 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001635
Juan Zhao96879df2012-02-07 08:45:41 +08001636 return panel_height;
1637}
1638
1639static void
1640shell_surface_set_maximized(struct wl_client *client,
1641 struct wl_resource *resource,
1642 struct wl_resource *output_resource )
1643{
1644 struct shell_surface *shsurf = resource->data;
1645 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001646 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001647 uint32_t edges = 0, panel_height = 0;
1648
1649 /* get the default output, if the client set it as NULL
1650 check whether the ouput is available */
1651 if (output_resource)
1652 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001653 else if (es->output)
1654 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001655 else
1656 shsurf->output = get_default_output(es->compositor);
1657
Tiago Vignattibe143262012-04-16 17:31:41 +03001658 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001659 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001660 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001661
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001662 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001663 shsurf->output->width,
1664 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001665
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001666 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001667}
1668
Alex Wu21858432012-04-01 20:13:08 +08001669static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001670black_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 +08001671
Alex Wu4539b082012-03-01 12:57:46 +08001672static struct weston_surface *
1673create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001674 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001675 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001676{
1677 struct weston_surface *surface = NULL;
1678
1679 surface = weston_surface_create(ec);
1680 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001681 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001682 return NULL;
1683 }
1684
Alex Wu21858432012-04-01 20:13:08 +08001685 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001686 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001687 weston_surface_configure(surface, x, y, w, h);
1688 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001689 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001690 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001691 pixman_region32_fini(&surface->input);
1692 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001693
Alex Wu4539b082012-03-01 12:57:46 +08001694 return surface;
1695}
1696
1697/* Create black surface and append it to the associated fullscreen surface.
1698 * Handle size dismatch and positioning according to the method. */
1699static void
1700shell_configure_fullscreen(struct shell_surface *shsurf)
1701{
1702 struct weston_output *output = shsurf->fullscreen_output;
1703 struct weston_surface *surface = shsurf->surface;
1704 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001705 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001706
Alex Wu4539b082012-03-01 12:57:46 +08001707 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001708 shsurf->fullscreen.black_surface =
1709 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001710 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001711 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001712 output->width,
1713 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001714
1715 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1716 wl_list_insert(&surface->layer_link,
1717 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001718 shsurf->fullscreen.black_surface->output = output;
1719
1720 switch (shsurf->fullscreen.type) {
1721 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001722 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001723 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001724 break;
1725 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001726 /* 1:1 mapping between surface and output dimensions */
1727 if (output->width == surface->geometry.width &&
1728 output->height == surface->geometry.height) {
1729 weston_surface_set_position(surface, output->x, output->y);
1730 break;
1731 }
1732
Alex Wu4539b082012-03-01 12:57:46 +08001733 matrix = &shsurf->fullscreen.transform.matrix;
1734 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001735
Scott Moreau1bad5db2012-08-18 01:04:05 -06001736 output_aspect = (float) output->width /
1737 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001738 surface_aspect = (float) surface->geometry.width /
1739 (float) surface->geometry.height;
1740 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001741 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001742 (float) surface->geometry.width;
1743 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001744 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001745 (float) surface->geometry.height;
1746
Alex Wu4539b082012-03-01 12:57:46 +08001747 weston_matrix_scale(matrix, scale, scale, 1);
1748 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001749 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001750 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001751 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1752 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001753 weston_surface_set_position(surface, x, y);
1754
Alex Wu4539b082012-03-01 12:57:46 +08001755 break;
1756 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001757 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001758 struct weston_mode mode = {0,
Alex Wubd3354b2012-04-17 17:20:49 +08001759 surface->geometry.width,
1760 surface->geometry.height,
1761 shsurf->fullscreen.framerate};
1762
1763 if (weston_output_switch_mode(output, &mode) == 0) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001764 weston_surface_configure(shsurf->fullscreen.black_surface,
Alex Wubd3354b2012-04-17 17:20:49 +08001765 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001766 output->width,
1767 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001768 weston_surface_set_position(surface, output->x, output->y);
1769 break;
1770 }
1771 }
Alex Wu4539b082012-03-01 12:57:46 +08001772 break;
1773 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1774 break;
1775 default:
1776 break;
1777 }
1778}
1779
1780/* make the fullscreen and black surface at the top */
1781static void
1782shell_stack_fullscreen(struct shell_surface *shsurf)
1783{
Alex Wubd3354b2012-04-17 17:20:49 +08001784 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001785 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001786 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001787
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001788 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001789 wl_list_insert(&shell->fullscreen_layer.surface_list,
1790 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001791 weston_surface_damage(surface);
1792
1793 if (!shsurf->fullscreen.black_surface)
1794 shsurf->fullscreen.black_surface =
1795 create_black_surface(surface->compositor,
1796 surface,
1797 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001798 output->width,
1799 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001800
1801 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001802 wl_list_insert(&surface->layer_link,
1803 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001804 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001805}
1806
1807static void
1808shell_map_fullscreen(struct shell_surface *shsurf)
1809{
Alex Wu4539b082012-03-01 12:57:46 +08001810 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001811 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001812}
1813
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001814static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001815set_fullscreen(struct shell_surface *shsurf,
1816 uint32_t method,
1817 uint32_t framerate,
1818 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001819{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001820 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001821
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001822 if (output)
1823 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001824 else if (es->output)
1825 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001826 else
1827 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001828
Alex Wu4539b082012-03-01 12:57:46 +08001829 shsurf->fullscreen_output = shsurf->output;
1830 shsurf->fullscreen.type = method;
1831 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001832 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001833
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001834 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001835 shsurf->output->width,
1836 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001837}
1838
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001839static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001840shell_surface_set_fullscreen(struct wl_client *client,
1841 struct wl_resource *resource,
1842 uint32_t method,
1843 uint32_t framerate,
1844 struct wl_resource *output_resource)
1845{
1846 struct shell_surface *shsurf = resource->data;
1847 struct weston_output *output;
1848
1849 if (output_resource)
1850 output = output_resource->data;
1851 else
1852 output = NULL;
1853
1854 set_fullscreen(shsurf, method, framerate, output);
1855}
1856
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001857static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001858
1859static void
1860destroy_shell_seat(struct wl_listener *listener, void *data)
1861{
1862 struct shell_seat *shseat =
1863 container_of(listener,
1864 struct shell_seat, seat_destroy_listener);
1865 struct shell_surface *shsurf, *prev = NULL;
1866
1867 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001868 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001869 shseat->popup_grab.client = NULL;
1870
1871 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1872 shsurf->popup.shseat = NULL;
1873 if (prev) {
1874 wl_list_init(&prev->popup.grab_link);
1875 }
1876 prev = shsurf;
1877 }
1878 wl_list_init(&prev->popup.grab_link);
1879 }
1880
1881 wl_list_remove(&shseat->seat_destroy_listener.link);
1882 free(shseat);
1883}
1884
1885static struct shell_seat *
1886create_shell_seat(struct weston_seat *seat)
1887{
1888 struct shell_seat *shseat;
1889
1890 shseat = calloc(1, sizeof *shseat);
1891 if (!shseat) {
1892 weston_log("no memory to allocate shell seat\n");
1893 return NULL;
1894 }
1895
1896 shseat->seat = seat;
1897 wl_list_init(&shseat->popup_grab.surfaces_list);
1898
1899 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1900 wl_signal_add(&seat->destroy_signal,
1901 &shseat->seat_destroy_listener);
1902
1903 return shseat;
1904}
1905
1906static struct shell_seat *
1907get_shell_seat(struct weston_seat *seat)
1908{
1909 struct wl_listener *listener;
1910
1911 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
1912 if (listener == NULL)
1913 return create_shell_seat(seat);
1914
1915 return container_of(listener,
1916 struct shell_seat, seat_destroy_listener);
1917}
1918
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001919static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001920popup_grab_focus(struct weston_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001921 struct wl_surface *surface,
1922 wl_fixed_t x,
1923 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001924{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001925 struct weston_pointer *pointer = grab->pointer;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001926 struct shell_seat *shseat =
1927 container_of(grab, struct shell_seat, popup_grab.grab);
1928 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001929
Pekka Paalanencb108432012-01-19 16:25:40 +02001930 if (surface && surface->resource.client == client) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001931 weston_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001932 grab->focus = surface;
1933 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001934 weston_pointer_set_focus(pointer, NULL,
1935 wl_fixed_from_int(0),
1936 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001937 grab->focus = NULL;
1938 }
1939}
1940
1941static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001942popup_grab_motion(struct weston_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001943 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001944{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001945 struct wl_resource *resource;
1946
Daniel Stone37816df2012-05-16 18:45:18 +01001947 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001948 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001949 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001950}
1951
1952static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001953popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001954 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001955{
1956 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001957 struct shell_seat *shseat =
1958 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001959 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001960 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001961 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001962
Daniel Stone37816df2012-05-16 18:45:18 +01001963 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001964 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001965 display = wl_client_get_display(resource->client);
1966 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001967 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001968 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01001969 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001970 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001971 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001972 }
1973
Daniel Stone4dbadb12012-05-30 16:31:51 +01001974 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01001975 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001976}
1977
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001978static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001979 popup_grab_focus,
1980 popup_grab_motion,
1981 popup_grab_button,
1982};
1983
1984static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001985popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001986{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001987 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001988 struct shell_seat *shseat =
1989 container_of(grab, struct shell_seat, popup_grab.grab);
1990 struct shell_surface *shsurf;
1991 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001992
1993 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001994 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001995 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02001996 shseat->popup_grab.grab.interface = NULL;
1997 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01001998 /* Send the popup_done event to all the popups open */
1999 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2000 wl_shell_surface_send_popup_done(&shsurf->resource);
2001 shsurf->popup.shseat = NULL;
2002 if (prev) {
2003 wl_list_init(&prev->popup.grab_link);
2004 }
2005 prev = shsurf;
2006 }
2007 wl_list_init(&prev->popup.grab_link);
2008 wl_list_init(&shseat->popup_grab.surfaces_list);
2009 }
2010}
2011
2012static void
2013add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2014{
Kristian Høgsberge3148752013-05-06 23:19:49 -04002015 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002016
2017 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
2018 shseat->popup_grab.client = shsurf->surface->surface.resource.client;
2019 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002020 /* We must make sure here that this popup was opened after
2021 * a mouse press, and not just by moving around with other
2022 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002023 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002024 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002025
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002026 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002027 }
2028 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2029}
2030
2031static void
2032remove_popup_grab(struct shell_surface *shsurf)
2033{
2034 struct shell_seat *shseat = shsurf->popup.shseat;
2035
2036 wl_list_remove(&shsurf->popup.grab_link);
2037 wl_list_init(&shsurf->popup.grab_link);
2038 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002039 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002040 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002041 }
2042}
2043
2044static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002045shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002046{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002047 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002048 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002049 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002050
2051 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002052
Pekka Paalanen483243f2013-03-08 14:56:50 +02002053 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002054 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2055 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002056
Kristian Høgsberge3148752013-05-06 23:19:49 -04002057 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002058 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002059 } else {
2060 wl_shell_surface_send_popup_done(&shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002061 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002062 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002063}
2064
2065static void
2066shell_surface_set_popup(struct wl_client *client,
2067 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002068 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002069 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002070 struct wl_resource *parent_resource,
2071 int32_t x, int32_t y, uint32_t flags)
2072{
2073 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002074
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002075 shsurf->type = SHELL_SURFACE_POPUP;
2076 shsurf->parent = parent_resource->data;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002077 shsurf->popup.shseat = get_shell_seat(seat_resource->data);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002078 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002079 shsurf->popup.x = x;
2080 shsurf->popup.y = y;
2081}
2082
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002083static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002084 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002085 shell_surface_move,
2086 shell_surface_resize,
2087 shell_surface_set_toplevel,
2088 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002089 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002090 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002091 shell_surface_set_maximized,
2092 shell_surface_set_title,
2093 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002094};
2095
2096static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002097destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002098{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002099 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2100 remove_popup_grab(shsurf);
2101 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002102
Alex Wubd3354b2012-04-17 17:20:49 +08002103 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2104 shell_surface_is_top_fullscreen(shsurf)) {
2105 weston_output_switch_mode(shsurf->fullscreen_output,
2106 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002107 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002108
Alex Wuaa08e2d2012-03-05 11:01:40 +08002109 if (shsurf->fullscreen.black_surface)
2110 weston_surface_destroy(shsurf->fullscreen.black_surface);
2111
Alex Wubd3354b2012-04-17 17:20:49 +08002112 /* As destroy_resource() use wl_list_for_each_safe(),
2113 * we can always remove the listener.
2114 */
2115 wl_list_remove(&shsurf->surface_destroy_listener.link);
2116 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002117 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002118 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002119
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002120 wl_list_remove(&shsurf->link);
2121 free(shsurf);
2122}
2123
2124static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002125shell_destroy_shell_surface(struct wl_resource *resource)
2126{
2127 struct shell_surface *shsurf = resource->data;
2128
2129 destroy_shell_surface(shsurf);
2130}
2131
2132static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002133shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002134{
2135 struct shell_surface *shsurf = container_of(listener,
2136 struct shell_surface,
2137 surface_destroy_listener);
2138
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002139 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002140 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002141 } else {
2142 wl_signal_emit(&shsurf->resource.destroy_signal,
2143 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002144 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002145 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002146}
2147
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002148static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002149shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002150
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002151static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002152get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002153{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002154 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002155 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002156 else
2157 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002158}
2159
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002160static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002161create_shell_surface(void *shell, struct weston_surface *surface,
2162 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002163{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002164 struct shell_surface *shsurf;
2165
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002166 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002167 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002168 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002169 }
2170
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002171 shsurf = calloc(1, sizeof *shsurf);
2172 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002173 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002174 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002175 }
2176
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002177 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002178 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002179
Tiago Vignattibc052c92012-04-19 16:18:18 +03002180 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002181 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002182 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002183 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002184 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002185 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2186 shsurf->fullscreen.framerate = 0;
2187 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002188 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002189 wl_list_init(&shsurf->fullscreen.transform.link);
2190
Tiago Vignattibc052c92012-04-19 16:18:18 +03002191 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002192 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2193 wl_signal_add(&surface->surface.resource.destroy_signal,
2194 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002195
2196 /* init link so its safe to always remove it in destroy_shell_surface */
2197 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002198 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002199
Pekka Paalanen460099f2012-01-20 16:48:25 +02002200 /* empty when not in use */
2201 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002202 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002203
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002204 wl_list_init(&shsurf->workspace_transform.link);
2205
Pekka Paalanen98262232011-12-01 10:42:22 +02002206 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002207 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002208
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002209 shsurf->client = client;
2210
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002211 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002212}
2213
2214static void
2215shell_get_shell_surface(struct wl_client *client,
2216 struct wl_resource *resource,
2217 uint32_t id,
2218 struct wl_resource *surface_resource)
2219{
2220 struct weston_surface *surface = surface_resource->data;
2221 struct desktop_shell *shell = resource->data;
2222 struct shell_surface *shsurf;
2223
2224 if (get_shell_surface(surface)) {
2225 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002226 WL_DISPLAY_ERROR_INVALID_OBJECT,
2227 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002228 return;
2229 }
2230
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002231 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002232 if (!shsurf) {
2233 wl_resource_post_error(surface_resource,
2234 WL_DISPLAY_ERROR_INVALID_OBJECT,
2235 "surface->configure already set");
2236 return;
2237 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002238
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002239 shsurf->resource.destroy = shell_destroy_shell_surface;
2240 shsurf->resource.object.id = id;
2241 shsurf->resource.object.interface = &wl_shell_surface_interface;
2242 shsurf->resource.object.implementation =
2243 (void (**)(void)) &shell_surface_implementation;
2244 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002245
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002246 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002247}
2248
2249static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002250 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002251};
2252
Kristian Høgsberg07937562011-04-12 17:25:42 -04002253static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002254shell_fade(struct desktop_shell *shell, enum fade_type type);
2255
2256static int
2257screensaver_timeout(void *data)
2258{
2259 struct desktop_shell *shell = data;
2260
2261 shell_fade(shell, FADE_OUT);
2262
2263 return 1;
2264}
2265
2266static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002267handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002268{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002269 struct desktop_shell *shell =
2270 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002271
Pekka Paalanen18027e52011-12-02 16:31:49 +02002272 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002273
2274 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002275 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002276}
2277
2278static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002279launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002280{
2281 if (shell->screensaver.binding)
2282 return;
2283
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002284 if (!shell->screensaver.path) {
2285 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002286 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002287 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002288
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002289 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002290 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002291 return;
2292 }
2293
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002294 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002295 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002296 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002297 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002298}
2299
2300static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002301terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002302{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002303 if (shell->screensaver.process.pid == 0)
2304 return;
2305
2306 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002307}
2308
2309static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002310configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002311{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002312 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002313
Giulio Camuffo184df502013-02-21 11:29:21 +01002314 if (width == 0)
2315 return;
2316
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002317 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2318 if (s->output == es->output && s != es) {
2319 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002320 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002321 }
2322 }
2323
Giulio Camuffo184df502013-02-21 11:29:21 +01002324 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002325
2326 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002327 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002328 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002329 }
2330}
2331
2332static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002333background_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 -04002334{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002335 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002336
Giulio Camuffo184df502013-02-21 11:29:21 +01002337 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002338}
2339
2340static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002341desktop_shell_set_background(struct wl_client *client,
2342 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002343 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002344 struct wl_resource *surface_resource)
2345{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002346 struct desktop_shell *shell = resource->data;
2347 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002348
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002349 if (surface->configure) {
2350 wl_resource_post_error(surface_resource,
2351 WL_DISPLAY_ERROR_INVALID_OBJECT,
2352 "surface role already assigned");
2353 return;
2354 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002355
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002356 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002357 surface->configure_private = shell;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002358 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002359 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002360 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002361 surface->output->width,
2362 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002363}
2364
2365static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002366panel_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 -04002367{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002368 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002369
Giulio Camuffo184df502013-02-21 11:29:21 +01002370 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002371}
2372
2373static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002374desktop_shell_set_panel(struct wl_client *client,
2375 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002376 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002377 struct wl_resource *surface_resource)
2378{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002379 struct desktop_shell *shell = resource->data;
2380 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002381
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002382 if (surface->configure) {
2383 wl_resource_post_error(surface_resource,
2384 WL_DISPLAY_ERROR_INVALID_OBJECT,
2385 "surface role already assigned");
2386 return;
2387 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002388
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002389 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002390 surface->configure_private = shell;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002391 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002392 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002393 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002394 surface->output->width,
2395 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002396}
2397
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002398static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002399lock_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 -04002400{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002401 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002402
Giulio Camuffo184df502013-02-21 11:29:21 +01002403 if (width == 0)
2404 return;
2405
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002406 center_on_output(surface, get_default_output(shell->compositor));
2407
2408 if (!weston_surface_is_mapped(surface)) {
2409 wl_list_insert(&shell->lock_layer.surface_list,
2410 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002411 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002412 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002413 }
2414}
2415
2416static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002417handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002418{
Tiago Vignattibe143262012-04-16 17:31:41 +03002419 struct desktop_shell *shell =
2420 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002421
Martin Minarik6d118362012-06-07 18:01:59 +02002422 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002423 shell->lock_surface = NULL;
2424}
2425
2426static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002427desktop_shell_set_lock_surface(struct wl_client *client,
2428 struct wl_resource *resource,
2429 struct wl_resource *surface_resource)
2430{
Tiago Vignattibe143262012-04-16 17:31:41 +03002431 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002432 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002433
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002434 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002435
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002436 if (!shell->locked)
2437 return;
2438
Pekka Paalanen98262232011-12-01 10:42:22 +02002439 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002440
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002441 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2442 wl_signal_add(&surface_resource->destroy_signal,
2443 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002444
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002445 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002446 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002447}
2448
2449static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002450resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002451{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002452 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002453
Pekka Paalanen77346a62011-11-30 16:26:35 +02002454 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002455
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002456 wl_list_remove(&shell->lock_layer.link);
2457 wl_list_insert(&shell->compositor->cursor_layer.link,
2458 &shell->fullscreen_layer.link);
2459 wl_list_insert(&shell->fullscreen_layer.link,
2460 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002461 if (shell->showing_input_panels) {
2462 wl_list_insert(&shell->panel_layer.link,
2463 &shell->input_panel_layer.link);
2464 wl_list_insert(&shell->input_panel_layer.link,
2465 &ws->layer.link);
2466 } else {
2467 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2468 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002469
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002470 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002471
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002472 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002473 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002474 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002475}
2476
2477static void
2478desktop_shell_unlock(struct wl_client *client,
2479 struct wl_resource *resource)
2480{
Tiago Vignattibe143262012-04-16 17:31:41 +03002481 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002482
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002483 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002484
2485 if (shell->locked)
2486 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002487}
2488
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002489static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002490desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002491 struct wl_resource *resource,
2492 struct wl_resource *surface_resource)
2493{
2494 struct desktop_shell *shell = resource->data;
2495
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002496 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002497}
2498
Kristian Høgsberg75840622011-09-06 13:48:16 -04002499static const struct desktop_shell_interface desktop_shell_implementation = {
2500 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002501 desktop_shell_set_panel,
2502 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002503 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002504 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002505};
2506
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002507static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002508get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002509{
2510 struct shell_surface *shsurf;
2511
2512 shsurf = get_shell_surface(surface);
2513 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002514 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002515 return shsurf->type;
2516}
2517
Kristian Høgsberg75840622011-09-06 13:48:16 -04002518static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002519move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002520{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002521 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002522 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002523 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002524
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002525 if (surface == NULL)
2526 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002527
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002528 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002529 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2530 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002531 return;
2532
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002533 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002534}
2535
2536static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002537resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002538{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002539 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002540 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002541 uint32_t edges = 0;
2542 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002543 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002544
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002545 if (surface == NULL)
2546 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002547
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002548 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002549 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2550 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002551 return;
2552
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002553 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002554 wl_fixed_to_int(seat->pointer->grab_x),
2555 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002556 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002557
Pekka Paalanen60921e52012-01-25 15:55:43 +02002558 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002559 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002560 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002561 edges |= 0;
2562 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002563 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002564
Pekka Paalanen60921e52012-01-25 15:55:43 +02002565 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002566 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002567 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002568 edges |= 0;
2569 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002570 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002571
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002572 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002573}
2574
2575static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002576surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002577 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002578{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002579 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002580 struct shell_surface *shsurf;
2581 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002582 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002583
2584 if (surface == NULL)
2585 return;
2586
2587 shsurf = get_shell_surface(surface);
2588 if (!shsurf)
2589 return;
2590
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002591 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002592
Scott Moreau02709af2012-05-22 01:54:10 -06002593 if (surface->alpha > 1.0)
2594 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002595 if (surface->alpha < step)
2596 surface->alpha = step;
2597
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002598 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002599 weston_surface_damage(surface);
2600}
2601
2602static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002603do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002604 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002605{
Daniel Stone37816df2012-05-16 18:45:18 +01002606 struct weston_seat *ws = (struct weston_seat *) seat;
2607 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002608 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002609 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002610
2611 wl_list_for_each(output, &compositor->output_list, link) {
2612 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002613 wl_fixed_to_double(seat->pointer->x),
2614 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002615 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002616 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002617 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002618 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002619 increment = -output->zoom.increment;
2620 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002621 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002622 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002623 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002624 else
2625 increment = 0;
2626
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002627 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002628
Scott Moreaue6603982012-06-11 13:07:51 -06002629 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002630 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002631 else if (output->zoom.level > output->zoom.max_level)
2632 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002633 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002634 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002635 output->disable_planes++;
2636 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002637
Scott Moreaue6603982012-06-11 13:07:51 -06002638 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002639
Scott Moreau8dacaab2012-06-17 18:10:58 -06002640 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002641 }
2642 }
2643}
2644
Scott Moreauccbf29d2012-02-22 14:21:41 -07002645static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002646zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002647 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002648{
2649 do_zoom(seat, time, 0, axis, value);
2650}
2651
2652static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002653zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002654 void *data)
2655{
2656 do_zoom(seat, time, key, 0, 0);
2657}
2658
2659static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002660terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002661 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002662{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002663 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002664
Daniel Stone325fc2d2012-05-30 16:31:58 +01002665 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002666}
2667
2668static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002669rotate_grab_motion(struct weston_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002670 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002671{
2672 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002673 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002674 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002675 struct shell_surface *shsurf = rotate->base.shsurf;
2676 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002677 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002678
2679 if (!shsurf)
2680 return;
2681
2682 surface = shsurf->surface;
2683
2684 cx = 0.5f * surface->geometry.width;
2685 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002686
Daniel Stone37816df2012-05-16 18:45:18 +01002687 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2688 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002689 r = sqrtf(dx * dx + dy * dy);
2690
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002691 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002692 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002693
2694 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002695 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002696 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002697
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002698 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002699 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002700
2701 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002702 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002703 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002704 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002705 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002706
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002707 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002708 &shsurf->surface->geometry.transformation_list,
2709 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002710 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002711 wl_list_init(&shsurf->rotation.transform.link);
2712 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002713 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002714 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002715
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002716 /* We need to adjust the position of the surface
2717 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002718 cposx = surface->geometry.x + cx;
2719 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002720 dposx = rotate->center.x - cposx;
2721 dposy = rotate->center.y - cposy;
2722 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002723 weston_surface_set_position(surface,
2724 surface->geometry.x + dposx,
2725 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002726 }
2727
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002728 /* Repaint implies weston_surface_update_transform(), which
2729 * lazily applies the damage due to rotation update.
2730 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002731 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002732}
2733
2734static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002735rotate_grab_button(struct weston_pointer_grab *grab,
2736 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002737{
2738 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002739 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002740 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002741 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002742 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002743
Daniel Stone4dbadb12012-05-30 16:31:51 +01002744 if (pointer->button_count == 0 &&
2745 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002746 if (shsurf)
2747 weston_matrix_multiply(&shsurf->rotation.rotation,
2748 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002749 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002750 free(rotate);
2751 }
2752}
2753
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002754static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002755 noop_grab_focus,
2756 rotate_grab_motion,
2757 rotate_grab_button,
2758};
2759
2760static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002761surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002762{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002763 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002764 float dx, dy;
2765 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002766
Pekka Paalanen460099f2012-01-20 16:48:25 +02002767 rotate = malloc(sizeof *rotate);
2768 if (!rotate)
2769 return;
2770
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002771 weston_surface_to_global_float(surface->surface,
2772 surface->surface->geometry.width / 2,
2773 surface->surface->geometry.height / 2,
2774 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002775
Daniel Stone37816df2012-05-16 18:45:18 +01002776 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2777 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002778 r = sqrtf(dx * dx + dy * dy);
2779 if (r > 20.0f) {
2780 struct weston_matrix inverse;
2781
2782 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002783 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002784 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002785
2786 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002787 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002788 } else {
2789 weston_matrix_init(&surface->rotation.rotation);
2790 weston_matrix_init(&rotate->rotation);
2791 }
2792
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002793 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2794 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002795}
2796
2797static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002798rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002799 void *data)
2800{
2801 struct weston_surface *base_surface =
2802 (struct weston_surface *) seat->pointer->focus;
2803 struct shell_surface *surface;
2804
2805 if (base_surface == NULL)
2806 return;
2807
2808 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002809 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2810 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002811 return;
2812
2813 surface_rotate(surface, seat);
2814}
2815
2816static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002817lower_fullscreen_layer(struct desktop_shell *shell)
2818{
2819 struct workspace *ws;
2820 struct weston_surface *surface, *prev;
2821
2822 ws = get_current_workspace(shell);
2823 wl_list_for_each_reverse_safe(surface, prev,
2824 &shell->fullscreen_layer.surface_list,
2825 layer_link)
2826 weston_surface_restack(surface, &ws->layer.surface_list);
2827}
2828
2829static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002830activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002831 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002832{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002833 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002834 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002835
Daniel Stone37816df2012-05-16 18:45:18 +01002836 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002837
Jonas Ådahl8538b222012-08-29 22:13:03 +02002838 state = ensure_focus_state(shell, seat);
2839 if (state == NULL)
2840 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002841
2842 state->keyboard_focus = es;
2843 wl_list_remove(&state->surface_destroy_listener.link);
2844 wl_signal_add(&es->surface.resource.destroy_signal,
2845 &state->surface_destroy_listener);
2846
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002847 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002848 case SHELL_SURFACE_FULLSCREEN:
2849 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002850 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002851 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002852 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002853 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002854 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002855 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002856 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002857 }
2858}
2859
Alex Wu21858432012-04-01 20:13:08 +08002860/* no-op func for checking black surface */
2861static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002862black_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 +08002863{
2864}
2865
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002866static bool
Alex Wu21858432012-04-01 20:13:08 +08002867is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2868{
2869 if (es->configure == black_surface_configure) {
2870 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002871 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002872 return true;
2873 }
2874 return false;
2875}
2876
Kristian Høgsberg75840622011-09-06 13:48:16 -04002877static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002878click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002879 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002880{
Daniel Stone37816df2012-05-16 18:45:18 +01002881 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002882 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002883 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002884 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002885
Daniel Stone37816df2012-05-16 18:45:18 +01002886 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002887 if (!focus)
2888 return;
2889
Alex Wu21858432012-04-01 20:13:08 +08002890 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002891 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002892
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002893 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2894 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002895
Daniel Stone325fc2d2012-05-30 16:31:58 +01002896 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002897 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002898}
2899
2900static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002901lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002902{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002903 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002904
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002905 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002906 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002907 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002908 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002909
2910 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002911
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002912 /* Hide all surfaces by removing the fullscreen, panel and
2913 * toplevel layers. This way nothing else can show or receive
2914 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002915
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002916 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002917 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002918 if (shell->showing_input_panels)
2919 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002920 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002921 wl_list_insert(&shell->compositor->cursor_layer.link,
2922 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002923
Pekka Paalanen77346a62011-11-30 16:26:35 +02002924 launch_screensaver(shell);
2925
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002926 /* TODO: disable bindings that should not work while locked. */
2927
2928 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002929}
2930
2931static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002932unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002933{
Pekka Paalanend81c2162011-11-16 13:47:34 +02002934 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002935 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002936 return;
2937 }
2938
2939 /* If desktop-shell client has gone away, unlock immediately. */
2940 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002941 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002942 return;
2943 }
2944
2945 if (shell->prepare_event_sent)
2946 return;
2947
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002948 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002949 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002950}
2951
2952static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002953shell_fade_done(struct weston_surface_animation *animation, void *data)
2954{
2955 struct desktop_shell *shell = data;
2956
2957 shell->fade.animation = NULL;
2958
2959 switch (shell->fade.type) {
2960 case FADE_IN:
2961 weston_surface_destroy(shell->fade.surface);
2962 shell->fade.surface = NULL;
2963 break;
2964 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002965 lock(shell);
2966 break;
2967 }
2968}
2969
2970static void
2971shell_fade(struct desktop_shell *shell, enum fade_type type)
2972{
2973 struct weston_compositor *compositor = shell->compositor;
2974 struct weston_surface *surface;
2975 float tint;
2976
2977 switch (type) {
2978 case FADE_IN:
2979 tint = 0.0;
2980 break;
2981 case FADE_OUT:
2982 tint = 1.0;
2983 break;
2984 default:
2985 weston_log("shell: invalid fade type\n");
2986 return;
2987 }
2988
2989 shell->fade.type = type;
2990
2991 if (shell->fade.surface == NULL) {
2992 surface = weston_surface_create(compositor);
2993 if (!surface)
2994 return;
2995
2996 weston_surface_configure(surface, 0, 0, 8192, 8192);
2997 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
2998 surface->alpha = 1.0 - tint;
2999 wl_list_insert(&compositor->fade_layer.surface_list,
3000 &surface->layer_link);
3001 weston_surface_update_transform(surface);
3002 shell->fade.surface = surface;
3003 pixman_region32_init(&surface->input);
3004 }
3005
3006 if (shell->fade.animation)
3007 weston_fade_update(shell->fade.animation,
3008 shell->fade.surface->alpha, tint, 30.0);
3009 else
3010 shell->fade.animation =
3011 weston_fade_run(shell->fade.surface,
3012 1.0 - tint, tint, 30.0,
3013 shell_fade_done, shell);
3014}
3015
3016static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003017idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003018{
3019 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003020 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003021
3022 shell_fade(shell, FADE_OUT);
3023 /* lock() is called from shell_fade_done() */
3024}
3025
3026static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003027wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003028{
3029 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003030 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003031
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003032 unlock(shell);
3033}
3034
3035static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003036show_input_panels(struct wl_listener *listener, void *data)
3037{
3038 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003039 container_of(listener, struct desktop_shell,
3040 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003041 struct input_panel_surface *surface, *next;
3042 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003043
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003044 shell->text_input.surface = (struct weston_surface*)data;
3045
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003046 if (shell->showing_input_panels)
3047 return;
3048
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003049 shell->showing_input_panels = true;
3050
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003051 if (!shell->locked)
3052 wl_list_insert(&shell->panel_layer.link,
3053 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003054
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003055 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003056 &shell->input_panel.surfaces, link) {
3057 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003058 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003059 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003060 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003061 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003062 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003063 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003064 weston_surface_damage(ws);
3065 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003066 }
3067}
3068
3069static void
3070hide_input_panels(struct wl_listener *listener, void *data)
3071{
3072 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003073 container_of(listener, struct desktop_shell,
3074 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003075 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003076
Jan Arne Petersen61381972013-01-31 15:52:21 +01003077 if (!shell->showing_input_panels)
3078 return;
3079
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003080 shell->showing_input_panels = false;
3081
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003082 if (!shell->locked)
3083 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003084
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003085 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003086 &shell->input_panel_layer.surface_list, layer_link)
3087 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003088}
3089
3090static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003091update_input_panels(struct wl_listener *listener, void *data)
3092{
3093 struct desktop_shell *shell =
3094 container_of(listener, struct desktop_shell,
3095 update_input_panel_listener);
3096
3097 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3098}
3099
3100static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003101center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003102{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003103 int32_t width = weston_surface_buffer_width(surface);
3104 int32_t height = weston_surface_buffer_height(surface);
3105 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003106
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003107 x = output->x + (output->width - width) / 2;
3108 y = output->y + (output->height - height) / 2;
3109
3110 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003111}
3112
3113static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003114weston_surface_set_initial_position (struct weston_surface *surface,
3115 struct desktop_shell *shell)
3116{
3117 struct weston_compositor *compositor = shell->compositor;
3118 int ix = 0, iy = 0;
3119 int range_x, range_y;
3120 int dx, dy, x, y, panel_height;
3121 struct weston_output *output, *target_output = NULL;
3122 struct weston_seat *seat;
3123
3124 /* As a heuristic place the new window on the same output as the
3125 * pointer. Falling back to the output containing 0, 0.
3126 *
3127 * TODO: Do something clever for touch too?
3128 */
3129 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003130 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003131 ix = wl_fixed_to_int(seat->pointer->x);
3132 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003133 break;
3134 }
3135 }
3136
3137 wl_list_for_each(output, &compositor->output_list, link) {
3138 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3139 target_output = output;
3140 break;
3141 }
3142 }
3143
3144 if (!target_output) {
3145 weston_surface_set_position(surface, 10 + random() % 400,
3146 10 + random() % 400);
3147 return;
3148 }
3149
3150 /* Valid range within output where the surface will still be onscreen.
3151 * If this is negative it means that the surface is bigger than
3152 * output.
3153 */
3154 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003155 range_x = target_output->width - surface->geometry.width;
3156 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003157 surface->geometry.height;
3158
Rob Bradford4cb88c72012-08-13 15:18:44 +01003159 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003160 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003161 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003162 dx = 0;
3163
3164 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003165 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003166 else
3167 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003168
3169 x = target_output->x + dx;
3170 y = target_output->y + dy;
3171
3172 weston_surface_set_position (surface, x, y);
3173}
3174
3175static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003176map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003177 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003178{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003179 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003180 struct shell_surface *shsurf = get_shell_surface(surface);
3181 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003182 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003183 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003184 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003185 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003186
Pekka Paalanen60921e52012-01-25 15:55:43 +02003187 surface->geometry.width = width;
3188 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003189 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003190
3191 /* initial positioning, see also configure() */
3192 switch (surface_type) {
3193 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003194 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003195 break;
Alex Wu4539b082012-03-01 12:57:46 +08003196 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003197 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003198 shell_map_fullscreen(shsurf);
3199 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003200 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003201 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003202 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01003203 weston_surface_set_position(surface, shsurf->output->x,
3204 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08003205 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003206 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003207 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003208 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003209 case SHELL_SURFACE_NONE:
3210 weston_surface_set_position(surface,
3211 surface->geometry.x + sx,
3212 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003213 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003214 default:
3215 ;
3216 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003217
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003218 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003219 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003220 case SHELL_SURFACE_POPUP:
3221 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003222 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003223 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3224 break;
Alex Wu4539b082012-03-01 12:57:46 +08003225 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003226 case SHELL_SURFACE_NONE:
3227 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003228 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003229 ws = get_current_workspace(shell);
3230 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003231 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003232 }
3233
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003234 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003235 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003236 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3237 surface->output = shsurf->output;
3238 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003239
Juan Zhao7bb92f02011-12-15 11:31:51 -05003240 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05003241 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003242 if (shsurf->transient.flags ==
3243 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3244 break;
3245 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003246 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003247 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003248 if (!shell->locked) {
3249 wl_list_for_each(seat, &compositor->seat_list, link)
3250 activate(shell, surface, seat);
3251 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003252 break;
3253 default:
3254 break;
3255 }
3256
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003257 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003258 {
3259 switch (shell->win_animation_type) {
3260 case ANIMATION_FADE:
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +02003261 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003262 break;
3263 case ANIMATION_ZOOM:
3264 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3265 break;
3266 default:
3267 break;
3268 }
3269 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003270}
3271
3272static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003273configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003274 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003275{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003276 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3277 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003278
Pekka Paalanen77346a62011-11-30 16:26:35 +02003279 shsurf = get_shell_surface(surface);
3280 if (shsurf)
3281 surface_type = shsurf->type;
3282
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003283 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003284
3285 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003286 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003287 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003288 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003289 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003290 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003291 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003292 surface->geometry.x = surface->output->x;
3293 surface->geometry.y = surface->output->y +
3294 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003295 break;
Alex Wu4539b082012-03-01 12:57:46 +08003296 case SHELL_SURFACE_TOPLEVEL:
3297 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003298 default:
3299 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003300 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003301
Alex Wu4539b082012-03-01 12:57:46 +08003302 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003303 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003304 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003305
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003306 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003307 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003308 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003309}
3310
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003311static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003312shell_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 +03003313{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003314 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003315 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003316
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003317 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003318
Giulio Camuffo5085a752013-03-25 21:42:45 +01003319 if (!weston_surface_is_mapped(es) && !wl_list_empty(&shsurf->popup.grab_link)) {
3320 remove_popup_grab(shsurf);
3321 }
3322
Giulio Camuffo184df502013-02-21 11:29:21 +01003323 if (width == 0)
3324 return;
3325
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003326 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003327 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003328 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003329 type_changed = 1;
3330 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003331
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003332 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003333 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003334 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003335 es->geometry.width != width ||
3336 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003337 float from_x, from_y;
3338 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003339
3340 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3341 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3342 configure(shell, es,
3343 es->geometry.x + to_x - from_x,
3344 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003345 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003346 }
3347}
3348
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003349static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003350
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003351static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003352desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003353{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003354 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003355 struct desktop_shell *shell =
3356 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003357
3358 shell->child.process.pid = 0;
3359 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003360
3361 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3362 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003363 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003364 shell->child.deathstamp = time;
3365 shell->child.deathcount = 0;
3366 }
3367
3368 shell->child.deathcount++;
3369 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003370 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003371 return;
3372 }
3373
Martin Minarik6d118362012-06-07 18:01:59 +02003374 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003375 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003376}
3377
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003378static void
3379launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003380{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003381 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003382 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003383
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003384 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003385 &shell->child.process,
3386 shell_exe,
3387 desktop_shell_sigchld);
3388
3389 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003390 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003391}
3392
3393static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003394bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3395{
Tiago Vignattibe143262012-04-16 17:31:41 +03003396 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003397
3398 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003399 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003400}
3401
Kristian Høgsberg75840622011-09-06 13:48:16 -04003402static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003403unbind_desktop_shell(struct wl_resource *resource)
3404{
Tiago Vignattibe143262012-04-16 17:31:41 +03003405 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003406
3407 if (shell->locked)
3408 resume_desktop(shell);
3409
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003410 shell->child.desktop_shell = NULL;
3411 shell->prepare_event_sent = false;
3412 free(resource);
3413}
3414
3415static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003416bind_desktop_shell(struct wl_client *client,
3417 void *data, uint32_t version, uint32_t id)
3418{
Tiago Vignattibe143262012-04-16 17:31:41 +03003419 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003420 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003421
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003422 resource = wl_client_add_object(client, &desktop_shell_interface,
3423 &desktop_shell_implementation,
3424 id, shell);
3425
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003426 if (client == shell->child.client) {
3427 resource->destroy = unbind_desktop_shell;
3428 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003429 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003430 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003431
3432 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3433 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003434 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003435}
3436
Pekka Paalanen6e168112011-11-24 11:34:05 +02003437static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003438screensaver_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 -04003439{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003440 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003441
Giulio Camuffo184df502013-02-21 11:29:21 +01003442 if (width == 0)
3443 return;
3444
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003445 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003446 if (!shell->locked)
3447 return;
3448
3449 center_on_output(surface, surface->output);
3450
3451 if (wl_list_empty(&surface->layer_link)) {
3452 wl_list_insert(shell->lock_layer.surface_list.prev,
3453 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003454 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003455 wl_event_source_timer_update(shell->screensaver.timer,
3456 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003457 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003458 }
3459}
3460
3461static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003462screensaver_set_surface(struct wl_client *client,
3463 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003464 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003465 struct wl_resource *output_resource)
3466{
Tiago Vignattibe143262012-04-16 17:31:41 +03003467 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003468 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003469 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003470
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003471 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003472 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003473 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003474}
3475
3476static const struct screensaver_interface screensaver_implementation = {
3477 screensaver_set_surface
3478};
3479
3480static void
3481unbind_screensaver(struct wl_resource *resource)
3482{
Tiago Vignattibe143262012-04-16 17:31:41 +03003483 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003484
Pekka Paalanen77346a62011-11-30 16:26:35 +02003485 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003486 free(resource);
3487}
3488
3489static void
3490bind_screensaver(struct wl_client *client,
3491 void *data, uint32_t version, uint32_t id)
3492{
Tiago Vignattibe143262012-04-16 17:31:41 +03003493 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003494 struct wl_resource *resource;
3495
3496 resource = wl_client_add_object(client, &screensaver_interface,
3497 &screensaver_implementation,
3498 id, shell);
3499
Pekka Paalanen77346a62011-11-30 16:26:35 +02003500 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003501 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003502 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003503 return;
3504 }
3505
3506 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3507 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003508 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003509}
3510
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003511static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003512input_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 -04003513{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003514 struct input_panel_surface *ip_surface = surface->configure_private;
3515 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003516 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003517 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003518 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003519
Giulio Camuffo184df502013-02-21 11:29:21 +01003520 if (width == 0)
3521 return;
3522
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003523 if (!weston_surface_is_mapped(surface)) {
3524 if (!shell->showing_input_panels)
3525 return;
3526
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003527 show_surface = 1;
3528 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003529
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003530 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003531
3532 if (ip_surface->panel) {
3533 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3534 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3535 } else {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003536 mode = ip_surface->output->current;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003537
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003538 x = ip_surface->output->x + (mode->width - width) / 2;
3539 y = ip_surface->output->y + mode->height - height;
3540 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003541
3542 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003543 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003544 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003545
3546 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003547 wl_list_insert(&shell->input_panel_layer.surface_list,
3548 &surface->layer_link);
3549 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003550 weston_surface_damage(surface);
3551 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3552 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003553}
3554
3555static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003556destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003557{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003558 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003559 wl_list_remove(&input_panel_surface->link);
3560
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003561 input_panel_surface->surface->configure = NULL;
3562
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003563 free(input_panel_surface);
3564}
3565
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003566static struct input_panel_surface *
3567get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003568{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003569 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003570 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003571 } else {
3572 return NULL;
3573 }
3574}
3575
3576static void
3577input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3578{
3579 struct input_panel_surface *ipsurface = container_of(listener,
3580 struct input_panel_surface,
3581 surface_destroy_listener);
3582
3583 if (ipsurface->resource.client) {
3584 wl_resource_destroy(&ipsurface->resource);
3585 } else {
3586 wl_signal_emit(&ipsurface->resource.destroy_signal,
3587 &ipsurface->resource);
3588 destroy_input_panel_surface(ipsurface);
3589 }
3590}
3591static struct input_panel_surface *
3592create_input_panel_surface(struct desktop_shell *shell,
3593 struct weston_surface *surface)
3594{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003595 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003596
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003597 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3598 if (!input_panel_surface)
3599 return NULL;
3600
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003601 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003602 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003603
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003604 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003605
3606 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003607
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003608 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3609 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
3610 wl_signal_add(&surface->surface.resource.destroy_signal,
3611 &input_panel_surface->surface_destroy_listener);
3612
3613 wl_list_init(&input_panel_surface->link);
3614
3615 return input_panel_surface;
3616}
3617
3618static void
3619input_panel_surface_set_toplevel(struct wl_client *client,
3620 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003621 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003622 uint32_t position)
3623{
3624 struct input_panel_surface *input_panel_surface = resource->data;
3625 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003626
3627 wl_list_insert(&shell->input_panel.surfaces,
3628 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003629
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003630 input_panel_surface->output = output_resource->data;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003631 input_panel_surface->panel = 0;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003632
3633 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__,
3634 input_panel_surface->panel,
3635 input_panel_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003636}
3637
3638static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003639input_panel_surface_set_overlay_panel(struct wl_client *client,
3640 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003641{
3642 struct input_panel_surface *input_panel_surface = resource->data;
3643 struct desktop_shell *shell = input_panel_surface->shell;
3644
3645 wl_list_insert(&shell->input_panel.surfaces,
3646 &input_panel_surface->link);
3647
3648 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003649}
3650
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003651static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003652 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003653 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003654};
3655
3656static void
3657destroy_input_panel_surface_resource(struct wl_resource *resource)
3658{
3659 struct input_panel_surface *ipsurf = resource->data;
3660
3661 destroy_input_panel_surface(ipsurf);
3662}
3663
3664static void
3665input_panel_get_input_panel_surface(struct wl_client *client,
3666 struct wl_resource *resource,
3667 uint32_t id,
3668 struct wl_resource *surface_resource)
3669{
3670 struct weston_surface *surface = surface_resource->data;
3671 struct desktop_shell *shell = resource->data;
3672 struct input_panel_surface *ipsurf;
3673
3674 if (get_input_panel_surface(surface)) {
3675 wl_resource_post_error(surface_resource,
3676 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003677 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003678 return;
3679 }
3680
3681 ipsurf = create_input_panel_surface(shell, surface);
3682 if (!ipsurf) {
3683 wl_resource_post_error(surface_resource,
3684 WL_DISPLAY_ERROR_INVALID_OBJECT,
3685 "surface->configure already set");
3686 return;
3687 }
3688
3689 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3690 ipsurf->resource.object.id = id;
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003691 ipsurf->resource.object.interface = &wl_input_panel_surface_interface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003692 ipsurf->resource.object.implementation =
3693 (void (**)(void)) &input_panel_surface_implementation;
3694 ipsurf->resource.data = ipsurf;
3695
3696 wl_client_add_resource(client, &ipsurf->resource);
3697}
3698
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003699static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003700 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003701};
3702
3703static void
3704unbind_input_panel(struct wl_resource *resource)
3705{
3706 struct desktop_shell *shell = resource->data;
3707
3708 shell->input_panel.binding = NULL;
3709 free(resource);
3710}
3711
3712static void
3713bind_input_panel(struct wl_client *client,
3714 void *data, uint32_t version, uint32_t id)
3715{
3716 struct desktop_shell *shell = data;
3717 struct wl_resource *resource;
3718
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003719 resource = wl_client_add_object(client, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003720 &input_panel_implementation,
3721 id, shell);
3722
3723 if (shell->input_panel.binding == NULL) {
3724 resource->destroy = unbind_input_panel;
3725 shell->input_panel.binding = resource;
3726 return;
3727 }
3728
3729 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3730 "interface object already bound");
3731 wl_resource_destroy(resource);
3732}
3733
Kristian Høgsberg07045392012-02-19 18:52:44 -05003734struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003735 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003736 struct weston_surface *current;
3737 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003738 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003739};
3740
3741static void
3742switcher_next(struct switcher *switcher)
3743{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003744 struct weston_surface *surface;
3745 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003746 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003747 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003748
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003749 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003750 switch (get_shell_surface_type(surface)) {
3751 case SHELL_SURFACE_TOPLEVEL:
3752 case SHELL_SURFACE_FULLSCREEN:
3753 case SHELL_SURFACE_MAXIMIZED:
3754 if (first == NULL)
3755 first = surface;
3756 if (prev == switcher->current)
3757 next = surface;
3758 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003759 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003760 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003761 weston_surface_damage(surface);
3762 break;
3763 default:
3764 break;
3765 }
Alex Wu1659daa2012-04-01 20:13:09 +08003766
3767 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003768 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003769 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003770 weston_surface_damage(surface);
3771 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003772 }
3773
3774 if (next == NULL)
3775 next = first;
3776
Alex Wu07b26062012-03-12 16:06:01 +08003777 if (next == NULL)
3778 return;
3779
Kristian Høgsberg07045392012-02-19 18:52:44 -05003780 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003781 wl_signal_add(&next->surface.resource.destroy_signal,
3782 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003783
3784 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003785 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003786
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003787 shsurf = get_shell_surface(switcher->current);
3788 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003789 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003790}
3791
3792static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003793switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003794{
3795 struct switcher *switcher =
3796 container_of(listener, struct switcher, listener);
3797
3798 switcher_next(switcher);
3799}
3800
3801static void
Daniel Stone351eb612012-05-31 15:27:47 -04003802switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003803{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003804 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003805 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003806 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003807
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003808 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003809 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003810 weston_surface_damage(surface);
3811 }
3812
Alex Wu07b26062012-03-12 16:06:01 +08003813 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003814 activate(switcher->shell, switcher->current,
3815 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003816 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003817 weston_keyboard_end_grab(keyboard);
3818 if (keyboard->input_method_resource)
3819 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003820 free(switcher);
3821}
3822
3823static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003824switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003825 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003826{
3827 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003828 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003829
Daniel Stonec9785ea2012-05-30 16:31:52 +01003830 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003831 switcher_next(switcher);
3832}
3833
3834static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003835switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04003836 uint32_t mods_depressed, uint32_t mods_latched,
3837 uint32_t mods_locked, uint32_t group)
3838{
3839 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003840 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003841
Daniel Stone351eb612012-05-31 15:27:47 -04003842 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3843 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003844}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003845
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003846static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003847 switcher_key,
3848 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003849};
3850
3851static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003852switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003853 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003854{
Tiago Vignattibe143262012-04-16 17:31:41 +03003855 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003856 struct switcher *switcher;
3857
3858 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003859 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003860 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003861 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003862 wl_list_init(&switcher->listener.link);
3863
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003864 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003865 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003866 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
3867 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003868 switcher_next(switcher);
3869}
3870
Pekka Paalanen3c647232011-12-22 13:43:43 +02003871static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003872backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003873 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003874{
3875 struct weston_compositor *compositor = data;
3876 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003877 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003878
3879 /* TODO: we're limiting to simple use cases, where we assume just
3880 * control on the primary display. We'd have to extend later if we
3881 * ever get support for setting backlights on random desktop LCD
3882 * panels though */
3883 output = get_default_output(compositor);
3884 if (!output)
3885 return;
3886
3887 if (!output->set_backlight)
3888 return;
3889
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003890 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3891 backlight_new = output->backlight_current - 25;
3892 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3893 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003894
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003895 if (backlight_new < 5)
3896 backlight_new = 5;
3897 if (backlight_new > 255)
3898 backlight_new = 255;
3899
3900 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003901 output->set_backlight(output, output->backlight_current);
3902}
3903
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003904struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003905 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003906 struct weston_seat *seat;
3907 uint32_t key[2];
3908 int key_released[2];
3909};
3910
3911static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003912debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003913 uint32_t key, uint32_t state)
3914{
3915 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3916 struct wl_resource *resource;
3917 struct wl_display *display;
3918 uint32_t serial;
3919 int send = 0, terminate = 0;
3920 int check_binding = 1;
3921 int i;
3922
3923 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3924 /* Do not run bindings on key releases */
3925 check_binding = 0;
3926
3927 for (i = 0; i < 2; i++)
3928 if (key == db->key[i])
3929 db->key_released[i] = 1;
3930
3931 if (db->key_released[0] && db->key_released[1]) {
3932 /* All key releases been swalled so end the grab */
3933 terminate = 1;
3934 } else if (key != db->key[0] && key != db->key[1]) {
3935 /* Should not swallow release of other keys */
3936 send = 1;
3937 }
3938 } else if (key == db->key[0] && !db->key_released[0]) {
3939 /* Do not check bindings for the first press of the binding
3940 * key. This allows it to be used as a debug shortcut.
3941 * We still need to swallow this event. */
3942 check_binding = 0;
3943 } else if (db->key[1]) {
3944 /* If we already ran a binding don't process another one since
3945 * we can't keep track of all the binding keys that were
3946 * pressed in order to swallow the release events. */
3947 send = 1;
3948 check_binding = 0;
3949 }
3950
3951 if (check_binding) {
3952 struct weston_compositor *ec = db->seat->compositor;
3953
3954 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3955 key, state)) {
3956 /* We ran a binding so swallow the press and keep the
3957 * grab to swallow the released too. */
3958 send = 0;
3959 terminate = 0;
3960 db->key[1] = key;
3961 } else {
3962 /* Terminate the grab since the key pressed is not a
3963 * debug binding key. */
3964 send = 1;
3965 terminate = 1;
3966 }
3967 }
3968
3969 if (send) {
3970 resource = grab->keyboard->focus_resource;
3971
3972 if (resource) {
3973 display = wl_client_get_display(resource->client);
3974 serial = wl_display_next_serial(display);
3975 wl_keyboard_send_key(resource, serial, time, key, state);
3976 }
3977 }
3978
3979 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003980 weston_keyboard_end_grab(grab->keyboard);
3981 if (grab->keyboard->input_method_resource)
3982 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003983 free(db);
3984 }
3985}
3986
3987static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003988debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003989 uint32_t mods_depressed, uint32_t mods_latched,
3990 uint32_t mods_locked, uint32_t group)
3991{
3992 struct wl_resource *resource;
3993
3994 resource = grab->keyboard->focus_resource;
3995 if (!resource)
3996 return;
3997
3998 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3999 mods_latched, mods_locked, group);
4000}
4001
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004002struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004003 debug_binding_key,
4004 debug_binding_modifiers
4005};
4006
4007static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004008debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004009{
4010 struct debug_binding_grab *grab;
4011
4012 grab = calloc(1, sizeof *grab);
4013 if (!grab)
4014 return;
4015
4016 grab->seat = (struct weston_seat *) seat;
4017 grab->key[0] = key;
4018 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004019 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004020}
4021
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004022static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004023force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004024 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004025{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004026 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004027 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004028 struct desktop_shell *shell = data;
4029 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004030 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004031
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004032 focus_surface = seat->keyboard->focus;
4033 if (!focus_surface)
4034 return;
4035
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004036 wl_signal_emit(&compositor->kill_signal, focus_surface);
4037
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004038 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03004039 wl_client_get_credentials(client, &pid, NULL, NULL);
4040
4041 /* Skip clients that we launched ourselves (the credentials of
4042 * the socketpair is ours) */
4043 if (pid == getpid())
4044 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004045
Daniel Stone325fc2d2012-05-30 16:31:58 +01004046 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004047}
4048
4049static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004050workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004051 uint32_t key, void *data)
4052{
4053 struct desktop_shell *shell = data;
4054 unsigned int new_index = shell->workspaces.current;
4055
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004056 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004057 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004058 if (new_index != 0)
4059 new_index--;
4060
4061 change_workspace(shell, new_index);
4062}
4063
4064static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004065workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004066 uint32_t key, void *data)
4067{
4068 struct desktop_shell *shell = data;
4069 unsigned int new_index = shell->workspaces.current;
4070
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004071 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004072 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004073 if (new_index < shell->workspaces.num - 1)
4074 new_index++;
4075
4076 change_workspace(shell, new_index);
4077}
4078
4079static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004080workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004081 uint32_t key, void *data)
4082{
4083 struct desktop_shell *shell = data;
4084 unsigned int new_index;
4085
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004086 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004087 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004088 new_index = key - KEY_F1;
4089 if (new_index >= shell->workspaces.num)
4090 new_index = shell->workspaces.num - 1;
4091
4092 change_workspace(shell, new_index);
4093}
4094
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004095static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004096workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004097 uint32_t key, void *data)
4098{
4099 struct desktop_shell *shell = data;
4100 unsigned int new_index = shell->workspaces.current;
4101
4102 if (shell->locked)
4103 return;
4104
4105 if (new_index != 0)
4106 new_index--;
4107
4108 take_surface_to_workspace_by_seat(shell, seat, new_index);
4109}
4110
4111static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004112workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004113 uint32_t key, void *data)
4114{
4115 struct desktop_shell *shell = data;
4116 unsigned int new_index = shell->workspaces.current;
4117
4118 if (shell->locked)
4119 return;
4120
4121 if (new_index < shell->workspaces.num - 1)
4122 new_index++;
4123
4124 take_surface_to_workspace_by_seat(shell, seat, new_index);
4125}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004126
4127static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004128shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004129{
Tiago Vignattibe143262012-04-16 17:31:41 +03004130 struct desktop_shell *shell =
4131 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004132 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004133
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004134 if (shell->child.client)
4135 wl_client_destroy(shell->child.client);
4136
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004137 wl_list_remove(&shell->idle_listener.link);
4138 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004139 wl_list_remove(&shell->show_input_panel_listener.link);
4140 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004141
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004142 wl_array_for_each(ws, &shell->workspaces.array)
4143 workspace_destroy(*ws);
4144 wl_array_release(&shell->workspaces.array);
4145
Pekka Paalanen3c647232011-12-22 13:43:43 +02004146 free(shell->screensaver.path);
4147 free(shell);
4148}
4149
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004150static void
4151shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4152{
4153 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004154 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004155
4156 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004157 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4158 MODIFIER_CTRL | MODIFIER_ALT,
4159 terminate_binding, ec);
4160 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4161 click_to_activate_binding,
4162 shell);
4163 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4164 MODIFIER_SUPER | MODIFIER_ALT,
4165 surface_opacity_binding, NULL);
4166 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4167 MODIFIER_SUPER, zoom_axis_binding,
4168 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004169
4170 /* configurable bindings */
4171 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004172 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4173 zoom_key_binding, NULL);
4174 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4175 zoom_key_binding, NULL);
4176 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4177 shell);
4178 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4179 resize_binding, shell);
4180 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4181 rotate_binding, NULL);
4182 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4183 shell);
4184 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4185 ec);
4186 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4187 backlight_binding, ec);
4188 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4189 ec);
4190 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4191 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004192 weston_compositor_add_key_binding(ec, KEY_K, mod,
4193 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004194 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4195 workspace_up_binding, shell);
4196 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4197 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004198 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4199 workspace_move_surface_up_binding,
4200 shell);
4201 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4202 workspace_move_surface_down_binding,
4203 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004204
4205 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4206 if (shell->workspaces.num > 1) {
4207 num_workspace_bindings = shell->workspaces.num;
4208 if (num_workspace_bindings > 6)
4209 num_workspace_bindings = 6;
4210 for (i = 0; i < num_workspace_bindings; i++)
4211 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4212 workspace_f_binding,
4213 shell);
4214 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004215
4216 /* Debug bindings */
4217 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4218 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004219}
4220
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004221WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004222module_init(struct weston_compositor *ec,
4223 int *argc, char *argv[], const char *config_file)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004224{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004225 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004226 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004227 struct workspace **pws;
4228 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004229 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004230
4231 shell = malloc(sizeof *shell);
4232 if (shell == NULL)
4233 return -1;
4234
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004235 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004236 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004237
4238 shell->destroy_listener.notify = shell_destroy;
4239 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004240 shell->idle_listener.notify = idle_handler;
4241 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4242 shell->wake_listener.notify = wake_handler;
4243 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004244 shell->show_input_panel_listener.notify = show_input_panels;
4245 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4246 shell->hide_input_panel_listener.notify = hide_input_panels;
4247 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004248 shell->update_input_panel_listener.notify = update_input_panels;
4249 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004250 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004251 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004252 ec->shell_interface.create_shell_surface = create_shell_surface;
4253 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004254 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004255 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004256 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004257 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004258
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004259 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004260
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004261 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4262 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004263 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4264 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004265 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004266
4267 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004268 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004269
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004270 shell_configuration(shell, config_file);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004271
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004272 for (i = 0; i < shell->workspaces.num; i++) {
4273 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4274 if (pws == NULL)
4275 return -1;
4276
4277 *pws = workspace_create();
4278 if (*pws == NULL)
4279 return -1;
4280 }
4281 activate_workspace(shell, 0);
4282
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004283 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004284 wl_list_init(&shell->workspaces.animation.link);
4285 shell->workspaces.animation.frame = animate_workspace_change_frame;
4286
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004287 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4288 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004289 return -1;
4290
Kristian Høgsberg75840622011-09-06 13:48:16 -04004291 if (wl_display_add_global(ec->wl_display,
4292 &desktop_shell_interface,
4293 shell, bind_desktop_shell) == NULL)
4294 return -1;
4295
Pekka Paalanen6e168112011-11-24 11:34:05 +02004296 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4297 shell, bind_screensaver) == NULL)
4298 return -1;
4299
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004300 if (wl_display_add_global(ec->wl_display, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004301 shell, bind_input_panel) == NULL)
4302 return -1;
4303
Jonas Ådahle9d22502012-08-29 22:13:01 +02004304 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4305 shell, bind_workspace_manager) == NULL)
4306 return -1;
4307
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004308 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004309
4310 loop = wl_display_get_event_loop(ec->wl_display);
4311 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004312
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004313 shell->screensaver.timer =
4314 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4315
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004316 wl_list_for_each(seat, &ec->seat_list, link)
4317 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004318
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004319 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004320
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004321 shell_fade(shell, FADE_IN);
4322
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004323 return 0;
4324}