blob: 3f4833d681b8ba2bf7f948f97188304686999b15 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040033#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050034
Pekka Paalanen50719bc2011-11-22 14:18:50 +020035#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040037#include "desktop-shell-server-protocol.h"
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010038#include "input-method-server-protocol.h"
Jonas Ådahle9d22502012-08-29 22:13:01 +020039#include "workspaces-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020040#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050041
Jonas Ådahle3cddce2012-06-13 00:01:22 +020042#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020043#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020044
Juan Zhaoe10d2792012-04-25 19:09:52 +080045enum animation_type {
46 ANIMATION_NONE,
47
48 ANIMATION_ZOOM,
49 ANIMATION_FADE
50};
51
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +020052enum fade_type {
53 FADE_IN,
54 FADE_OUT
55};
56
Jonas Ådahl04769742012-06-13 00:01:24 +020057struct focus_state {
58 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040059 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020060 struct weston_surface *keyboard_focus;
61 struct wl_list link;
62 struct wl_listener seat_destroy_listener;
63 struct wl_listener surface_destroy_listener;
64};
65
Jonas Ådahle3cddce2012-06-13 00:01:22 +020066struct workspace {
67 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020068
69 struct wl_list focus_list;
70 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020071};
72
Philipp Brüschweiler88013572012-08-06 13:44:42 +020073struct input_panel_surface {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010074 struct wl_resource resource;
75
76 struct desktop_shell *shell;
77
Philipp Brüschweiler88013572012-08-06 13:44:42 +020078 struct wl_list link;
79 struct weston_surface *surface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +010080 struct wl_listener surface_destroy_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020081
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +020082 struct weston_output *output;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020083 uint32_t panel;
Philipp Brüschweiler88013572012-08-06 13:44:42 +020084};
85
Tiago Vignattibe143262012-04-16 17:31:41 +030086struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050087 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040088
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +020089 struct wl_listener idle_listener;
90 struct wl_listener wake_listener;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040091 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020092 struct wl_listener show_input_panel_listener;
93 struct wl_listener hide_input_panel_listener;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +020094 struct wl_listener update_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020095
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050096 struct weston_layer fullscreen_layer;
97 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050098 struct weston_layer background_layer;
99 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200100 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500101
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400102 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300103 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400104
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200105 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500106 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200107 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200108 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +0200109
110 unsigned deathcount;
111 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200112 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200113
114 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200115 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200116 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200117
Jan Arne Petersen14da96b2013-04-18 16:47:28 +0200118 struct {
119 struct weston_surface *surface;
120 pixman_box32_t cursor_rectangle;
121 } text_input;
122
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400123 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500124 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100125
Pekka Paalanen77346a62011-11-30 16:26:35 +0200126 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200127 struct wl_array array;
128 unsigned int current;
129 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200130
Jonas Ådahle9d22502012-08-29 22:13:01 +0200131 struct wl_list client_list;
132
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200133 struct weston_animation animation;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200134 struct wl_list anim_sticky_list;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200135 int anim_dir;
136 uint32_t anim_timestamp;
137 double anim_current;
138 struct workspace *anim_from;
139 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200140 } workspaces;
141
142 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200143 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200144 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200145 struct wl_resource *binding;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500146 struct weston_process process;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200147 struct wl_event_source *timer;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200148 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500149
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200150 struct {
151 struct wl_resource *binding;
152 struct wl_list surfaces;
153 } input_panel;
154
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +0200155 struct {
156 struct weston_surface *surface;
157 struct weston_surface_animation *animation;
158 enum fade_type type;
159 } fade;
160
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300161 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800162 enum animation_type win_animation_type;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400163};
164
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500165enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200166 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500167 SHELL_SURFACE_TOPLEVEL,
168 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500169 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800170 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500171 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200172};
173
Scott Moreauff1db4a2012-04-17 19:06:18 -0600174struct ping_timer {
175 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600176 uint32_t serial;
177};
178
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200179struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200180 struct wl_resource resource;
181
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500182 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200183 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400184 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300185 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200186
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400187 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400188 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500189 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800190 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800191 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600192 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100193
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500194 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200195 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500196 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200197 } rotation;
198
199 struct {
Giulio Camuffo5085a752013-03-25 21:42:45 +0100200 struct wl_list grab_link;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500201 int32_t x, y;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100202 struct shell_seat *shseat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400203 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500204 } popup;
205
Alex Wu4539b082012-03-01 12:57:46 +0800206 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300207 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400208 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300209 } transient;
210
211 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800212 enum wl_shell_surface_fullscreen_method type;
213 struct weston_transform transform; /* matrix from x, y */
214 uint32_t framerate;
215 struct weston_surface *black_surface;
216 } fullscreen;
217
Scott Moreauff1db4a2012-04-17 19:06:18 -0600218 struct ping_timer *ping_timer;
219
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200220 struct weston_transform workspace_transform;
221
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500222 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500223 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100224 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400225
226 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200227};
228
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300229struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400230 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300231 struct shell_surface *shsurf;
232 struct wl_listener shsurf_destroy_listener;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400233 struct weston_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300234};
235
236struct weston_move_grab {
237 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100238 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500239};
240
Pekka Paalanen460099f2012-01-20 16:48:25 +0200241struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300242 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500243 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200244 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200245 float x;
246 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200247 } center;
248};
249
Giulio Camuffo5085a752013-03-25 21:42:45 +0100250struct shell_seat {
251 struct weston_seat *seat;
252 struct wl_listener seat_destroy_listener;
253
254 struct {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400255 struct weston_pointer_grab grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100256 struct wl_list surfaces_list;
257 struct wl_client *client;
258 int32_t initial_up;
259 } popup_grab;
260};
261
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400262static void
263activate(struct desktop_shell *shell, struct weston_surface *es,
264 struct weston_seat *seat);
265
266static struct workspace *
267get_current_workspace(struct desktop_shell *shell);
268
Alex Wubd3354b2012-04-17 17:20:49 +0800269static struct shell_surface *
270get_shell_surface(struct weston_surface *surface);
271
272static struct desktop_shell *
273shell_surface_get_shell(struct shell_surface *shsurf);
274
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500275static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400276surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500277
Alex Wubd3354b2012-04-17 17:20:49 +0800278static bool
279shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
280{
281 struct desktop_shell *shell;
282 struct weston_surface *top_fs_es;
283
284 shell = shell_surface_get_shell(shsurf);
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100285
Alex Wubd3354b2012-04-17 17:20:49 +0800286 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
287 return false;
288
289 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
Quentin Glidicc0d79ce2013-01-29 14:16:13 +0100290 struct weston_surface,
Alex Wubd3354b2012-04-17 17:20:49 +0800291 layer_link);
292 return (shsurf == get_shell_surface(top_fs_es));
293}
294
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500295static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400296destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300297{
298 struct shell_grab *grab;
299
300 grab = container_of(listener, struct shell_grab,
301 shsurf_destroy_listener);
302
303 grab->shsurf = NULL;
304}
305
306static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400307popup_grab_end(struct weston_pointer *pointer);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400308
309static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300310shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400311 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300312 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400313 struct weston_pointer *pointer,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300314 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300315{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300316 struct desktop_shell *shell = shsurf->shell;
317
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400318 popup_grab_end(pointer);
319
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300320 grab->grab.interface = interface;
321 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400322 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
323 wl_signal_add(&shsurf->resource.destroy_signal,
324 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300325
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300326 grab->pointer = pointer;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300327
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400328 weston_pointer_start_grab(pointer, &grab->grab);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300329 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400330 weston_pointer_set_focus(pointer, shell->grab_surface,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400331 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300332}
333
334static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300335shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300336{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400337 if (grab->shsurf)
338 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300339
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400340 weston_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300341}
342
343static void
Alex Wu4539b082012-03-01 12:57:46 +0800344center_on_output(struct weston_surface *surface,
345 struct weston_output *output);
346
Daniel Stone496ca172012-05-30 16:31:42 +0100347static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300348get_modifier(char *modifier)
349{
350 if (!modifier)
351 return MODIFIER_SUPER;
352
353 if (!strcmp("ctrl", modifier))
354 return MODIFIER_CTRL;
355 else if (!strcmp("alt", modifier))
356 return MODIFIER_ALT;
357 else if (!strcmp("super", modifier))
358 return MODIFIER_SUPER;
359 else
360 return MODIFIER_SUPER;
361}
362
Juan Zhaoe10d2792012-04-25 19:09:52 +0800363static enum animation_type
364get_animation_type(char *animation)
365{
366 if (!animation)
367 return ANIMATION_NONE;
368
369 if (!strcmp("zoom", animation))
370 return ANIMATION_ZOOM;
371 else if (!strcmp("fade", animation))
372 return ANIMATION_FADE;
373 else
374 return ANIMATION_NONE;
375}
376
Alex Wu4539b082012-03-01 12:57:46 +0800377static void
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -0500378shell_configuration(struct desktop_shell *shell, const char *config_file)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200379{
Pekka Paalanen7296e792011-12-07 16:22:00 +0200380 char *path = NULL;
381 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200382 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300383 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800384 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200385
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400386 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300387 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800388 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200389 { "num-workspaces",
390 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200391 };
392
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400393 struct config_key saver_keys[] = {
394 { "path", CONFIG_KEY_STRING, &path },
395 { "duration", CONFIG_KEY_INTEGER, &duration },
396 };
397
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200398 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400399 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200400 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
401 };
402
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500403 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200404
Pekka Paalanen7296e792011-12-07 16:22:00 +0200405 shell->screensaver.path = path;
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +0200406 shell->screensaver.duration = duration * 1000;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300407 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800408 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200409 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
410}
411
412static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200413focus_state_destroy(struct focus_state *state)
414{
415 wl_list_remove(&state->seat_destroy_listener.link);
416 wl_list_remove(&state->surface_destroy_listener.link);
417 free(state);
418}
419
420static void
421focus_state_seat_destroy(struct wl_listener *listener, void *data)
422{
423 struct focus_state *state = container_of(listener,
424 struct focus_state,
425 seat_destroy_listener);
426
427 wl_list_remove(&state->link);
428 focus_state_destroy(state);
429}
430
431static void
432focus_state_surface_destroy(struct wl_listener *listener, void *data)
433{
434 struct focus_state *state = container_of(listener,
435 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400436 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400437 struct desktop_shell *shell;
438 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200439
Kristian Høgsberge3778222012-07-31 17:29:30 -0400440 next = NULL;
441 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
442 if (surface == state->keyboard_focus)
443 continue;
444
445 next = surface;
446 break;
447 }
448
449 if (next) {
450 shell = state->seat->compositor->shell_interface.shell;
451 activate(shell, next, state->seat);
452 } else {
453 wl_list_remove(&state->link);
454 focus_state_destroy(state);
455 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200456}
457
458static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400459focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200460{
Jonas Ådahl04769742012-06-13 00:01:24 +0200461 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200462
463 state = malloc(sizeof *state);
464 if (state == NULL)
465 return NULL;
466
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400467 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200468 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400469 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200470
471 state->seat_destroy_listener.notify = focus_state_seat_destroy;
472 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400473 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200474 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400475 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200476
477 return state;
478}
479
Jonas Ådahl8538b222012-08-29 22:13:03 +0200480static struct focus_state *
481ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
482{
483 struct workspace *ws = get_current_workspace(shell);
484 struct focus_state *state;
485
486 wl_list_for_each(state, &ws->focus_list, link)
487 if (state->seat == seat)
488 break;
489
490 if (&state->link == &ws->focus_list)
491 state = focus_state_create(seat, ws);
492
493 return state;
494}
495
Jonas Ådahl04769742012-06-13 00:01:24 +0200496static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400497restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200498{
499 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400500 struct weston_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200501
502 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400503 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200504
Kristian Høgsberge3148752013-05-06 23:19:49 -0400505 weston_keyboard_set_focus(state->seat->keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200506 }
507}
508
509static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200510replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
511 struct weston_seat *seat)
512{
513 struct focus_state *state;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400514 struct weston_surface *surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200515
516 wl_list_for_each(state, &ws->focus_list, link) {
517 if (state->seat == seat) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400518 surface = seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200519 state->keyboard_focus =
520 (struct weston_surface *) surface;
521 return;
522 }
523 }
524}
525
526static void
527drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
528 struct weston_surface *surface)
529{
530 struct focus_state *state;
531
532 wl_list_for_each(state, &ws->focus_list, link)
533 if (state->keyboard_focus == surface)
534 state->keyboard_focus = NULL;
535}
536
537static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200538workspace_destroy(struct workspace *ws)
539{
Jonas Ådahl04769742012-06-13 00:01:24 +0200540 struct focus_state *state, *next;
541
542 wl_list_for_each_safe(state, next, &ws->focus_list, link)
543 focus_state_destroy(state);
544
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200545 free(ws);
546}
547
Jonas Ådahl04769742012-06-13 00:01:24 +0200548static void
549seat_destroyed(struct wl_listener *listener, void *data)
550{
551 struct weston_seat *seat = data;
552 struct focus_state *state, *next;
553 struct workspace *ws = container_of(listener,
554 struct workspace,
555 seat_destroyed_listener);
556
557 wl_list_for_each_safe(state, next, &ws->focus_list, link)
558 if (state->seat == seat)
559 wl_list_remove(&state->link);
560}
561
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200562static struct workspace *
563workspace_create(void)
564{
565 struct workspace *ws = malloc(sizeof *ws);
566 if (ws == NULL)
567 return NULL;
568
569 weston_layer_init(&ws->layer, NULL);
570
Jonas Ådahl04769742012-06-13 00:01:24 +0200571 wl_list_init(&ws->focus_list);
572 wl_list_init(&ws->seat_destroyed_listener.link);
573 ws->seat_destroyed_listener.notify = seat_destroyed;
574
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200575 return ws;
576}
577
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200578static int
579workspace_is_empty(struct workspace *ws)
580{
581 return wl_list_empty(&ws->layer.surface_list);
582}
583
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200584static struct workspace *
585get_workspace(struct desktop_shell *shell, unsigned int index)
586{
587 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200588 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200589 pws += index;
590 return *pws;
591}
592
593static struct workspace *
594get_current_workspace(struct desktop_shell *shell)
595{
596 return get_workspace(shell, shell->workspaces.current);
597}
598
599static void
600activate_workspace(struct desktop_shell *shell, unsigned int index)
601{
602 struct workspace *ws;
603
604 ws = get_workspace(shell, index);
605 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
606
607 shell->workspaces.current = index;
608}
609
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200610static unsigned int
611get_output_height(struct weston_output *output)
612{
613 return abs(output->region.extents.y1 - output->region.extents.y2);
614}
615
616static void
617surface_translate(struct weston_surface *surface, double d)
618{
619 struct shell_surface *shsurf = get_shell_surface(surface);
620 struct weston_transform *transform;
621
622 transform = &shsurf->workspace_transform;
623 if (wl_list_empty(&transform->link))
624 wl_list_insert(surface->geometry.transformation_list.prev,
625 &shsurf->workspace_transform.link);
626
627 weston_matrix_init(&shsurf->workspace_transform.matrix);
628 weston_matrix_translate(&shsurf->workspace_transform.matrix,
629 0.0, d, 0.0);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200630 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200631}
632
633static void
634workspace_translate_out(struct workspace *ws, double fraction)
635{
636 struct weston_surface *surface;
637 unsigned int height;
638 double d;
639
640 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
641 height = get_output_height(surface->output);
642 d = height * fraction;
643
644 surface_translate(surface, d);
645 }
646}
647
648static void
649workspace_translate_in(struct workspace *ws, double fraction)
650{
651 struct weston_surface *surface;
652 unsigned int height;
653 double d;
654
655 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
656 height = get_output_height(surface->output);
657
658 if (fraction > 0)
659 d = -(height - height * fraction);
660 else
661 d = height + height * fraction;
662
663 surface_translate(surface, d);
664 }
665}
666
667static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200668broadcast_current_workspace_state(struct desktop_shell *shell)
669{
670 struct wl_resource *resource;
671
672 wl_list_for_each(resource, &shell->workspaces.client_list, link)
673 workspace_manager_send_state(resource,
674 shell->workspaces.current,
675 shell->workspaces.num);
676}
677
678static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200679reverse_workspace_change_animation(struct desktop_shell *shell,
680 unsigned int index,
681 struct workspace *from,
682 struct workspace *to)
683{
684 shell->workspaces.current = index;
685
686 shell->workspaces.anim_to = to;
687 shell->workspaces.anim_from = from;
688 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
689 shell->workspaces.anim_timestamp = 0;
690
Scott Moreau4272e632012-08-13 09:58:41 -0600691 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200692}
693
694static void
695workspace_deactivate_transforms(struct workspace *ws)
696{
697 struct weston_surface *surface;
698 struct shell_surface *shsurf;
699
700 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
701 shsurf = get_shell_surface(surface);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200702 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
703 wl_list_remove(&shsurf->workspace_transform.link);
704 wl_list_init(&shsurf->workspace_transform.link);
705 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200706 weston_surface_geometry_dirty(surface);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200707 }
708}
709
710static void
711finish_workspace_change_animation(struct desktop_shell *shell,
712 struct workspace *from,
713 struct workspace *to)
714{
Scott Moreau4272e632012-08-13 09:58:41 -0600715 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200716
717 wl_list_remove(&shell->workspaces.animation.link);
718 workspace_deactivate_transforms(from);
719 workspace_deactivate_transforms(to);
720 shell->workspaces.anim_to = NULL;
721
722 wl_list_remove(&shell->workspaces.anim_from->layer.link);
723}
724
725static void
726animate_workspace_change_frame(struct weston_animation *animation,
727 struct weston_output *output, uint32_t msecs)
728{
729 struct desktop_shell *shell =
730 container_of(animation, struct desktop_shell,
731 workspaces.animation);
732 struct workspace *from = shell->workspaces.anim_from;
733 struct workspace *to = shell->workspaces.anim_to;
734 uint32_t t;
735 double x, y;
736
737 if (workspace_is_empty(from) && workspace_is_empty(to)) {
738 finish_workspace_change_animation(shell, from, to);
739 return;
740 }
741
742 if (shell->workspaces.anim_timestamp == 0) {
743 if (shell->workspaces.anim_current == 0.0)
744 shell->workspaces.anim_timestamp = msecs;
745 else
746 shell->workspaces.anim_timestamp =
747 msecs -
748 /* Invers of movement function 'y' below. */
749 (asin(1.0 - shell->workspaces.anim_current) *
750 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
751 M_2_PI);
752 }
753
754 t = msecs - shell->workspaces.anim_timestamp;
755
756 /*
757 * x = [0, π/2]
758 * y(x) = sin(x)
759 */
760 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
761 y = sin(x);
762
763 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600764 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200765
766 workspace_translate_out(from, shell->workspaces.anim_dir * y);
767 workspace_translate_in(to, shell->workspaces.anim_dir * y);
768 shell->workspaces.anim_current = y;
769
Scott Moreau4272e632012-08-13 09:58:41 -0600770 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200771 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200772 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200773 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200774}
775
776static void
777animate_workspace_change(struct desktop_shell *shell,
778 unsigned int index,
779 struct workspace *from,
780 struct workspace *to)
781{
782 struct weston_output *output;
783
784 int dir;
785
786 if (index > shell->workspaces.current)
787 dir = -1;
788 else
789 dir = 1;
790
791 shell->workspaces.current = index;
792
793 shell->workspaces.anim_dir = dir;
794 shell->workspaces.anim_from = from;
795 shell->workspaces.anim_to = to;
796 shell->workspaces.anim_current = 0.0;
797 shell->workspaces.anim_timestamp = 0;
798
799 output = container_of(shell->compositor->output_list.next,
800 struct weston_output, link);
801 wl_list_insert(&output->animation_list,
802 &shell->workspaces.animation.link);
803
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200804 wl_list_insert(from->layer.link.prev, &to->layer.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200805
806 workspace_translate_in(to, 0);
807
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400808 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200809
Scott Moreau4272e632012-08-13 09:58:41 -0600810 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200811}
812
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200813static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200814update_workspace(struct desktop_shell *shell, unsigned int index,
815 struct workspace *from, struct workspace *to)
816{
817 shell->workspaces.current = index;
818 wl_list_insert(&from->layer.link, &to->layer.link);
819 wl_list_remove(&from->layer.link);
820}
821
822static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200823change_workspace(struct desktop_shell *shell, unsigned int index)
824{
825 struct workspace *from;
826 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200827
828 if (index == shell->workspaces.current)
829 return;
830
831 /* Don't change workspace when there is any fullscreen surfaces. */
832 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
833 return;
834
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200835 from = get_current_workspace(shell);
836 to = get_workspace(shell, index);
837
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200838 if (shell->workspaces.anim_from == to &&
839 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200840 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200841 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200842 broadcast_current_workspace_state(shell);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200843 return;
844 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200845
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200846 if (shell->workspaces.anim_to != NULL)
847 finish_workspace_change_animation(shell,
848 shell->workspaces.anim_from,
849 shell->workspaces.anim_to);
850
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200851 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200852
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200853 if (workspace_is_empty(to) && workspace_is_empty(from))
854 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200855 else
856 animate_workspace_change(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200857
858 broadcast_current_workspace_state(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200859}
860
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200861static bool
862workspace_has_only(struct workspace *ws, struct weston_surface *surface)
863{
864 struct wl_list *list = &ws->layer.surface_list;
865 struct wl_list *e;
866
867 if (wl_list_empty(list))
868 return false;
869
870 e = list->next;
871
872 if (e->next != list)
873 return false;
874
875 return container_of(e, struct weston_surface, layer_link) == surface;
876}
877
878static void
Jonas Ådahle9d22502012-08-29 22:13:01 +0200879move_surface_to_workspace(struct desktop_shell *shell,
880 struct weston_surface *surface,
881 uint32_t workspace)
882{
883 struct workspace *from;
884 struct workspace *to;
885 struct weston_seat *seat;
886
887 if (workspace == shell->workspaces.current)
888 return;
889
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200890 if (workspace >= shell->workspaces.num)
891 workspace = shell->workspaces.num - 1;
892
Jonas Ådahle9d22502012-08-29 22:13:01 +0200893 from = get_current_workspace(shell);
894 to = get_workspace(shell, workspace);
895
896 wl_list_remove(&surface->layer_link);
897 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
898
899 drop_focus_state(shell, from, surface);
900 wl_list_for_each(seat, &shell->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400901 if (seat->keyboard && seat->keyboard->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400902 weston_keyboard_set_focus(seat->keyboard, NULL);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200903
904 weston_surface_damage_below(surface);
905}
906
907static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200908take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -0400909 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200910 unsigned int index)
911{
912 struct weston_surface *surface =
Kristian Høgsberge3148752013-05-06 23:19:49 -0400913 (struct weston_surface *) seat->keyboard->focus;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200914 struct shell_surface *shsurf;
915 struct workspace *from;
916 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +0200917 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200918
919 if (surface == NULL ||
920 index == shell->workspaces.current)
921 return;
922
923 from = get_current_workspace(shell);
924 to = get_workspace(shell, index);
925
926 wl_list_remove(&surface->layer_link);
927 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
928
Jonas Ådahle9d22502012-08-29 22:13:01 +0200929 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200930 drop_focus_state(shell, from, surface);
931
932 if (shell->workspaces.anim_from == to &&
933 shell->workspaces.anim_to == from) {
Jonas Ådahle9d22502012-08-29 22:13:01 +0200934 wl_list_remove(&to->layer.link);
935 wl_list_insert(from->layer.link.prev, &to->layer.link);
936
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200937 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +0200938 broadcast_current_workspace_state(shell);
939
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200940 return;
941 }
942
943 if (shell->workspaces.anim_to != NULL)
944 finish_workspace_change_animation(shell,
945 shell->workspaces.anim_from,
946 shell->workspaces.anim_to);
947
948 if (workspace_is_empty(from) &&
949 workspace_has_only(to, surface))
950 update_workspace(shell, index, from, to);
951 else {
952 shsurf = get_shell_surface(surface);
953 if (wl_list_empty(&shsurf->workspace_transform.link))
954 wl_list_insert(&shell->workspaces.anim_sticky_list,
955 &shsurf->workspace_transform.link);
956
957 animate_workspace_change(shell, index, from, to);
958 }
Jonas Ådahle9d22502012-08-29 22:13:01 +0200959
960 broadcast_current_workspace_state(shell);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200961
962 state = ensure_focus_state(shell, seat);
963 if (state != NULL)
964 state->keyboard_focus = surface;
Jonas Ådahle9d22502012-08-29 22:13:01 +0200965}
966
967static void
968workspace_manager_move_surface(struct wl_client *client,
969 struct wl_resource *resource,
970 struct wl_resource *surface_resource,
971 uint32_t workspace)
972{
973 struct desktop_shell *shell = resource->data;
974 struct weston_surface *surface =
975 (struct weston_surface *) surface_resource;
976
977 move_surface_to_workspace(shell, surface, workspace);
978}
979
980static const struct workspace_manager_interface workspace_manager_implementation = {
981 workspace_manager_move_surface,
982};
983
984static void
985unbind_resource(struct wl_resource *resource)
986{
987 wl_list_remove(&resource->link);
988 free(resource);
989}
990
991static void
992bind_workspace_manager(struct wl_client *client,
993 void *data, uint32_t version, uint32_t id)
994{
995 struct desktop_shell *shell = data;
996 struct wl_resource *resource;
997
998 resource = wl_client_add_object(client, &workspace_manager_interface,
999 &workspace_manager_implementation,
1000 id, shell);
1001
1002 if (resource == NULL) {
1003 weston_log("couldn't add workspace manager object");
1004 return;
1005 }
1006
1007 resource->destroy = unbind_resource;
1008 wl_list_insert(&shell->workspaces.client_list, &resource->link);
1009
1010 workspace_manager_send_state(resource,
1011 shell->workspaces.current,
1012 shell->workspaces.num);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001013}
1014
Pekka Paalanen56cdea92011-11-23 16:14:12 +02001015static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001016noop_grab_focus(struct weston_pointer_grab *grab,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001017 struct weston_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001018{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001019}
1020
1021static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001022move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001023{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001024 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001025 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001026 struct shell_surface *shsurf = move->base.shsurf;
1027 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +01001028 int dx = wl_fixed_to_int(pointer->x + move->dx);
1029 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001030
1031 if (!shsurf)
1032 return;
1033
1034 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001035
Daniel Stone103db7f2012-05-08 17:17:55 +01001036 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001037 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -04001038
1039 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001040}
1041
1042static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001043move_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001044 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001045{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001046 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1047 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001048 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001049 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001050
Daniel Stone4dbadb12012-05-30 16:31:51 +01001051 if (pointer->button_count == 0 &&
1052 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001053 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001054 free(grab);
1055 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001056}
1057
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001058static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001059 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001060 move_grab_motion,
1061 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001062};
1063
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001064static int
Kristian Høgsberge3148752013-05-06 23:19:49 -04001065surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001066{
1067 struct weston_move_grab *move;
1068
1069 if (!shsurf)
1070 return -1;
1071
1072 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1073 return 0;
1074
1075 move = malloc(sizeof *move);
1076 if (!move)
1077 return -1;
1078
1079 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001080 seat->pointer->grab_x;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001081 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Kristian Høgsberge3148752013-05-06 23:19:49 -04001082 seat->pointer->grab_y;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001083
1084 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001085 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001086
1087 return 0;
1088}
1089
1090static void
1091shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1092 struct wl_resource *seat_resource, uint32_t serial)
1093{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001094 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001095 struct shell_surface *shsurf = resource->data;
1096
Kristian Høgsberge3148752013-05-06 23:19:49 -04001097 if (seat->pointer->button_count == 0 ||
1098 seat->pointer->grab_serial != serial ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001099 seat->pointer->focus != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001100 return;
1101
Kristian Høgsberge3148752013-05-06 23:19:49 -04001102 if (surface_move(shsurf, seat) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001103 wl_resource_post_no_memory(resource);
1104}
1105
1106struct weston_resize_grab {
1107 struct shell_grab base;
1108 uint32_t edges;
1109 int32_t width, height;
1110};
1111
1112static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001113resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001114{
1115 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001116 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001117 struct shell_surface *shsurf = resize->base.shsurf;
1118 int32_t width, height;
1119 wl_fixed_t from_x, from_y;
1120 wl_fixed_t to_x, to_y;
1121
1122 if (!shsurf)
1123 return;
1124
1125 weston_surface_from_global_fixed(shsurf->surface,
1126 pointer->grab_x, pointer->grab_y,
1127 &from_x, &from_y);
1128 weston_surface_from_global_fixed(shsurf->surface,
1129 pointer->x, pointer->y, &to_x, &to_y);
1130
1131 width = resize->width;
1132 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1133 width += wl_fixed_to_int(from_x - to_x);
1134 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1135 width += wl_fixed_to_int(to_x - from_x);
1136 }
1137
1138 height = resize->height;
1139 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1140 height += wl_fixed_to_int(from_y - to_y);
1141 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1142 height += wl_fixed_to_int(to_y - from_y);
1143 }
1144
1145 shsurf->client->send_configure(shsurf->surface,
1146 resize->edges, width, height);
1147}
1148
1149static void
1150send_configure(struct weston_surface *surface,
1151 uint32_t edges, int32_t width, int32_t height)
1152{
1153 struct shell_surface *shsurf = get_shell_surface(surface);
1154
1155 wl_shell_surface_send_configure(&shsurf->resource,
1156 edges, width, height);
1157}
1158
1159static const struct weston_shell_client shell_client = {
1160 send_configure
1161};
1162
1163static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001164resize_grab_button(struct weston_pointer_grab *grab,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001165 uint32_t time, uint32_t button, uint32_t state_w)
1166{
1167 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001168 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001169 enum wl_pointer_button_state state = state_w;
1170
1171 if (pointer->button_count == 0 &&
1172 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1173 shell_grab_end(&resize->base);
1174 free(grab);
1175 }
1176}
1177
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001178static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001179 noop_grab_focus,
1180 resize_grab_motion,
1181 resize_grab_button,
1182};
1183
1184static int
1185surface_resize(struct shell_surface *shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001186 struct weston_seat *seat, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001187{
1188 struct weston_resize_grab *resize;
1189
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01001190 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1191 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001192 return 0;
1193
1194 if (edges == 0 || edges > 15 ||
1195 (edges & 3) == 3 || (edges & 12) == 12)
1196 return 0;
1197
1198 resize = malloc(sizeof *resize);
1199 if (!resize)
1200 return -1;
1201
1202 resize->edges = edges;
1203 resize->width = shsurf->surface->geometry.width;
1204 resize->height = shsurf->surface->geometry.height;
1205
1206 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001207 seat->pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001208
1209 return 0;
1210}
1211
1212static void
1213shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1214 struct wl_resource *seat_resource, uint32_t serial,
1215 uint32_t edges)
1216{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001217 struct weston_seat *seat = seat_resource->data;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001218 struct shell_surface *shsurf = resource->data;
1219
1220 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1221 return;
1222
Kristian Høgsberge3148752013-05-06 23:19:49 -04001223 if (seat->pointer->button_count == 0 ||
1224 seat->pointer->grab_serial != serial ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001225 seat->pointer->focus != shsurf->surface)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001226 return;
1227
Kristian Høgsberge3148752013-05-06 23:19:49 -04001228 if (surface_resize(shsurf, seat, edges) < 0)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001229 wl_resource_post_no_memory(resource);
1230}
1231
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001232static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001233busy_cursor_grab_focus(struct weston_pointer_grab *base,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001234 struct weston_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001235{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001236 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001237
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001238 if (grab->shsurf->surface != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001239 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001240 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001241 }
1242}
1243
1244static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001245busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001246{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001247}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001248
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001249static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001250busy_cursor_grab_button(struct weston_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001251 uint32_t time, uint32_t button, uint32_t state)
1252{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001253 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001254 struct shell_surface *shsurf = grab->shsurf;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001255 struct weston_seat *seat = grab->grab.pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001256
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001257 if (shsurf && button == BTN_LEFT && state) {
1258 activate(shsurf->shell, shsurf->surface, seat);
1259 surface_move(shsurf, seat);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001260 } else if (shsurf && button == BTN_RIGHT && state) {
1261 activate(shsurf->shell, shsurf->surface, seat);
Kristian Høgsberge3148752013-05-06 23:19:49 -04001262 surface_rotate(shsurf, seat);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001263 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001264}
1265
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001266static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001267 busy_cursor_grab_focus,
1268 busy_cursor_grab_motion,
1269 busy_cursor_grab_button,
1270};
1271
1272static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001273set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001274{
1275 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001276
1277 grab = malloc(sizeof *grab);
1278 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001279 return;
1280
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001281 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1282 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001283}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001284
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001285static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001286end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001287{
1288 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1289
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001290 if (grab->grab.interface == &busy_cursor_grab_interface &&
1291 grab->shsurf == shsurf) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001292 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001293 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001294 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001295}
1296
Scott Moreau9521d5e2012-04-19 13:06:17 -06001297static void
1298ping_timer_destroy(struct shell_surface *shsurf)
1299{
1300 if (!shsurf || !shsurf->ping_timer)
1301 return;
1302
1303 if (shsurf->ping_timer->source)
1304 wl_event_source_remove(shsurf->ping_timer->source);
1305
1306 free(shsurf->ping_timer);
1307 shsurf->ping_timer = NULL;
1308}
1309
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001310static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001311ping_timeout_handler(void *data)
1312{
1313 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001314 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001315
Scott Moreau9521d5e2012-04-19 13:06:17 -06001316 /* Client is not responding */
1317 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001318
1319 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001320 if (seat->pointer->focus == shsurf->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001321 set_busy_cursor(shsurf, seat->pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001322
1323 return 1;
1324}
1325
1326static void
1327ping_handler(struct weston_surface *surface, uint32_t serial)
1328{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001329 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001330 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001331 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001332
1333 if (!shsurf)
1334 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001335 if (!shsurf->resource.client)
1336 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001337
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001338 if (shsurf->surface == shsurf->shell->grab_surface)
1339 return;
1340
Scott Moreauff1db4a2012-04-17 19:06:18 -06001341 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001342 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001343 if (!shsurf->ping_timer)
1344 return;
1345
1346 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001347 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1348 shsurf->ping_timer->source =
1349 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1350 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1351
1352 wl_shell_surface_send_ping(&shsurf->resource, serial);
1353 }
1354}
1355
1356static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001357handle_pointer_focus(struct wl_listener *listener, void *data)
1358{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001359 struct weston_pointer *pointer = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001360 struct weston_surface *surface =
1361 (struct weston_surface *) pointer->focus;
1362 struct weston_compositor *compositor;
1363 struct shell_surface *shsurf;
1364 uint32_t serial;
1365
1366 if (!surface)
1367 return;
1368
1369 compositor = surface->compositor;
1370 shsurf = get_shell_surface(surface);
1371
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001372 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001373 set_busy_cursor(shsurf, pointer);
1374 } else {
1375 serial = wl_display_next_serial(compositor->wl_display);
1376 ping_handler(surface, serial);
1377 }
1378}
1379
1380static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001381create_pointer_focus_listener(struct weston_seat *seat)
1382{
1383 struct wl_listener *listener;
1384
Kristian Høgsberge3148752013-05-06 23:19:49 -04001385 if (!seat->pointer)
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001386 return;
1387
1388 listener = malloc(sizeof *listener);
1389 listener->notify = handle_pointer_focus;
Kristian Høgsberge3148752013-05-06 23:19:49 -04001390 wl_signal_add(&seat->pointer->focus_signal, listener);
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001391}
1392
1393static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001394shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1395 uint32_t serial)
1396{
1397 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001398 struct weston_seat *seat;
1399 struct weston_compositor *ec = shsurf->surface->compositor;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001400
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001401 if (shsurf->ping_timer == NULL)
1402 /* Just ignore unsolicited pong. */
1403 return;
1404
Scott Moreauff1db4a2012-04-17 19:06:18 -06001405 if (shsurf->ping_timer->serial == serial) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001406 shsurf->unresponsive = 0;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001407 wl_list_for_each(seat, &ec->seat_list, link)
1408 end_busy_cursor(shsurf, seat->pointer);
Scott Moreau9521d5e2012-04-19 13:06:17 -06001409 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001410 }
1411}
1412
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001413static void
1414shell_surface_set_title(struct wl_client *client,
1415 struct wl_resource *resource, const char *title)
1416{
1417 struct shell_surface *shsurf = resource->data;
1418
1419 free(shsurf->title);
1420 shsurf->title = strdup(title);
1421}
1422
1423static void
1424shell_surface_set_class(struct wl_client *client,
1425 struct wl_resource *resource, const char *class)
1426{
1427 struct shell_surface *shsurf = resource->data;
1428
1429 free(shsurf->class);
1430 shsurf->class = strdup(class);
1431}
1432
Juan Zhao96879df2012-02-07 08:45:41 +08001433static struct weston_output *
1434get_default_output(struct weston_compositor *compositor)
1435{
1436 return container_of(compositor->output_list.next,
1437 struct weston_output, link);
1438}
1439
Alex Wu4539b082012-03-01 12:57:46 +08001440static void
1441shell_unset_fullscreen(struct shell_surface *shsurf)
1442{
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001443 struct workspace *ws;
Alex Wu4539b082012-03-01 12:57:46 +08001444 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001445 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1446 shell_surface_is_top_fullscreen(shsurf)) {
1447 weston_output_switch_mode(shsurf->fullscreen_output,
1448 shsurf->fullscreen_output->origin);
1449 }
Alex Wu4539b082012-03-01 12:57:46 +08001450 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1451 shsurf->fullscreen.framerate = 0;
1452 wl_list_remove(&shsurf->fullscreen.transform.link);
1453 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001454 if (shsurf->fullscreen.black_surface)
1455 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001456 shsurf->fullscreen.black_surface = NULL;
1457 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001458 weston_surface_set_position(shsurf->surface,
1459 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001460 if (shsurf->saved_rotation_valid) {
1461 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1462 &shsurf->rotation.transform.link);
1463 shsurf->saved_rotation_valid = false;
1464 }
Rafal Mielniczuk3e3862c2012-10-07 20:25:36 +02001465
1466 ws = get_current_workspace(shsurf->shell);
1467 wl_list_remove(&shsurf->surface->layer_link);
1468 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001469}
1470
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001471static void
1472shell_unset_maximized(struct shell_surface *shsurf)
1473{
1474 struct workspace *ws;
1475 /* undo all maximized things here */
1476 shsurf->output = get_default_output(shsurf->surface->compositor);
1477 weston_surface_set_position(shsurf->surface,
1478 shsurf->saved_x,
1479 shsurf->saved_y);
1480
1481 if (shsurf->saved_rotation_valid) {
1482 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1483 &shsurf->rotation.transform.link);
1484 shsurf->saved_rotation_valid = false;
1485 }
1486
1487 ws = get_current_workspace(shsurf->shell);
1488 wl_list_remove(&shsurf->surface->layer_link);
1489 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1490}
1491
Pekka Paalanen98262232011-12-01 10:42:22 +02001492static int
1493reset_shell_surface_type(struct shell_surface *surface)
1494{
1495 switch (surface->type) {
1496 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001497 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001498 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001499 case SHELL_SURFACE_MAXIMIZED:
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001500 shell_unset_maximized(surface);
Juan Zhao96879df2012-02-07 08:45:41 +08001501 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001502 case SHELL_SURFACE_NONE:
1503 case SHELL_SURFACE_TOPLEVEL:
1504 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001505 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001506 break;
1507 }
1508
1509 surface->type = SHELL_SURFACE_NONE;
1510 return 0;
1511}
1512
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001513static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001514set_surface_type(struct shell_surface *shsurf)
1515{
1516 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001517 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001518
1519 reset_shell_surface_type(shsurf);
1520
1521 shsurf->type = shsurf->next_type;
1522 shsurf->next_type = SHELL_SURFACE_NONE;
1523
1524 switch (shsurf->type) {
1525 case SHELL_SURFACE_TOPLEVEL:
1526 break;
1527 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001528 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001529 pes->geometry.x + shsurf->transient.x,
1530 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001531 break;
1532
1533 case SHELL_SURFACE_MAXIMIZED:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001534 case SHELL_SURFACE_FULLSCREEN:
1535 shsurf->saved_x = surface->geometry.x;
1536 shsurf->saved_y = surface->geometry.y;
1537 shsurf->saved_position_valid = true;
1538
1539 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1540 wl_list_remove(&shsurf->rotation.transform.link);
1541 wl_list_init(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001542 weston_surface_geometry_dirty(shsurf->surface);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001543 shsurf->saved_rotation_valid = true;
1544 }
1545 break;
1546
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001547 default:
1548 break;
1549 }
1550}
1551
1552static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001553set_toplevel(struct shell_surface *shsurf)
1554{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001555 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001556}
1557
1558static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001559shell_surface_set_toplevel(struct wl_client *client,
1560 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001561{
Pekka Paalanen98262232011-12-01 10:42:22 +02001562 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001563
Tiago Vignattibc052c92012-04-19 16:18:18 +03001564 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001565}
1566
1567static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001568set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001569 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001570{
1571 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001572 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001573 shsurf->transient.x = x;
1574 shsurf->transient.y = y;
1575 shsurf->transient.flags = flags;
1576 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1577}
1578
1579static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001580shell_surface_set_transient(struct wl_client *client,
1581 struct wl_resource *resource,
1582 struct wl_resource *parent_resource,
1583 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001584{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001585 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001586 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001587
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001588 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001589}
1590
Tiago Vignattibe143262012-04-16 17:31:41 +03001591static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001592shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001593{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001594 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001595}
1596
1597static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001598get_output_panel_height(struct desktop_shell *shell,
1599 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001600{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001601 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001602 int panel_height = 0;
1603
1604 if (!output)
1605 return 0;
1606
Juan Zhao4ab94682012-07-09 22:24:09 -07001607 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001608 if (surface->output == output) {
1609 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001610 break;
1611 }
1612 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001613
Juan Zhao96879df2012-02-07 08:45:41 +08001614 return panel_height;
1615}
1616
1617static void
1618shell_surface_set_maximized(struct wl_client *client,
1619 struct wl_resource *resource,
1620 struct wl_resource *output_resource )
1621{
1622 struct shell_surface *shsurf = resource->data;
1623 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001624 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001625 uint32_t edges = 0, panel_height = 0;
1626
1627 /* get the default output, if the client set it as NULL
1628 check whether the ouput is available */
1629 if (output_resource)
1630 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001631 else if (es->output)
1632 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001633 else
1634 shsurf->output = get_default_output(es->compositor);
1635
Tiago Vignattibe143262012-04-16 17:31:41 +03001636 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001637 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001638 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001639
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001640 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001641 shsurf->output->width,
1642 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001643
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001644 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001645}
1646
Alex Wu21858432012-04-01 20:13:08 +08001647static void
Giulio Camuffo184df502013-02-21 11:29:21 +01001648black_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 +08001649
Alex Wu4539b082012-03-01 12:57:46 +08001650static struct weston_surface *
1651create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001652 struct weston_surface *fs_surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001653 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08001654{
1655 struct weston_surface *surface = NULL;
1656
1657 surface = weston_surface_create(ec);
1658 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001659 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001660 return NULL;
1661 }
1662
Alex Wu21858432012-04-01 20:13:08 +08001663 surface->configure = black_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01001664 surface->configure_private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001665 weston_surface_configure(surface, x, y, w, h);
1666 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03001667 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001668 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01001669 pixman_region32_fini(&surface->input);
1670 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001671
Alex Wu4539b082012-03-01 12:57:46 +08001672 return surface;
1673}
1674
1675/* Create black surface and append it to the associated fullscreen surface.
1676 * Handle size dismatch and positioning according to the method. */
1677static void
1678shell_configure_fullscreen(struct shell_surface *shsurf)
1679{
1680 struct weston_output *output = shsurf->fullscreen_output;
1681 struct weston_surface *surface = shsurf->surface;
1682 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001683 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001684
Alex Wu4539b082012-03-01 12:57:46 +08001685 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001686 shsurf->fullscreen.black_surface =
1687 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001688 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001689 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001690 output->width,
1691 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001692
1693 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1694 wl_list_insert(&surface->layer_link,
1695 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001696 shsurf->fullscreen.black_surface->output = output;
1697
1698 switch (shsurf->fullscreen.type) {
1699 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Pekka Paalanende685b82012-12-04 15:58:12 +02001700 if (surface->buffer_ref.buffer)
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001701 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001702 break;
1703 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
Rob Bradford9f3dd152013-02-12 11:53:47 +00001704 /* 1:1 mapping between surface and output dimensions */
1705 if (output->width == surface->geometry.width &&
1706 output->height == surface->geometry.height) {
1707 weston_surface_set_position(surface, output->x, output->y);
1708 break;
1709 }
1710
Alex Wu4539b082012-03-01 12:57:46 +08001711 matrix = &shsurf->fullscreen.transform.matrix;
1712 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001713
Scott Moreau1bad5db2012-08-18 01:04:05 -06001714 output_aspect = (float) output->width /
1715 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001716 surface_aspect = (float) surface->geometry.width /
1717 (float) surface->geometry.height;
1718 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001719 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001720 (float) surface->geometry.width;
1721 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001722 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001723 (float) surface->geometry.height;
1724
Alex Wu4539b082012-03-01 12:57:46 +08001725 weston_matrix_scale(matrix, scale, scale, 1);
1726 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001727 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001728 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001729 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1730 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001731 weston_surface_set_position(surface, x, y);
1732
Alex Wu4539b082012-03-01 12:57:46 +08001733 break;
1734 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001735 if (shell_surface_is_top_fullscreen(shsurf)) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001736 struct weston_mode mode = {0,
Alex Wubd3354b2012-04-17 17:20:49 +08001737 surface->geometry.width,
1738 surface->geometry.height,
1739 shsurf->fullscreen.framerate};
1740
1741 if (weston_output_switch_mode(output, &mode) == 0) {
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01001742 weston_surface_configure(shsurf->fullscreen.black_surface,
Alex Wubd3354b2012-04-17 17:20:49 +08001743 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001744 output->width,
1745 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001746 weston_surface_set_position(surface, output->x, output->y);
1747 break;
1748 }
1749 }
Alex Wu4539b082012-03-01 12:57:46 +08001750 break;
1751 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1752 break;
1753 default:
1754 break;
1755 }
1756}
1757
1758/* make the fullscreen and black surface at the top */
1759static void
1760shell_stack_fullscreen(struct shell_surface *shsurf)
1761{
Alex Wubd3354b2012-04-17 17:20:49 +08001762 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001763 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001764 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001765
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001766 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001767 wl_list_insert(&shell->fullscreen_layer.surface_list,
1768 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001769 weston_surface_damage(surface);
1770
1771 if (!shsurf->fullscreen.black_surface)
1772 shsurf->fullscreen.black_surface =
1773 create_black_surface(surface->compositor,
1774 surface,
1775 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001776 output->width,
1777 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001778
1779 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001780 wl_list_insert(&surface->layer_link,
1781 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001782 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001783}
1784
1785static void
1786shell_map_fullscreen(struct shell_surface *shsurf)
1787{
Alex Wu4539b082012-03-01 12:57:46 +08001788 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001789 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001790}
1791
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001792static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001793set_fullscreen(struct shell_surface *shsurf,
1794 uint32_t method,
1795 uint32_t framerate,
1796 struct weston_output *output)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001797{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001798 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001799
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001800 if (output)
1801 shsurf->output = output;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001802 else if (es->output)
1803 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001804 else
1805 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001806
Alex Wu4539b082012-03-01 12:57:46 +08001807 shsurf->fullscreen_output = shsurf->output;
1808 shsurf->fullscreen.type = method;
1809 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001810 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001811
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001812 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001813 shsurf->output->width,
1814 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001815}
1816
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001817static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001818shell_surface_set_fullscreen(struct wl_client *client,
1819 struct wl_resource *resource,
1820 uint32_t method,
1821 uint32_t framerate,
1822 struct wl_resource *output_resource)
1823{
1824 struct shell_surface *shsurf = resource->data;
1825 struct weston_output *output;
1826
1827 if (output_resource)
1828 output = output_resource->data;
1829 else
1830 output = NULL;
1831
1832 set_fullscreen(shsurf, method, framerate, output);
1833}
1834
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001835static const struct weston_pointer_grab_interface popup_grab_interface;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001836
1837static void
1838destroy_shell_seat(struct wl_listener *listener, void *data)
1839{
1840 struct shell_seat *shseat =
1841 container_of(listener,
1842 struct shell_seat, seat_destroy_listener);
1843 struct shell_surface *shsurf, *prev = NULL;
1844
1845 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001846 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001847 shseat->popup_grab.client = NULL;
1848
1849 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1850 shsurf->popup.shseat = NULL;
1851 if (prev) {
1852 wl_list_init(&prev->popup.grab_link);
1853 }
1854 prev = shsurf;
1855 }
1856 wl_list_init(&prev->popup.grab_link);
1857 }
1858
1859 wl_list_remove(&shseat->seat_destroy_listener.link);
1860 free(shseat);
1861}
1862
1863static struct shell_seat *
1864create_shell_seat(struct weston_seat *seat)
1865{
1866 struct shell_seat *shseat;
1867
1868 shseat = calloc(1, sizeof *shseat);
1869 if (!shseat) {
1870 weston_log("no memory to allocate shell seat\n");
1871 return NULL;
1872 }
1873
1874 shseat->seat = seat;
1875 wl_list_init(&shseat->popup_grab.surfaces_list);
1876
1877 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1878 wl_signal_add(&seat->destroy_signal,
1879 &shseat->seat_destroy_listener);
1880
1881 return shseat;
1882}
1883
1884static struct shell_seat *
1885get_shell_seat(struct weston_seat *seat)
1886{
1887 struct wl_listener *listener;
1888
1889 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
1890 if (listener == NULL)
1891 return create_shell_seat(seat);
1892
1893 return container_of(listener,
1894 struct shell_seat, seat_destroy_listener);
1895}
1896
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001897static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001898popup_grab_focus(struct weston_pointer_grab *grab,
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001899 struct weston_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01001900 wl_fixed_t x,
1901 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001902{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001903 struct weston_pointer *pointer = grab->pointer;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001904 struct shell_seat *shseat =
1905 container_of(grab, struct shell_seat, popup_grab.grab);
1906 struct wl_client *client = shseat->popup_grab.client;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001907
Pekka Paalanencb108432012-01-19 16:25:40 +02001908 if (surface && surface->resource.client == client) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001909 weston_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001910 } else {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001911 weston_pointer_set_focus(pointer, NULL,
1912 wl_fixed_from_int(0),
1913 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001914 }
1915}
1916
1917static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001918popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001919{
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001920 struct weston_pointer *pointer = grab->pointer;
1921 wl_fixed_t sx, sy;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001922
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04001923 if (pointer->focus_resource) {
1924 weston_surface_from_global_fixed(pointer->focus,
1925 pointer->x, pointer->y,
1926 &sx, &sy);
1927 wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
1928 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001929}
1930
1931static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001932popup_grab_button(struct weston_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001933 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001934{
1935 struct wl_resource *resource;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001936 struct shell_seat *shseat =
1937 container_of(grab, struct shell_seat, popup_grab.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001938 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001939 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001940 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001941
Daniel Stone37816df2012-05-16 18:45:18 +01001942 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001943 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001944 display = wl_client_get_display(resource->client);
1945 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001946 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001947 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Giulio Camuffo5085a752013-03-25 21:42:45 +01001948 (shseat->popup_grab.initial_up ||
Kristian Høgsberge3148752013-05-06 23:19:49 -04001949 time - shseat->seat->pointer->grab_time > 500)) {
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001950 popup_grab_end(grab->pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001951 }
1952
Daniel Stone4dbadb12012-05-30 16:31:51 +01001953 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Giulio Camuffo5085a752013-03-25 21:42:45 +01001954 shseat->popup_grab.initial_up = 1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001955}
1956
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001957static const struct weston_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001958 popup_grab_focus,
1959 popup_grab_motion,
1960 popup_grab_button,
1961};
1962
1963static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001964popup_grab_end(struct weston_pointer *pointer)
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001965{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001966 struct weston_pointer_grab *grab = pointer->grab;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001967 struct shell_seat *shseat =
1968 container_of(grab, struct shell_seat, popup_grab.grab);
1969 struct shell_surface *shsurf;
1970 struct shell_surface *prev = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04001971
1972 if (pointer->grab->interface == &popup_grab_interface) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001973 weston_pointer_end_grab(grab->pointer);
Giulio Camuffo5085a752013-03-25 21:42:45 +01001974 shseat->popup_grab.client = NULL;
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02001975 shseat->popup_grab.grab.interface = NULL;
1976 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
Giulio Camuffo5085a752013-03-25 21:42:45 +01001977 /* Send the popup_done event to all the popups open */
1978 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1979 wl_shell_surface_send_popup_done(&shsurf->resource);
1980 shsurf->popup.shseat = NULL;
1981 if (prev) {
1982 wl_list_init(&prev->popup.grab_link);
1983 }
1984 prev = shsurf;
1985 }
1986 wl_list_init(&prev->popup.grab_link);
1987 wl_list_init(&shseat->popup_grab.surfaces_list);
1988 }
1989}
1990
1991static void
1992add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
1993{
Kristian Høgsberge3148752013-05-06 23:19:49 -04001994 struct weston_seat *seat = shseat->seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001995
1996 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001997 shseat->popup_grab.client = shsurf->resource.client;
Giulio Camuffo5085a752013-03-25 21:42:45 +01001998 shseat->popup_grab.grab.interface = &popup_grab_interface;
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01001999 /* We must make sure here that this popup was opened after
2000 * a mouse press, and not just by moving around with other
2001 * popups already open. */
Kristian Høgsberge3148752013-05-06 23:19:49 -04002002 if (shseat->seat->pointer->button_count > 0)
Giulio Camuffo1b4b61a2013-03-27 18:05:26 +01002003 shseat->popup_grab.initial_up = 0;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002004
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002005 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002006 }
2007 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2008}
2009
2010static void
2011remove_popup_grab(struct shell_surface *shsurf)
2012{
2013 struct shell_seat *shseat = shsurf->popup.shseat;
2014
2015 wl_list_remove(&shsurf->popup.grab_link);
2016 wl_list_init(&shsurf->popup.grab_link);
2017 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002018 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
Philipp Brüschweiler63e7be62013-04-15 21:09:54 +02002019 shseat->popup_grab.grab.interface = NULL;
Kristian Høgsberg57e09072012-10-30 14:07:27 -04002020 }
2021}
2022
2023static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002024shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002025{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002026 struct shell_seat *shseat = shsurf->popup.shseat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002027 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002028 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002029
2030 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002031
Pekka Paalanen483243f2013-03-08 14:56:50 +02002032 weston_surface_set_transform_parent(es, parent);
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002033 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2034 weston_surface_update_transform(es);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002035
Kristian Høgsberge3148752013-05-06 23:19:49 -04002036 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
Giulio Camuffo5085a752013-03-25 21:42:45 +01002037 add_popup_grab(shsurf, shseat);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002038 } else {
2039 wl_shell_surface_send_popup_done(&shsurf->resource);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002040 shseat->popup_grab.client = NULL;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002041 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002042}
2043
2044static void
2045shell_surface_set_popup(struct wl_client *client,
2046 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002047 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002048 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002049 struct wl_resource *parent_resource,
2050 int32_t x, int32_t y, uint32_t flags)
2051{
2052 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002053
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002054 shsurf->type = SHELL_SURFACE_POPUP;
2055 shsurf->parent = parent_resource->data;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002056 shsurf->popup.shseat = get_shell_seat(seat_resource->data);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002057 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002058 shsurf->popup.x = x;
2059 shsurf->popup.y = y;
2060}
2061
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002062static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002063 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002064 shell_surface_move,
2065 shell_surface_resize,
2066 shell_surface_set_toplevel,
2067 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002068 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08002069 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04002070 shell_surface_set_maximized,
2071 shell_surface_set_title,
2072 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002073};
2074
2075static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002076destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002077{
Giulio Camuffo5085a752013-03-25 21:42:45 +01002078 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2079 remove_popup_grab(shsurf);
2080 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05002081
Alex Wubd3354b2012-04-17 17:20:49 +08002082 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2083 shell_surface_is_top_fullscreen(shsurf)) {
2084 weston_output_switch_mode(shsurf->fullscreen_output,
2085 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002086 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002087
Alex Wuaa08e2d2012-03-05 11:01:40 +08002088 if (shsurf->fullscreen.black_surface)
2089 weston_surface_destroy(shsurf->fullscreen.black_surface);
2090
Alex Wubd3354b2012-04-17 17:20:49 +08002091 /* As destroy_resource() use wl_list_for_each_safe(),
2092 * we can always remove the listener.
2093 */
2094 wl_list_remove(&shsurf->surface_destroy_listener.link);
2095 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06002096 ping_timer_destroy(shsurf);
Scott Moreau976a0502013-03-07 10:15:17 -07002097 free(shsurf->title);
Alex Wubd3354b2012-04-17 17:20:49 +08002098
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002099 wl_list_remove(&shsurf->link);
2100 free(shsurf);
2101}
2102
2103static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03002104shell_destroy_shell_surface(struct wl_resource *resource)
2105{
2106 struct shell_surface *shsurf = resource->data;
2107
2108 destroy_shell_surface(shsurf);
2109}
2110
2111static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002112shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002113{
2114 struct shell_surface *shsurf = container_of(listener,
2115 struct shell_surface,
2116 surface_destroy_listener);
2117
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002118 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03002119 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002120 } else {
2121 wl_signal_emit(&shsurf->resource.destroy_signal,
2122 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03002123 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04002124 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002125}
2126
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002127static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002128shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002129
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002130static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002131get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002132{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002133 if (surface->configure == shell_surface_configure)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002134 return surface->configure_private;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04002135 else
2136 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002137}
2138
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002139static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002140create_shell_surface(void *shell, struct weston_surface *surface,
2141 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002142{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002143 struct shell_surface *shsurf;
2144
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002145 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02002146 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002147 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002148 }
2149
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002150 shsurf = calloc(1, sizeof *shsurf);
2151 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02002152 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002153 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002154 }
2155
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002156 surface->configure = shell_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002157 surface->configure_private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002158
Tiago Vignattibc052c92012-04-19 16:18:18 +03002159 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002160 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002161 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002162 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002163 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08002164 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2165 shsurf->fullscreen.framerate = 0;
2166 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002167 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002168 wl_list_init(&shsurf->fullscreen.transform.link);
2169
Tiago Vignattibc052c92012-04-19 16:18:18 +03002170 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002171 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002172 wl_signal_add(&surface->resource.destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002173 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002174
2175 /* init link so its safe to always remove it in destroy_shell_surface */
2176 wl_list_init(&shsurf->link);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002177 wl_list_init(&shsurf->popup.grab_link);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002178
Pekka Paalanen460099f2012-01-20 16:48:25 +02002179 /* empty when not in use */
2180 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002181 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002182
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002183 wl_list_init(&shsurf->workspace_transform.link);
2184
Pekka Paalanen98262232011-12-01 10:42:22 +02002185 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002186 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002187
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002188 shsurf->client = client;
2189
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04002190 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002191}
2192
2193static void
2194shell_get_shell_surface(struct wl_client *client,
2195 struct wl_resource *resource,
2196 uint32_t id,
2197 struct wl_resource *surface_resource)
2198{
2199 struct weston_surface *surface = surface_resource->data;
2200 struct desktop_shell *shell = resource->data;
2201 struct shell_surface *shsurf;
2202
2203 if (get_shell_surface(surface)) {
2204 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002205 WL_DISPLAY_ERROR_INVALID_OBJECT,
2206 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03002207 return;
2208 }
2209
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002210 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002211 if (!shsurf) {
2212 wl_resource_post_error(surface_resource,
2213 WL_DISPLAY_ERROR_INVALID_OBJECT,
2214 "surface->configure already set");
2215 return;
2216 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03002217
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002218 shsurf->resource.destroy = shell_destroy_shell_surface;
2219 shsurf->resource.object.id = id;
2220 shsurf->resource.object.interface = &wl_shell_surface_interface;
2221 shsurf->resource.object.implementation =
2222 (void (**)(void)) &shell_surface_implementation;
2223 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002224
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04002225 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002226}
2227
2228static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02002229 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002230};
2231
Kristian Høgsberg07937562011-04-12 17:25:42 -04002232static void
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02002233shell_fade(struct desktop_shell *shell, enum fade_type type);
2234
2235static int
2236screensaver_timeout(void *data)
2237{
2238 struct desktop_shell *shell = data;
2239
2240 shell_fade(shell, FADE_OUT);
2241
2242 return 1;
2243}
2244
2245static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002246handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02002247{
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002248 struct desktop_shell *shell =
2249 container_of(proc, struct desktop_shell, screensaver.process);
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002250
Pekka Paalanen18027e52011-12-02 16:31:49 +02002251 proc->pid = 0;
Ander Conselvan de Oliveira18639f82013-02-15 18:44:19 +02002252
2253 if (shell->locked)
Ander Conselvan de Oliveirab17537e2013-02-22 14:16:18 +02002254 weston_compositor_sleep(shell->compositor);
Pekka Paalanen18027e52011-12-02 16:31:49 +02002255}
2256
2257static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002258launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002259{
2260 if (shell->screensaver.binding)
2261 return;
2262
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002263 if (!shell->screensaver.path) {
2264 weston_compositor_sleep(shell->compositor);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002265 return;
Ander Conselvan de Oliveiradda9d782013-02-22 14:16:19 +02002266 }
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002267
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002268 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002269 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05002270 return;
2271 }
2272
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002273 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002274 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002275 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02002276 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002277}
2278
2279static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002280terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002281{
Pekka Paalanen18027e52011-12-02 16:31:49 +02002282 if (shell->screensaver.process.pid == 0)
2283 return;
2284
2285 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002286}
2287
2288static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002289configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002290{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002291 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002292
Giulio Camuffo184df502013-02-21 11:29:21 +01002293 if (width == 0)
2294 return;
2295
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002296 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2297 if (s->output == es->output && s != es) {
2298 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002299 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002300 }
2301 }
2302
Giulio Camuffo184df502013-02-21 11:29:21 +01002303 weston_surface_configure(es, es->output->x, es->output->y, width, height);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002304
2305 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002306 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002307 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002308 }
2309}
2310
2311static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002312background_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 -04002313{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002314 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002315
Giulio Camuffo184df502013-02-21 11:29:21 +01002316 configure_static_surface(es, &shell->background_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002317}
2318
2319static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002320desktop_shell_set_background(struct wl_client *client,
2321 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002322 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002323 struct wl_resource *surface_resource)
2324{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002325 struct desktop_shell *shell = resource->data;
2326 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002327
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002328 if (surface->configure) {
2329 wl_resource_post_error(surface_resource,
2330 WL_DISPLAY_ERROR_INVALID_OBJECT,
2331 "surface role already assigned");
2332 return;
2333 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002334
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002335 surface->configure = background_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002336 surface->configure_private = shell;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002337 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002338 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002339 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002340 surface->output->width,
2341 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002342}
2343
2344static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002345panel_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 -04002346{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002347 struct desktop_shell *shell = es->configure_private;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002348
Giulio Camuffo184df502013-02-21 11:29:21 +01002349 configure_static_surface(es, &shell->panel_layer, width, height);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002350}
2351
2352static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002353desktop_shell_set_panel(struct wl_client *client,
2354 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002355 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002356 struct wl_resource *surface_resource)
2357{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002358 struct desktop_shell *shell = resource->data;
2359 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002360
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002361 if (surface->configure) {
2362 wl_resource_post_error(surface_resource,
2363 WL_DISPLAY_ERROR_INVALID_OBJECT,
2364 "surface role already assigned");
2365 return;
2366 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002367
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002368 surface->configure = panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002369 surface->configure_private = shell;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002370 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002371 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002372 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002373 surface->output->width,
2374 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002375}
2376
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002377static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002378lock_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 -04002379{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002380 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002381
Giulio Camuffo184df502013-02-21 11:29:21 +01002382 if (width == 0)
2383 return;
2384
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002385 center_on_output(surface, get_default_output(shell->compositor));
2386
2387 if (!weston_surface_is_mapped(surface)) {
2388 wl_list_insert(&shell->lock_layer.surface_list,
2389 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002390 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002391 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002392 }
2393}
2394
2395static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002396handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002397{
Tiago Vignattibe143262012-04-16 17:31:41 +03002398 struct desktop_shell *shell =
2399 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002400
Martin Minarik6d118362012-06-07 18:01:59 +02002401 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002402 shell->lock_surface = NULL;
2403}
2404
2405static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002406desktop_shell_set_lock_surface(struct wl_client *client,
2407 struct wl_resource *resource,
2408 struct wl_resource *surface_resource)
2409{
Tiago Vignattibe143262012-04-16 17:31:41 +03002410 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002411 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002412
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002413 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002414
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002415 if (!shell->locked)
2416 return;
2417
Pekka Paalanen98262232011-12-01 10:42:22 +02002418 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002419
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002420 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2421 wl_signal_add(&surface_resource->destroy_signal,
2422 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002423
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002424 surface->configure = lock_surface_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002425 surface->configure_private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002426}
2427
2428static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002429resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002430{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002431 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002432
Pekka Paalanen77346a62011-11-30 16:26:35 +02002433 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002434
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002435 wl_list_remove(&shell->lock_layer.link);
2436 wl_list_insert(&shell->compositor->cursor_layer.link,
2437 &shell->fullscreen_layer.link);
2438 wl_list_insert(&shell->fullscreen_layer.link,
2439 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002440 if (shell->showing_input_panels) {
2441 wl_list_insert(&shell->panel_layer.link,
2442 &shell->input_panel_layer.link);
2443 wl_list_insert(&shell->input_panel_layer.link,
2444 &ws->layer.link);
2445 } else {
2446 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2447 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002448
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002449 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002450
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002451 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002452 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002453 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002454}
2455
2456static void
2457desktop_shell_unlock(struct wl_client *client,
2458 struct wl_resource *resource)
2459{
Tiago Vignattibe143262012-04-16 17:31:41 +03002460 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002461
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002462 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002463
2464 if (shell->locked)
2465 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002466}
2467
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002468static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002469desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002470 struct wl_resource *resource,
2471 struct wl_resource *surface_resource)
2472{
2473 struct desktop_shell *shell = resource->data;
2474
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002475 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002476}
2477
Kristian Høgsberg75840622011-09-06 13:48:16 -04002478static const struct desktop_shell_interface desktop_shell_implementation = {
2479 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002480 desktop_shell_set_panel,
2481 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002482 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002483 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002484};
2485
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002486static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002487get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002488{
2489 struct shell_surface *shsurf;
2490
2491 shsurf = get_shell_surface(surface);
2492 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002493 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002494 return shsurf->type;
2495}
2496
Kristian Høgsberg75840622011-09-06 13:48:16 -04002497static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002498move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002499{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002500 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002501 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002502 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002503
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002504 if (surface == NULL)
2505 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002506
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002507 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002508 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2509 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002510 return;
2511
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002512 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002513}
2514
2515static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002516resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002517{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002518 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002519 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002520 uint32_t edges = 0;
2521 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002522 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002523
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002524 if (surface == NULL)
2525 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002526
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002527 shsurf = get_shell_surface(surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002528 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2529 shsurf->type == SHELL_SURFACE_MAXIMIZED)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002530 return;
2531
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002532 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002533 wl_fixed_to_int(seat->pointer->grab_x),
2534 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002535 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002536
Pekka Paalanen60921e52012-01-25 15:55:43 +02002537 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002538 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002539 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002540 edges |= 0;
2541 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002542 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002543
Pekka Paalanen60921e52012-01-25 15:55:43 +02002544 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002545 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002546 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002547 edges |= 0;
2548 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002549 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002550
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002551 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002552}
2553
2554static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002555surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002556 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002557{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002558 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002559 struct shell_surface *shsurf;
2560 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002561 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002562
2563 if (surface == NULL)
2564 return;
2565
2566 shsurf = get_shell_surface(surface);
2567 if (!shsurf)
2568 return;
2569
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002570 surface->alpha -= wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002571
Scott Moreau02709af2012-05-22 01:54:10 -06002572 if (surface->alpha > 1.0)
2573 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002574 if (surface->alpha < step)
2575 surface->alpha = step;
2576
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002577 weston_surface_geometry_dirty(surface);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002578 weston_surface_damage(surface);
2579}
2580
2581static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002582do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002583 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002584{
Daniel Stone37816df2012-05-16 18:45:18 +01002585 struct weston_seat *ws = (struct weston_seat *) seat;
2586 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002587 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002588 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002589
2590 wl_list_for_each(output, &compositor->output_list, link) {
2591 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002592 wl_fixed_to_double(seat->pointer->x),
2593 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002594 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002595 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002596 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002597 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002598 increment = -output->zoom.increment;
2599 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002600 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002601 increment = output->zoom.increment *
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02002602 -wl_fixed_to_double(value) / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002603 else
2604 increment = 0;
2605
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002606 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002607
Scott Moreaue6603982012-06-11 13:07:51 -06002608 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002609 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002610 else if (output->zoom.level > output->zoom.max_level)
2611 output->zoom.level = output->zoom.max_level;
Ville Syrjäläaa628d02012-11-16 11:48:47 +02002612 else if (!output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002613 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002614 output->disable_planes++;
2615 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002616
Scott Moreaue6603982012-06-11 13:07:51 -06002617 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002618
Scott Moreau8dacaab2012-06-17 18:10:58 -06002619 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002620 }
2621 }
2622}
2623
Scott Moreauccbf29d2012-02-22 14:21:41 -07002624static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002625zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002626 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002627{
2628 do_zoom(seat, time, 0, axis, value);
2629}
2630
2631static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002632zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002633 void *data)
2634{
2635 do_zoom(seat, time, key, 0, 0);
2636}
2637
2638static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002639terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002640 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002641{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002642 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002643
Daniel Stone325fc2d2012-05-30 16:31:58 +01002644 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002645}
2646
2647static void
Kristian Høgsbergbe6403e2013-05-08 21:03:21 -04002648rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002649{
2650 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002651 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002652 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002653 struct shell_surface *shsurf = rotate->base.shsurf;
2654 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002655 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002656
2657 if (!shsurf)
2658 return;
2659
2660 surface = shsurf->surface;
2661
2662 cx = 0.5f * surface->geometry.width;
2663 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002664
Daniel Stone37816df2012-05-16 18:45:18 +01002665 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2666 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002667 r = sqrtf(dx * dx + dy * dy);
2668
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002669 wl_list_remove(&shsurf->rotation.transform.link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02002670 weston_surface_geometry_dirty(shsurf->surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002671
2672 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002673 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002674 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002675
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002676 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002677 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002678
2679 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002680 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002681 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002682 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002683 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002684
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002685 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002686 &shsurf->surface->geometry.transformation_list,
2687 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002688 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002689 wl_list_init(&shsurf->rotation.transform.link);
2690 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002691 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002692 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002693
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002694 /* We need to adjust the position of the surface
2695 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002696 cposx = surface->geometry.x + cx;
2697 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002698 dposx = rotate->center.x - cposx;
2699 dposy = rotate->center.y - cposy;
2700 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002701 weston_surface_set_position(surface,
2702 surface->geometry.x + dposx,
2703 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002704 }
2705
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002706 /* Repaint implies weston_surface_update_transform(), which
2707 * lazily applies the damage due to rotation update.
2708 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002709 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002710}
2711
2712static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002713rotate_grab_button(struct weston_pointer_grab *grab,
2714 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002715{
2716 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002717 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002718 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002719 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002720 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002721
Daniel Stone4dbadb12012-05-30 16:31:51 +01002722 if (pointer->button_count == 0 &&
2723 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002724 if (shsurf)
2725 weston_matrix_multiply(&shsurf->rotation.rotation,
2726 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002727 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002728 free(rotate);
2729 }
2730}
2731
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04002732static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002733 noop_grab_focus,
2734 rotate_grab_motion,
2735 rotate_grab_button,
2736};
2737
2738static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002739surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002740{
Pekka Paalanen460099f2012-01-20 16:48:25 +02002741 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002742 float dx, dy;
2743 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002744
Pekka Paalanen460099f2012-01-20 16:48:25 +02002745 rotate = malloc(sizeof *rotate);
2746 if (!rotate)
2747 return;
2748
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002749 weston_surface_to_global_float(surface->surface,
2750 surface->surface->geometry.width / 2,
2751 surface->surface->geometry.height / 2,
2752 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002753
Daniel Stone37816df2012-05-16 18:45:18 +01002754 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2755 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002756 r = sqrtf(dx * dx + dy * dy);
2757 if (r > 20.0f) {
2758 struct weston_matrix inverse;
2759
2760 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002761 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002762 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002763
2764 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002765 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002766 } else {
2767 weston_matrix_init(&surface->rotation.rotation);
2768 weston_matrix_init(&rotate->rotation);
2769 }
2770
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002771 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2772 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002773}
2774
2775static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002776rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002777 void *data)
2778{
2779 struct weston_surface *base_surface =
2780 (struct weston_surface *) seat->pointer->focus;
2781 struct shell_surface *surface;
2782
2783 if (base_surface == NULL)
2784 return;
2785
2786 surface = get_shell_surface(base_surface);
Rafal Mielniczuk23c67592013-03-11 19:26:53 +01002787 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2788 surface->type == SHELL_SURFACE_MAXIMIZED)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05002789 return;
2790
2791 surface_rotate(surface, seat);
2792}
2793
2794static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002795lower_fullscreen_layer(struct desktop_shell *shell)
2796{
2797 struct workspace *ws;
2798 struct weston_surface *surface, *prev;
2799
2800 ws = get_current_workspace(shell);
2801 wl_list_for_each_reverse_safe(surface, prev,
2802 &shell->fullscreen_layer.surface_list,
2803 layer_link)
2804 weston_surface_restack(surface, &ws->layer.surface_list);
2805}
2806
2807static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002808activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002809 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002810{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002811 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02002812 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002813
Daniel Stone37816df2012-05-16 18:45:18 +01002814 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002815
Jonas Ådahl8538b222012-08-29 22:13:03 +02002816 state = ensure_focus_state(shell, seat);
2817 if (state == NULL)
2818 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002819
2820 state->keyboard_focus = es;
2821 wl_list_remove(&state->surface_destroy_listener.link);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04002822 wl_signal_add(&es->resource.destroy_signal,
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002823 &state->surface_destroy_listener);
2824
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002825 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002826 case SHELL_SURFACE_FULLSCREEN:
2827 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002828 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002829 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002830 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002831 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002832 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002833 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002834 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002835 }
2836}
2837
Alex Wu21858432012-04-01 20:13:08 +08002838/* no-op func for checking black surface */
2839static void
Giulio Camuffo184df502013-02-21 11:29:21 +01002840black_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 +08002841{
2842}
2843
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01002844static bool
Alex Wu21858432012-04-01 20:13:08 +08002845is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2846{
2847 if (es->configure == black_surface_configure) {
2848 if (fs_surface)
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01002849 *fs_surface = (struct weston_surface *)es->configure_private;
Alex Wu21858432012-04-01 20:13:08 +08002850 return true;
2851 }
2852 return false;
2853}
2854
Kristian Høgsberg75840622011-09-06 13:48:16 -04002855static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002856click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002857 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002858{
Daniel Stone37816df2012-05-16 18:45:18 +01002859 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002860 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002861 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002862 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002863
Daniel Stone37816df2012-05-16 18:45:18 +01002864 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002865 if (!focus)
2866 return;
2867
Alex Wu21858432012-04-01 20:13:08 +08002868 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002869 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002870
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002871 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2872 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002873
Daniel Stone325fc2d2012-05-30 16:31:58 +01002874 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002875 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002876}
2877
2878static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002879lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002880{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002881 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002882
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002883 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002884 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002885 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002886 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002887
2888 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002889
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002890 /* Hide all surfaces by removing the fullscreen, panel and
2891 * toplevel layers. This way nothing else can show or receive
2892 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002893
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002894 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002895 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002896 if (shell->showing_input_panels)
2897 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002898 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002899 wl_list_insert(&shell->compositor->cursor_layer.link,
2900 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002901
Pekka Paalanen77346a62011-11-30 16:26:35 +02002902 launch_screensaver(shell);
2903
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002904 /* TODO: disable bindings that should not work while locked. */
2905
2906 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002907}
2908
2909static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002910unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002911{
Pekka Paalanend81c2162011-11-16 13:47:34 +02002912 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002913 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002914 return;
2915 }
2916
2917 /* If desktop-shell client has gone away, unlock immediately. */
2918 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002919 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002920 return;
2921 }
2922
2923 if (shell->prepare_event_sent)
2924 return;
2925
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002926 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002927 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002928}
2929
2930static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002931shell_fade_done(struct weston_surface_animation *animation, void *data)
2932{
2933 struct desktop_shell *shell = data;
2934
2935 shell->fade.animation = NULL;
2936
2937 switch (shell->fade.type) {
2938 case FADE_IN:
2939 weston_surface_destroy(shell->fade.surface);
2940 shell->fade.surface = NULL;
2941 break;
2942 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002943 lock(shell);
2944 break;
2945 }
2946}
2947
2948static void
2949shell_fade(struct desktop_shell *shell, enum fade_type type)
2950{
2951 struct weston_compositor *compositor = shell->compositor;
2952 struct weston_surface *surface;
2953 float tint;
2954
2955 switch (type) {
2956 case FADE_IN:
2957 tint = 0.0;
2958 break;
2959 case FADE_OUT:
2960 tint = 1.0;
2961 break;
2962 default:
2963 weston_log("shell: invalid fade type\n");
2964 return;
2965 }
2966
2967 shell->fade.type = type;
2968
2969 if (shell->fade.surface == NULL) {
2970 surface = weston_surface_create(compositor);
2971 if (!surface)
2972 return;
2973
2974 weston_surface_configure(surface, 0, 0, 8192, 8192);
2975 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
2976 surface->alpha = 1.0 - tint;
2977 wl_list_insert(&compositor->fade_layer.surface_list,
2978 &surface->layer_link);
2979 weston_surface_update_transform(surface);
2980 shell->fade.surface = surface;
2981 pixman_region32_init(&surface->input);
2982 }
2983
2984 if (shell->fade.animation)
2985 weston_fade_update(shell->fade.animation,
2986 shell->fade.surface->alpha, tint, 30.0);
2987 else
2988 shell->fade.animation =
2989 weston_fade_run(shell->fade.surface,
2990 1.0 - tint, tint, 30.0,
2991 shell_fade_done, shell);
2992}
2993
2994static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002995idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002996{
2997 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002998 container_of(listener, struct desktop_shell, idle_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002999
3000 shell_fade(shell, FADE_OUT);
3001 /* lock() is called from shell_fade_done() */
3002}
3003
3004static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003005wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003006{
3007 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003008 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003009
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003010 unlock(shell);
3011}
3012
3013static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003014show_input_panels(struct wl_listener *listener, void *data)
3015{
3016 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003017 container_of(listener, struct desktop_shell,
3018 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003019 struct input_panel_surface *surface, *next;
3020 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003021
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003022 shell->text_input.surface = (struct weston_surface*)data;
3023
Jan Arne Petersen451a9712013-02-11 15:10:11 +01003024 if (shell->showing_input_panels)
3025 return;
3026
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003027 shell->showing_input_panels = true;
3028
Jan Arne Petersencf18a322012-11-07 15:32:54 +01003029 if (!shell->locked)
3030 wl_list_insert(&shell->panel_layer.link,
3031 &shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003032
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003033 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003034 &shell->input_panel.surfaces, link) {
3035 ws = surface->surface;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003036 if (!ws->buffer_ref.buffer)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003037 continue;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003038 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003039 &ws->layer_link);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003040 weston_surface_geometry_dirty(ws);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003041 weston_surface_update_transform(ws);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003042 weston_surface_damage(ws);
3043 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003044 }
3045}
3046
3047static void
3048hide_input_panels(struct wl_listener *listener, void *data)
3049{
3050 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003051 container_of(listener, struct desktop_shell,
3052 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003053 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003054
Jan Arne Petersen61381972013-01-31 15:52:21 +01003055 if (!shell->showing_input_panels)
3056 return;
3057
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003058 shell->showing_input_panels = false;
3059
Jan Arne Petersen82ec9092012-12-03 15:36:02 +01003060 if (!shell->locked)
3061 wl_list_remove(&shell->input_panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003062
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003063 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003064 &shell->input_panel_layer.surface_list, layer_link)
3065 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003066}
3067
3068static void
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003069update_input_panels(struct wl_listener *listener, void *data)
3070{
3071 struct desktop_shell *shell =
3072 container_of(listener, struct desktop_shell,
3073 update_input_panel_listener);
3074
3075 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3076}
3077
3078static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003079center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02003080{
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003081 int32_t width = weston_surface_buffer_width(surface);
3082 int32_t height = weston_surface_buffer_height(surface);
3083 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003084
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003085 x = output->x + (output->width - width) / 2;
3086 y = output->y + (output->height - height) / 2;
3087
3088 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003089}
3090
3091static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003092weston_surface_set_initial_position (struct weston_surface *surface,
3093 struct desktop_shell *shell)
3094{
3095 struct weston_compositor *compositor = shell->compositor;
3096 int ix = 0, iy = 0;
3097 int range_x, range_y;
3098 int dx, dy, x, y, panel_height;
3099 struct weston_output *output, *target_output = NULL;
3100 struct weston_seat *seat;
3101
3102 /* As a heuristic place the new window on the same output as the
3103 * pointer. Falling back to the output containing 0, 0.
3104 *
3105 * TODO: Do something clever for touch too?
3106 */
3107 wl_list_for_each(seat, &compositor->seat_list, link) {
Kristian Høgsberg2bf87622013-05-07 23:17:41 -04003108 if (seat->pointer) {
Kristian Høgsberge3148752013-05-06 23:19:49 -04003109 ix = wl_fixed_to_int(seat->pointer->x);
3110 iy = wl_fixed_to_int(seat->pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003111 break;
3112 }
3113 }
3114
3115 wl_list_for_each(output, &compositor->output_list, link) {
3116 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3117 target_output = output;
3118 break;
3119 }
3120 }
3121
3122 if (!target_output) {
3123 weston_surface_set_position(surface, 10 + random() % 400,
3124 10 + random() % 400);
3125 return;
3126 }
3127
3128 /* Valid range within output where the surface will still be onscreen.
3129 * If this is negative it means that the surface is bigger than
3130 * output.
3131 */
3132 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003133 range_x = target_output->width - surface->geometry.width;
3134 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003135 surface->geometry.height;
3136
Rob Bradford4cb88c72012-08-13 15:18:44 +01003137 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003138 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003139 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01003140 dx = 0;
3141
3142 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003143 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01003144 else
3145 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003146
3147 x = target_output->x + dx;
3148 y = target_output->y + dy;
3149
3150 weston_surface_set_position (surface, x, y);
3151}
3152
3153static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003154map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003155 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003156{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003157 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003158 struct shell_surface *shsurf = get_shell_surface(surface);
3159 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003160 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01003161 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003162 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08003163 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003164
Pekka Paalanen60921e52012-01-25 15:55:43 +02003165 surface->geometry.width = width;
3166 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003167 weston_surface_geometry_dirty(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003168
3169 /* initial positioning, see also configure() */
3170 switch (surface_type) {
3171 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01003172 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003173 break;
Alex Wu4539b082012-03-01 12:57:46 +08003174 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04003175 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08003176 shell_map_fullscreen(shsurf);
3177 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003178 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003179 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08003180 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01003181 weston_surface_set_position(surface, shsurf->output->x,
3182 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08003183 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02003184 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04003185 shell_map_popup(shsurf);
Rob Bradforddb999382012-12-06 12:07:48 +00003186 break;
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02003187 case SHELL_SURFACE_NONE:
3188 weston_surface_set_position(surface,
3189 surface->geometry.x + sx,
3190 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02003191 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003192 default:
3193 ;
3194 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003195
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02003196 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003197 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003198 case SHELL_SURFACE_POPUP:
3199 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04003200 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05003201 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3202 break;
Alex Wu4539b082012-03-01 12:57:46 +08003203 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02003204 case SHELL_SURFACE_NONE:
3205 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003206 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003207 ws = get_current_workspace(shell);
3208 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003209 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003210 }
3211
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003212 if (surface_type != SHELL_SURFACE_NONE) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003213 weston_surface_update_transform(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02003214 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3215 surface->output = shsurf->output;
3216 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003217
Juan Zhao7bb92f02011-12-15 11:31:51 -05003218 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05003219 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03003220 if (shsurf->transient.flags ==
3221 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3222 break;
3223 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05003224 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08003225 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01003226 if (!shell->locked) {
3227 wl_list_for_each(seat, &compositor->seat_list, link)
3228 activate(shell, surface, seat);
3229 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05003230 break;
3231 default:
3232 break;
3233 }
3234
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05003235 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08003236 {
3237 switch (shell->win_animation_type) {
3238 case ANIMATION_FADE:
Ander Conselvan de Oliveiraee416052013-02-21 18:35:17 +02003239 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
Juan Zhaoe10d2792012-04-25 19:09:52 +08003240 break;
3241 case ANIMATION_ZOOM:
3242 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3243 break;
3244 default:
3245 break;
3246 }
3247 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003248}
3249
3250static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003251configure(struct desktop_shell *shell, struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003252 float x, float y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003253{
Pekka Paalanen77346a62011-11-30 16:26:35 +02003254 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3255 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003256
Pekka Paalanen77346a62011-11-30 16:26:35 +02003257 shsurf = get_shell_surface(surface);
3258 if (shsurf)
3259 surface_type = shsurf->type;
3260
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003261 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003262
3263 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08003264 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08003265 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003266 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08003267 break;
Juan Zhao96879df2012-02-07 08:45:41 +08003268 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08003269 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003270 surface->geometry.x = surface->output->x;
3271 surface->geometry.y = surface->output->y +
3272 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08003273 break;
Alex Wu4539b082012-03-01 12:57:46 +08003274 case SHELL_SURFACE_TOPLEVEL:
3275 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003276 default:
3277 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04003278 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05003279
Alex Wu4539b082012-03-01 12:57:46 +08003280 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05003281 if (surface->output) {
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003282 weston_surface_update_transform(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003283
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003284 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08003285 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003286 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04003287}
3288
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003289static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003290shell_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 +03003291{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03003292 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03003293 struct desktop_shell *shell = shsurf->shell;
Giulio Camuffo184df502013-02-21 11:29:21 +01003294
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03003295 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003296
Giulio Camuffo5085a752013-03-25 21:42:45 +01003297 if (!weston_surface_is_mapped(es) && !wl_list_empty(&shsurf->popup.grab_link)) {
3298 remove_popup_grab(shsurf);
3299 }
3300
Giulio Camuffo184df502013-02-21 11:29:21 +01003301 if (width == 0)
3302 return;
3303
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003304 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003305 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003306 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003307 type_changed = 1;
3308 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04003309
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003310 if (!weston_surface_is_mapped(es)) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003311 map(shell, es, width, height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04003312 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003313 es->geometry.width != width ||
3314 es->geometry.height != height) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003315 float from_x, from_y;
3316 float to_x, to_y;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003317
3318 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3319 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3320 configure(shell, es,
3321 es->geometry.x + to_x - from_x,
3322 es->geometry.y + to_y - from_y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003323 width, height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003324 }
3325}
3326
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003327static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003328
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003329static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003330desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003331{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003332 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03003333 struct desktop_shell *shell =
3334 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003335
3336 shell->child.process.pid = 0;
3337 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003338
3339 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3340 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003341 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003342 shell->child.deathstamp = time;
3343 shell->child.deathcount = 0;
3344 }
3345
3346 shell->child.deathcount++;
3347 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02003348 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003349 return;
3350 }
3351
Martin Minarik6d118362012-06-07 18:01:59 +02003352 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02003353 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003354}
3355
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003356static void
3357launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003358{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003359 struct desktop_shell *shell = data;
Kristian Høgsberg9724b512012-01-03 14:35:49 -05003360 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003361
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003362 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02003363 &shell->child.process,
3364 shell_exe,
3365 desktop_shell_sigchld);
3366
3367 if (!shell->child.client)
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03003368 weston_log("not able to start %s\n", shell_exe);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003369}
3370
3371static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003372bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3373{
Tiago Vignattibe143262012-04-16 17:31:41 +03003374 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003375
3376 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003377 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003378}
3379
Kristian Høgsberg75840622011-09-06 13:48:16 -04003380static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003381unbind_desktop_shell(struct wl_resource *resource)
3382{
Tiago Vignattibe143262012-04-16 17:31:41 +03003383 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003384
3385 if (shell->locked)
3386 resume_desktop(shell);
3387
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003388 shell->child.desktop_shell = NULL;
3389 shell->prepare_event_sent = false;
3390 free(resource);
3391}
3392
3393static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003394bind_desktop_shell(struct wl_client *client,
3395 void *data, uint32_t version, uint32_t id)
3396{
Tiago Vignattibe143262012-04-16 17:31:41 +03003397 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003398 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003399
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003400 resource = wl_client_add_object(client, &desktop_shell_interface,
3401 &desktop_shell_implementation,
3402 id, shell);
3403
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003404 if (client == shell->child.client) {
3405 resource->destroy = unbind_desktop_shell;
3406 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003407 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003408 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02003409
3410 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3411 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003412 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003413}
3414
Pekka Paalanen6e168112011-11-24 11:34:05 +02003415static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003416screensaver_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 -04003417{
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003418 struct desktop_shell *shell = surface->configure_private;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003419
Giulio Camuffo184df502013-02-21 11:29:21 +01003420 if (width == 0)
3421 return;
3422
Pekka Paalanen3a1d07d2012-12-20 14:02:13 +02003423 /* XXX: starting weston-screensaver beforehand does not work */
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003424 if (!shell->locked)
3425 return;
3426
3427 center_on_output(surface, surface->output);
3428
3429 if (wl_list_empty(&surface->layer_link)) {
3430 wl_list_insert(shell->lock_layer.surface_list.prev,
3431 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03003432 weston_surface_update_transform(surface);
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02003433 wl_event_source_timer_update(shell->screensaver.timer,
3434 shell->screensaver.duration);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003435 shell_fade(shell, FADE_IN);
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003436 }
3437}
3438
3439static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02003440screensaver_set_surface(struct wl_client *client,
3441 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003442 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02003443 struct wl_resource *output_resource)
3444{
Tiago Vignattibe143262012-04-16 17:31:41 +03003445 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003446 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003447 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003448
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04003449 surface->configure = screensaver_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003450 surface->configure_private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003451 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003452}
3453
3454static const struct screensaver_interface screensaver_implementation = {
3455 screensaver_set_surface
3456};
3457
3458static void
3459unbind_screensaver(struct wl_resource *resource)
3460{
Tiago Vignattibe143262012-04-16 17:31:41 +03003461 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003462
Pekka Paalanen77346a62011-11-30 16:26:35 +02003463 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003464 free(resource);
3465}
3466
3467static void
3468bind_screensaver(struct wl_client *client,
3469 void *data, uint32_t version, uint32_t id)
3470{
Tiago Vignattibe143262012-04-16 17:31:41 +03003471 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003472 struct wl_resource *resource;
3473
3474 resource = wl_client_add_object(client, &screensaver_interface,
3475 &screensaver_implementation,
3476 id, shell);
3477
Pekka Paalanen77346a62011-11-30 16:26:35 +02003478 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02003479 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02003480 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02003481 return;
3482 }
3483
3484 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3485 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003486 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02003487}
3488
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003489static void
Giulio Camuffo184df502013-02-21 11:29:21 +01003490input_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 -04003491{
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003492 struct input_panel_surface *ip_surface = surface->configure_private;
3493 struct desktop_shell *shell = ip_surface->shell;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003494 struct weston_mode *mode;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003495 float x, y;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003496 uint32_t show_surface = 0;
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003497
Giulio Camuffo184df502013-02-21 11:29:21 +01003498 if (width == 0)
3499 return;
3500
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003501 if (!weston_surface_is_mapped(surface)) {
3502 if (!shell->showing_input_panels)
3503 return;
3504
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003505 show_surface = 1;
3506 }
Jan Arne Petersenaf7b6c92013-01-16 21:26:53 +01003507
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003508 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003509
3510 if (ip_surface->panel) {
3511 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3512 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3513 } else {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003514 mode = ip_surface->output->current;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003515
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003516 x = ip_surface->output->x + (mode->width - width) / 2;
3517 y = ip_surface->output->y + mode->height - height;
3518 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003519
3520 weston_surface_configure(surface,
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003521 x, y,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003522 width, height);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003523
3524 if (show_surface) {
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003525 wl_list_insert(&shell->input_panel_layer.surface_list,
3526 &surface->layer_link);
3527 weston_surface_update_transform(surface);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003528 weston_surface_damage(surface);
3529 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3530 }
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003531}
3532
3533static void
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003534destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003535{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003536 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003537 wl_list_remove(&input_panel_surface->link);
3538
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003539 input_panel_surface->surface->configure = NULL;
3540
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003541 free(input_panel_surface);
3542}
3543
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003544static struct input_panel_surface *
3545get_input_panel_surface(struct weston_surface *surface)
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003546{
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003547 if (surface->configure == input_panel_configure) {
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003548 return surface->configure_private;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003549 } else {
3550 return NULL;
3551 }
3552}
3553
3554static void
3555input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3556{
3557 struct input_panel_surface *ipsurface = container_of(listener,
3558 struct input_panel_surface,
3559 surface_destroy_listener);
3560
3561 if (ipsurface->resource.client) {
3562 wl_resource_destroy(&ipsurface->resource);
3563 } else {
3564 wl_signal_emit(&ipsurface->resource.destroy_signal,
3565 &ipsurface->resource);
3566 destroy_input_panel_surface(ipsurface);
3567 }
3568}
3569static struct input_panel_surface *
3570create_input_panel_surface(struct desktop_shell *shell,
3571 struct weston_surface *surface)
3572{
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003573 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003574
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003575 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3576 if (!input_panel_surface)
3577 return NULL;
3578
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003579 surface->configure = input_panel_configure;
Giulio Camuffo7fe01b12013-03-28 18:02:42 +01003580 surface->configure_private = input_panel_surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003581
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003582 input_panel_surface->shell = shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003583
3584 input_panel_surface->surface = surface;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003585
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003586 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3587 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04003588 wl_signal_add(&surface->resource.destroy_signal,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003589 &input_panel_surface->surface_destroy_listener);
3590
3591 wl_list_init(&input_panel_surface->link);
3592
3593 return input_panel_surface;
3594}
3595
3596static void
3597input_panel_surface_set_toplevel(struct wl_client *client,
3598 struct wl_resource *resource,
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003599 struct wl_resource *output_resource,
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003600 uint32_t position)
3601{
3602 struct input_panel_surface *input_panel_surface = resource->data;
3603 struct desktop_shell *shell = input_panel_surface->shell;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003604
3605 wl_list_insert(&shell->input_panel.surfaces,
3606 &input_panel_surface->link);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003607
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003608 input_panel_surface->output = output_resource->data;
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003609 input_panel_surface->panel = 0;
Jan Arne Petersen7cd29e12013-04-18 16:47:29 +02003610
3611 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__,
3612 input_panel_surface->panel,
3613 input_panel_surface->output);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003614}
3615
3616static void
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003617input_panel_surface_set_overlay_panel(struct wl_client *client,
3618 struct wl_resource *resource)
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003619{
3620 struct input_panel_surface *input_panel_surface = resource->data;
3621 struct desktop_shell *shell = input_panel_surface->shell;
3622
3623 wl_list_insert(&shell->input_panel.surfaces,
3624 &input_panel_surface->link);
3625
3626 input_panel_surface->panel = 1;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003627}
3628
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003629static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003630 input_panel_surface_set_toplevel,
Jan Arne Petersen70d942b2013-04-18 16:47:37 +02003631 input_panel_surface_set_overlay_panel
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003632};
3633
3634static void
3635destroy_input_panel_surface_resource(struct wl_resource *resource)
3636{
3637 struct input_panel_surface *ipsurf = resource->data;
3638
3639 destroy_input_panel_surface(ipsurf);
3640}
3641
3642static void
3643input_panel_get_input_panel_surface(struct wl_client *client,
3644 struct wl_resource *resource,
3645 uint32_t id,
3646 struct wl_resource *surface_resource)
3647{
3648 struct weston_surface *surface = surface_resource->data;
3649 struct desktop_shell *shell = resource->data;
3650 struct input_panel_surface *ipsurf;
3651
3652 if (get_input_panel_surface(surface)) {
3653 wl_resource_post_error(surface_resource,
3654 WL_DISPLAY_ERROR_INVALID_OBJECT,
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003655 "wl_input_panel::get_input_panel_surface already requested");
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003656 return;
3657 }
3658
3659 ipsurf = create_input_panel_surface(shell, surface);
3660 if (!ipsurf) {
3661 wl_resource_post_error(surface_resource,
3662 WL_DISPLAY_ERROR_INVALID_OBJECT,
3663 "surface->configure already set");
3664 return;
3665 }
3666
3667 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3668 ipsurf->resource.object.id = id;
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003669 ipsurf->resource.object.interface = &wl_input_panel_surface_interface;
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003670 ipsurf->resource.object.implementation =
3671 (void (**)(void)) &input_panel_surface_implementation;
3672 ipsurf->resource.data = ipsurf;
3673
3674 wl_client_add_resource(client, &ipsurf->resource);
3675}
3676
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003677static const struct wl_input_panel_interface input_panel_implementation = {
Jan Arne Petersenffbb20f2013-01-16 21:26:55 +01003678 input_panel_get_input_panel_surface
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003679};
3680
3681static void
3682unbind_input_panel(struct wl_resource *resource)
3683{
3684 struct desktop_shell *shell = resource->data;
3685
3686 shell->input_panel.binding = NULL;
3687 free(resource);
3688}
3689
3690static void
3691bind_input_panel(struct wl_client *client,
3692 void *data, uint32_t version, uint32_t id)
3693{
3694 struct desktop_shell *shell = data;
3695 struct wl_resource *resource;
3696
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02003697 resource = wl_client_add_object(client, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003698 &input_panel_implementation,
3699 id, shell);
3700
3701 if (shell->input_panel.binding == NULL) {
3702 resource->destroy = unbind_input_panel;
3703 shell->input_panel.binding = resource;
3704 return;
3705 }
3706
3707 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3708 "interface object already bound");
3709 wl_resource_destroy(resource);
3710}
3711
Kristian Høgsberg07045392012-02-19 18:52:44 -05003712struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003713 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003714 struct weston_surface *current;
3715 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003716 struct weston_keyboard_grab grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003717};
3718
3719static void
3720switcher_next(struct switcher *switcher)
3721{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003722 struct weston_surface *surface;
3723 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003724 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003725 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003726
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003727 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003728 switch (get_shell_surface_type(surface)) {
3729 case SHELL_SURFACE_TOPLEVEL:
3730 case SHELL_SURFACE_FULLSCREEN:
3731 case SHELL_SURFACE_MAXIMIZED:
3732 if (first == NULL)
3733 first = surface;
3734 if (prev == switcher->current)
3735 next = surface;
3736 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003737 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003738 weston_surface_geometry_dirty(surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003739 weston_surface_damage(surface);
3740 break;
3741 default:
3742 break;
3743 }
Alex Wu1659daa2012-04-01 20:13:09 +08003744
3745 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003746 surface->alpha = 0.25;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02003747 weston_surface_geometry_dirty(surface);
Alex Wu1659daa2012-04-01 20:13:09 +08003748 weston_surface_damage(surface);
3749 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003750 }
3751
3752 if (next == NULL)
3753 next = first;
3754
Alex Wu07b26062012-03-12 16:06:01 +08003755 if (next == NULL)
3756 return;
3757
Kristian Høgsberg07045392012-02-19 18:52:44 -05003758 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04003759 wl_signal_add(&next->resource.destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003760
3761 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003762 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003763
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003764 shsurf = get_shell_surface(switcher->current);
3765 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003766 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003767}
3768
3769static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003770switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003771{
3772 struct switcher *switcher =
3773 container_of(listener, struct switcher, listener);
3774
3775 switcher_next(switcher);
3776}
3777
3778static void
Daniel Stone351eb612012-05-31 15:27:47 -04003779switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003780{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003781 struct weston_surface *surface;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003782 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003783 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003784
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003785 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003786 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003787 weston_surface_damage(surface);
3788 }
3789
Alex Wu07b26062012-03-12 16:06:01 +08003790 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003791 activate(switcher->shell, switcher->current,
3792 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003793 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003794 weston_keyboard_end_grab(keyboard);
3795 if (keyboard->input_method_resource)
3796 keyboard->grab = &keyboard->input_method_grab;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003797 free(switcher);
3798}
3799
3800static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003801switcher_key(struct weston_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003802 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003803{
3804 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003805 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003806
Daniel Stonec9785ea2012-05-30 16:31:52 +01003807 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003808 switcher_next(switcher);
3809}
3810
3811static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003812switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04003813 uint32_t mods_depressed, uint32_t mods_latched,
3814 uint32_t mods_locked, uint32_t group)
3815{
3816 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003817 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003818
Daniel Stone351eb612012-05-31 15:27:47 -04003819 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3820 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003821}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003822
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003823static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003824 switcher_key,
3825 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003826};
3827
3828static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003829switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003830 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003831{
Tiago Vignattibe143262012-04-16 17:31:41 +03003832 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003833 struct switcher *switcher;
3834
3835 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003836 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003837 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003838 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003839 wl_list_init(&switcher->listener.link);
3840
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003841 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003842 switcher->grab.interface = &switcher_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003843 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
3844 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003845 switcher_next(switcher);
3846}
3847
Pekka Paalanen3c647232011-12-22 13:43:43 +02003848static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003849backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003850 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003851{
3852 struct weston_compositor *compositor = data;
3853 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003854 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003855
3856 /* TODO: we're limiting to simple use cases, where we assume just
3857 * control on the primary display. We'd have to extend later if we
3858 * ever get support for setting backlights on random desktop LCD
3859 * panels though */
3860 output = get_default_output(compositor);
3861 if (!output)
3862 return;
3863
3864 if (!output->set_backlight)
3865 return;
3866
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003867 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3868 backlight_new = output->backlight_current - 25;
3869 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3870 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003871
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003872 if (backlight_new < 5)
3873 backlight_new = 5;
3874 if (backlight_new > 255)
3875 backlight_new = 255;
3876
3877 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003878 output->set_backlight(output, output->backlight_current);
3879}
3880
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003881struct debug_binding_grab {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003882 struct weston_keyboard_grab grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003883 struct weston_seat *seat;
3884 uint32_t key[2];
3885 int key_released[2];
3886};
3887
3888static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003889debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003890 uint32_t key, uint32_t state)
3891{
3892 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3893 struct wl_resource *resource;
3894 struct wl_display *display;
3895 uint32_t serial;
3896 int send = 0, terminate = 0;
3897 int check_binding = 1;
3898 int i;
3899
3900 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3901 /* Do not run bindings on key releases */
3902 check_binding = 0;
3903
3904 for (i = 0; i < 2; i++)
3905 if (key == db->key[i])
3906 db->key_released[i] = 1;
3907
3908 if (db->key_released[0] && db->key_released[1]) {
3909 /* All key releases been swalled so end the grab */
3910 terminate = 1;
3911 } else if (key != db->key[0] && key != db->key[1]) {
3912 /* Should not swallow release of other keys */
3913 send = 1;
3914 }
3915 } else if (key == db->key[0] && !db->key_released[0]) {
3916 /* Do not check bindings for the first press of the binding
3917 * key. This allows it to be used as a debug shortcut.
3918 * We still need to swallow this event. */
3919 check_binding = 0;
3920 } else if (db->key[1]) {
3921 /* If we already ran a binding don't process another one since
3922 * we can't keep track of all the binding keys that were
3923 * pressed in order to swallow the release events. */
3924 send = 1;
3925 check_binding = 0;
3926 }
3927
3928 if (check_binding) {
3929 struct weston_compositor *ec = db->seat->compositor;
3930
3931 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3932 key, state)) {
3933 /* We ran a binding so swallow the press and keep the
3934 * grab to swallow the released too. */
3935 send = 0;
3936 terminate = 0;
3937 db->key[1] = key;
3938 } else {
3939 /* Terminate the grab since the key pressed is not a
3940 * debug binding key. */
3941 send = 1;
3942 terminate = 1;
3943 }
3944 }
3945
3946 if (send) {
3947 resource = grab->keyboard->focus_resource;
3948
3949 if (resource) {
3950 display = wl_client_get_display(resource->client);
3951 serial = wl_display_next_serial(display);
3952 wl_keyboard_send_key(resource, serial, time, key, state);
3953 }
3954 }
3955
3956 if (terminate) {
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003957 weston_keyboard_end_grab(grab->keyboard);
3958 if (grab->keyboard->input_method_resource)
3959 grab->keyboard->grab = &grab->keyboard->input_method_grab;
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003960 free(db);
3961 }
3962}
3963
3964static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003965debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003966 uint32_t mods_depressed, uint32_t mods_latched,
3967 uint32_t mods_locked, uint32_t group)
3968{
3969 struct wl_resource *resource;
3970
3971 resource = grab->keyboard->focus_resource;
3972 if (!resource)
3973 return;
3974
3975 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3976 mods_latched, mods_locked, group);
3977}
3978
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003979struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003980 debug_binding_key,
3981 debug_binding_modifiers
3982};
3983
3984static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04003985debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003986{
3987 struct debug_binding_grab *grab;
3988
3989 grab = calloc(1, sizeof *grab);
3990 if (!grab)
3991 return;
3992
3993 grab->seat = (struct weston_seat *) seat;
3994 grab->key[0] = key;
3995 grab->grab.interface = &debug_binding_keyboard_grab;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04003996 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003997}
3998
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003999static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004000force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01004001 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004002{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004003 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004004 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004005 struct desktop_shell *shell = data;
4006 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004007 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004008
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004009 focus_surface = seat->keyboard->focus;
4010 if (!focus_surface)
4011 return;
4012
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004013 wl_signal_emit(&compositor->kill_signal, focus_surface);
4014
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004015 client = focus_surface->resource.client;
Tiago Vignatti920f1972012-09-27 17:48:35 +03004016 wl_client_get_credentials(client, &pid, NULL, NULL);
4017
4018 /* Skip clients that we launched ourselves (the credentials of
4019 * the socketpair is ours) */
4020 if (pid == getpid())
4021 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004022
Daniel Stone325fc2d2012-05-30 16:31:58 +01004023 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004024}
4025
4026static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004027workspace_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004028 uint32_t key, void *data)
4029{
4030 struct desktop_shell *shell = data;
4031 unsigned int new_index = shell->workspaces.current;
4032
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004033 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004034 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004035 if (new_index != 0)
4036 new_index--;
4037
4038 change_workspace(shell, new_index);
4039}
4040
4041static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004042workspace_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004043 uint32_t key, void *data)
4044{
4045 struct desktop_shell *shell = data;
4046 unsigned int new_index = shell->workspaces.current;
4047
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004048 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004049 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004050 if (new_index < shell->workspaces.num - 1)
4051 new_index++;
4052
4053 change_workspace(shell, new_index);
4054}
4055
4056static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004057workspace_f_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004058 uint32_t key, void *data)
4059{
4060 struct desktop_shell *shell = data;
4061 unsigned int new_index;
4062
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004063 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004064 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004065 new_index = key - KEY_F1;
4066 if (new_index >= shell->workspaces.num)
4067 new_index = shell->workspaces.num - 1;
4068
4069 change_workspace(shell, new_index);
4070}
4071
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004072static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004073workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004074 uint32_t key, void *data)
4075{
4076 struct desktop_shell *shell = data;
4077 unsigned int new_index = shell->workspaces.current;
4078
4079 if (shell->locked)
4080 return;
4081
4082 if (new_index != 0)
4083 new_index--;
4084
4085 take_surface_to_workspace_by_seat(shell, seat, new_index);
4086}
4087
4088static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04004089workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004090 uint32_t key, void *data)
4091{
4092 struct desktop_shell *shell = data;
4093 unsigned int new_index = shell->workspaces.current;
4094
4095 if (shell->locked)
4096 return;
4097
4098 if (new_index < shell->workspaces.num - 1)
4099 new_index++;
4100
4101 take_surface_to_workspace_by_seat(shell, seat, new_index);
4102}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004103
4104static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004105shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004106{
Tiago Vignattibe143262012-04-16 17:31:41 +03004107 struct desktop_shell *shell =
4108 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004109 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004110
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004111 if (shell->child.client)
4112 wl_client_destroy(shell->child.client);
4113
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004114 wl_list_remove(&shell->idle_listener.link);
4115 wl_list_remove(&shell->wake_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004116 wl_list_remove(&shell->show_input_panel_listener.link);
4117 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004118
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004119 wl_array_for_each(ws, &shell->workspaces.array)
4120 workspace_destroy(*ws);
4121 wl_array_release(&shell->workspaces.array);
4122
Pekka Paalanen3c647232011-12-22 13:43:43 +02004123 free(shell->screensaver.path);
4124 free(shell);
4125}
4126
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004127static void
4128shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4129{
4130 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004131 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004132
4133 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004134 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4135 MODIFIER_CTRL | MODIFIER_ALT,
4136 terminate_binding, ec);
4137 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4138 click_to_activate_binding,
4139 shell);
4140 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4141 MODIFIER_SUPER | MODIFIER_ALT,
4142 surface_opacity_binding, NULL);
4143 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4144 MODIFIER_SUPER, zoom_axis_binding,
4145 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004146
4147 /* configurable bindings */
4148 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01004149 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4150 zoom_key_binding, NULL);
4151 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4152 zoom_key_binding, NULL);
4153 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4154 shell);
4155 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4156 resize_binding, shell);
4157 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4158 rotate_binding, NULL);
4159 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4160 shell);
4161 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4162 ec);
4163 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4164 backlight_binding, ec);
4165 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4166 ec);
4167 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4168 backlight_binding, ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004169 weston_compositor_add_key_binding(ec, KEY_K, mod,
4170 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004171 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4172 workspace_up_binding, shell);
4173 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4174 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004175 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4176 workspace_move_surface_up_binding,
4177 shell);
4178 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4179 workspace_move_surface_down_binding,
4180 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004181
4182 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4183 if (shell->workspaces.num > 1) {
4184 num_workspace_bindings = shell->workspaces.num;
4185 if (num_workspace_bindings > 6)
4186 num_workspace_bindings = 6;
4187 for (i = 0; i < num_workspace_bindings; i++)
4188 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4189 workspace_f_binding,
4190 shell);
4191 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004192
4193 /* Debug bindings */
4194 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4195 debug_binding, shell);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004196}
4197
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004198WL_EXPORT int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004199module_init(struct weston_compositor *ec,
4200 int *argc, char *argv[], const char *config_file)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004201{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004202 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03004203 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004204 struct workspace **pws;
4205 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004206 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004207
4208 shell = malloc(sizeof *shell);
4209 if (shell == NULL)
4210 return -1;
4211
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04004212 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04004213 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004214
4215 shell->destroy_listener.notify = shell_destroy;
4216 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004217 shell->idle_listener.notify = idle_handler;
4218 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4219 shell->wake_listener.notify = wake_handler;
4220 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004221 shell->show_input_panel_listener.notify = show_input_panels;
4222 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4223 shell->hide_input_panel_listener.notify = hide_input_panels;
4224 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004225 shell->update_input_panel_listener.notify = update_input_panels;
4226 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06004227 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04004228 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03004229 ec->shell_interface.create_shell_surface = create_shell_surface;
4230 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04004231 ec->shell_interface.set_transient = set_transient;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05004232 ec->shell_interface.set_fullscreen = set_fullscreen;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04004233 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04004234 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004235
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004236 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004237
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004238 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4239 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004240 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4241 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004242 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004243
4244 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02004245 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05004246
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004247 shell_configuration(shell, config_file);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02004248
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004249 for (i = 0; i < shell->workspaces.num; i++) {
4250 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4251 if (pws == NULL)
4252 return -1;
4253
4254 *pws = workspace_create();
4255 if (*pws == NULL)
4256 return -1;
4257 }
4258 activate_workspace(shell, 0);
4259
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004260 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02004261 wl_list_init(&shell->workspaces.animation.link);
4262 shell->workspaces.animation.frame = animate_workspace_change_frame;
4263
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004264 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4265 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004266 return -1;
4267
Kristian Høgsberg75840622011-09-06 13:48:16 -04004268 if (wl_display_add_global(ec->wl_display,
4269 &desktop_shell_interface,
4270 shell, bind_desktop_shell) == NULL)
4271 return -1;
4272
Pekka Paalanen6e168112011-11-24 11:34:05 +02004273 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4274 shell, bind_screensaver) == NULL)
4275 return -1;
4276
Jan Arne Petersencc75ec12013-04-18 16:47:39 +02004277 if (wl_display_add_global(ec->wl_display, &wl_input_panel_interface,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004278 shell, bind_input_panel) == NULL)
4279 return -1;
4280
Jonas Ådahle9d22502012-08-29 22:13:01 +02004281 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4282 shell, bind_workspace_manager) == NULL)
4283 return -1;
4284
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05004285 shell->child.deathstamp = weston_compositor_get_time();
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004286
4287 loop = wl_display_get_event_loop(ec->wl_display);
4288 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004289
Ander Conselvan de Oliveira859e8852013-02-21 18:35:21 +02004290 shell->screensaver.timer =
4291 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4292
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04004293 wl_list_for_each(seat, &ec->seat_list, link)
4294 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04004295
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004296 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04004297
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004298 shell_fade(shell, FADE_IN);
4299
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004300 return 0;
4301}