blob: 87e688c8a8c529bca4b68337da52d7120273480c [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"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020038#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050039
Jonas Ådahle3cddce2012-06-13 00:01:22 +020040#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020041#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020042
Juan Zhaoe10d2792012-04-25 19:09:52 +080043enum animation_type {
44 ANIMATION_NONE,
45
46 ANIMATION_ZOOM,
47 ANIMATION_FADE
48};
49
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
51 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040052 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Jonas Ådahle3cddce2012-06-13 00:01:22 +020059struct workspace {
60 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020061
62 struct wl_list focus_list;
63 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020064};
65
Philipp Brüschweiler88013572012-08-06 13:44:42 +020066struct input_panel_surface {
67 struct wl_list link;
68 struct weston_surface *surface;
69 struct wl_listener listener;
70};
71
Tiago Vignattibe143262012-04-16 17:31:41 +030072struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050073 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040074
75 struct wl_listener lock_listener;
76 struct wl_listener unlock_listener;
77 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020078 struct wl_listener show_input_panel_listener;
79 struct wl_listener hide_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020080
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050081 struct weston_layer fullscreen_layer;
82 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050083 struct weston_layer background_layer;
84 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +020085 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050086
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040087 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030088 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040089
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020090 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050091 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020092 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020093 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020094
95 unsigned deathcount;
96 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020097 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020098
99 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200100 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200101 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200102
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400103 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500104 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100105
Pekka Paalanen77346a62011-11-30 16:26:35 +0200106 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200107 struct wl_array array;
108 unsigned int current;
109 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200110
111 struct weston_animation animation;
112 int anim_dir;
113 uint32_t anim_timestamp;
114 double anim_current;
115 struct workspace *anim_from;
116 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200117 } workspaces;
118
119 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200120 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200121 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200122 struct wl_resource *binding;
123 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500124 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200125 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500126
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200127 struct {
128 struct wl_resource *binding;
129 struct wl_list surfaces;
130 } input_panel;
131
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300132 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800133 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500134 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400135};
136
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500137enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200138 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500139 SHELL_SURFACE_TOPLEVEL,
140 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500141 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800142 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500143 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200144};
145
Scott Moreauff1db4a2012-04-17 19:06:18 -0600146struct ping_timer {
147 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600148 uint32_t serial;
149};
150
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200151struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200152 struct wl_resource resource;
153
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500154 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200155 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400156 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300157 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200158
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400159 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400160 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500161 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800162 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800163 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600164 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100165
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500166 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200167 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500168 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200169 } rotation;
170
171 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700172 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500173 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200174 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500175 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100176 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400177 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500178 } popup;
179
Alex Wu4539b082012-03-01 12:57:46 +0800180 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300181 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400182 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300183 } transient;
184
185 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800186 enum wl_shell_surface_fullscreen_method type;
187 struct weston_transform transform; /* matrix from x, y */
188 uint32_t framerate;
189 struct weston_surface *black_surface;
190 } fullscreen;
191
Scott Moreauff1db4a2012-04-17 19:06:18 -0600192 struct ping_timer *ping_timer;
193
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200194 struct weston_transform workspace_transform;
195
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500196 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500197 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100198 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400199
200 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200201};
202
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300203struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700204 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300205 struct shell_surface *shsurf;
206 struct wl_listener shsurf_destroy_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300207 struct wl_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300208};
209
210struct weston_move_grab {
211 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100212 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500213};
214
Pekka Paalanen460099f2012-01-20 16:48:25 +0200215struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300216 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500217 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200218 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400219 GLfloat x;
220 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200221 } center;
222};
223
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400224static void
225activate(struct desktop_shell *shell, struct weston_surface *es,
226 struct weston_seat *seat);
227
228static struct workspace *
229get_current_workspace(struct desktop_shell *shell);
230
Alex Wubd3354b2012-04-17 17:20:49 +0800231static struct shell_surface *
232get_shell_surface(struct weston_surface *surface);
233
234static struct desktop_shell *
235shell_surface_get_shell(struct shell_surface *shsurf);
236
237static bool
238shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
239{
240 struct desktop_shell *shell;
241 struct weston_surface *top_fs_es;
242
243 shell = shell_surface_get_shell(shsurf);
244
245 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
246 return false;
247
248 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
249 struct weston_surface,
250 layer_link);
251 return (shsurf == get_shell_surface(top_fs_es));
252}
253
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500254static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400255destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300256{
257 struct shell_grab *grab;
258
259 grab = container_of(listener, struct shell_grab,
260 shsurf_destroy_listener);
261
262 grab->shsurf = NULL;
263}
264
265static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300266shell_grab_start(struct shell_grab *grab,
267 const struct wl_pointer_grab_interface *interface,
268 struct shell_surface *shsurf,
269 struct wl_pointer *pointer,
270 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300271{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300272 struct desktop_shell *shell = shsurf->shell;
273
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300274 grab->grab.interface = interface;
275 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400276 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
277 wl_signal_add(&shsurf->resource.destroy_signal,
278 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300279
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300280 grab->pointer = pointer;
281 grab->grab.focus = &shsurf->surface->surface;
282
283 wl_pointer_start_grab(pointer, &grab->grab);
284 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
285 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
286 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300287}
288
289static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300290shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300291{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400292 if (grab->shsurf)
293 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300294
295 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300296}
297
298static void
Alex Wu4539b082012-03-01 12:57:46 +0800299center_on_output(struct weston_surface *surface,
300 struct weston_output *output);
301
Daniel Stone496ca172012-05-30 16:31:42 +0100302static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300303get_modifier(char *modifier)
304{
305 if (!modifier)
306 return MODIFIER_SUPER;
307
308 if (!strcmp("ctrl", modifier))
309 return MODIFIER_CTRL;
310 else if (!strcmp("alt", modifier))
311 return MODIFIER_ALT;
312 else if (!strcmp("super", modifier))
313 return MODIFIER_SUPER;
314 else
315 return MODIFIER_SUPER;
316}
317
Juan Zhaoe10d2792012-04-25 19:09:52 +0800318static enum animation_type
319get_animation_type(char *animation)
320{
321 if (!animation)
322 return ANIMATION_NONE;
323
324 if (!strcmp("zoom", animation))
325 return ANIMATION_ZOOM;
326 else if (!strcmp("fade", animation))
327 return ANIMATION_FADE;
328 else
329 return ANIMATION_NONE;
330}
331
Alex Wu4539b082012-03-01 12:57:46 +0800332static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300333shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200334{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200335 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200336 char *path = NULL;
337 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200338 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300339 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800340 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200341
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400342 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300343 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800344 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200345 { "num-workspaces",
346 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200347 };
348
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400349 struct config_key saver_keys[] = {
350 { "path", CONFIG_KEY_STRING, &path },
351 { "duration", CONFIG_KEY_INTEGER, &duration },
352 };
353
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200354 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400355 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200356 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
357 };
358
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200359 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500360 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200361 free(config_file);
362
Pekka Paalanen7296e792011-12-07 16:22:00 +0200363 shell->screensaver.path = path;
364 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300365 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800366 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200367 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
368}
369
370static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200371focus_state_destroy(struct focus_state *state)
372{
373 wl_list_remove(&state->seat_destroy_listener.link);
374 wl_list_remove(&state->surface_destroy_listener.link);
375 free(state);
376}
377
378static void
379focus_state_seat_destroy(struct wl_listener *listener, void *data)
380{
381 struct focus_state *state = container_of(listener,
382 struct focus_state,
383 seat_destroy_listener);
384
385 wl_list_remove(&state->link);
386 focus_state_destroy(state);
387}
388
389static void
390focus_state_surface_destroy(struct wl_listener *listener, void *data)
391{
392 struct focus_state *state = container_of(listener,
393 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400394 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400395 struct desktop_shell *shell;
396 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200397
Kristian Høgsberge3778222012-07-31 17:29:30 -0400398 next = NULL;
399 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
400 if (surface == state->keyboard_focus)
401 continue;
402
403 next = surface;
404 break;
405 }
406
407 if (next) {
408 shell = state->seat->compositor->shell_interface.shell;
409 activate(shell, next, state->seat);
410 } else {
411 wl_list_remove(&state->link);
412 focus_state_destroy(state);
413 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200414}
415
416static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400417focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200418{
Jonas Ådahl04769742012-06-13 00:01:24 +0200419 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200420
421 state = malloc(sizeof *state);
422 if (state == NULL)
423 return NULL;
424
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400425 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200426 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400427 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200428
429 state->seat_destroy_listener.notify = focus_state_seat_destroy;
430 state->surface_destroy_listener.notify = focus_state_surface_destroy;
431 wl_signal_add(&seat->seat.destroy_signal,
432 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400433 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200434
435 return state;
436}
437
438static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400439restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200440{
441 struct focus_state *state, *next;
442
443 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
444 if (state->keyboard_focus)
445 wl_keyboard_set_focus(state->seat->seat.keyboard,
446 &state->keyboard_focus->surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200447 }
448}
449
450static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200451workspace_destroy(struct workspace *ws)
452{
Jonas Ådahl04769742012-06-13 00:01:24 +0200453 struct focus_state *state, *next;
454
455 wl_list_for_each_safe(state, next, &ws->focus_list, link)
456 focus_state_destroy(state);
457
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200458 free(ws);
459}
460
Jonas Ådahl04769742012-06-13 00:01:24 +0200461static void
462seat_destroyed(struct wl_listener *listener, void *data)
463{
464 struct weston_seat *seat = data;
465 struct focus_state *state, *next;
466 struct workspace *ws = container_of(listener,
467 struct workspace,
468 seat_destroyed_listener);
469
470 wl_list_for_each_safe(state, next, &ws->focus_list, link)
471 if (state->seat == seat)
472 wl_list_remove(&state->link);
473}
474
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200475static struct workspace *
476workspace_create(void)
477{
478 struct workspace *ws = malloc(sizeof *ws);
479 if (ws == NULL)
480 return NULL;
481
482 weston_layer_init(&ws->layer, NULL);
483
Jonas Ådahl04769742012-06-13 00:01:24 +0200484 wl_list_init(&ws->focus_list);
485 wl_list_init(&ws->seat_destroyed_listener.link);
486 ws->seat_destroyed_listener.notify = seat_destroyed;
487
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200488 return ws;
489}
490
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200491static int
492workspace_is_empty(struct workspace *ws)
493{
494 return wl_list_empty(&ws->layer.surface_list);
495}
496
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200497static struct workspace *
498get_workspace(struct desktop_shell *shell, unsigned int index)
499{
500 struct workspace **pws = shell->workspaces.array.data;
501 pws += index;
502 return *pws;
503}
504
505static struct workspace *
506get_current_workspace(struct desktop_shell *shell)
507{
508 return get_workspace(shell, shell->workspaces.current);
509}
510
511static void
512activate_workspace(struct desktop_shell *shell, unsigned int index)
513{
514 struct workspace *ws;
515
516 ws = get_workspace(shell, index);
517 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
518
519 shell->workspaces.current = index;
520}
521
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200522static unsigned int
523get_output_height(struct weston_output *output)
524{
525 return abs(output->region.extents.y1 - output->region.extents.y2);
526}
527
528static void
529surface_translate(struct weston_surface *surface, double d)
530{
531 struct shell_surface *shsurf = get_shell_surface(surface);
532 struct weston_transform *transform;
533
534 transform = &shsurf->workspace_transform;
535 if (wl_list_empty(&transform->link))
536 wl_list_insert(surface->geometry.transformation_list.prev,
537 &shsurf->workspace_transform.link);
538
539 weston_matrix_init(&shsurf->workspace_transform.matrix);
540 weston_matrix_translate(&shsurf->workspace_transform.matrix,
541 0.0, d, 0.0);
542 surface->geometry.dirty = 1;
543}
544
545static void
546workspace_translate_out(struct workspace *ws, double fraction)
547{
548 struct weston_surface *surface;
549 unsigned int height;
550 double d;
551
552 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
553 height = get_output_height(surface->output);
554 d = height * fraction;
555
556 surface_translate(surface, d);
557 }
558}
559
560static void
561workspace_translate_in(struct workspace *ws, double fraction)
562{
563 struct weston_surface *surface;
564 unsigned int height;
565 double d;
566
567 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
568 height = get_output_height(surface->output);
569
570 if (fraction > 0)
571 d = -(height - height * fraction);
572 else
573 d = height + height * fraction;
574
575 surface_translate(surface, d);
576 }
577}
578
579static void
580workspace_damage_all_surfaces(struct workspace *ws)
581{
582 struct weston_surface *surface;
583
584 wl_list_for_each(surface, &ws->layer.surface_list, layer_link)
585 weston_surface_damage(surface);
586}
587
588static void
589reverse_workspace_change_animation(struct desktop_shell *shell,
590 unsigned int index,
591 struct workspace *from,
592 struct workspace *to)
593{
594 shell->workspaces.current = index;
595
596 shell->workspaces.anim_to = to;
597 shell->workspaces.anim_from = from;
598 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
599 shell->workspaces.anim_timestamp = 0;
600
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400601 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200602
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200603 workspace_damage_all_surfaces(from);
604 workspace_damage_all_surfaces(to);
605}
606
607static void
608workspace_deactivate_transforms(struct workspace *ws)
609{
610 struct weston_surface *surface;
611 struct shell_surface *shsurf;
612
613 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
614 shsurf = get_shell_surface(surface);
615 wl_list_remove(&shsurf->workspace_transform.link);
616 wl_list_init(&shsurf->workspace_transform.link);
617 shsurf->surface->geometry.dirty = 1;
618 }
619}
620
621static void
622finish_workspace_change_animation(struct desktop_shell *shell,
623 struct workspace *from,
624 struct workspace *to)
625{
626 workspace_damage_all_surfaces(from);
627 workspace_damage_all_surfaces(to);
628
629 wl_list_remove(&shell->workspaces.animation.link);
630 workspace_deactivate_transforms(from);
631 workspace_deactivate_transforms(to);
632 shell->workspaces.anim_to = NULL;
633
634 wl_list_remove(&shell->workspaces.anim_from->layer.link);
635}
636
637static void
638animate_workspace_change_frame(struct weston_animation *animation,
639 struct weston_output *output, uint32_t msecs)
640{
641 struct desktop_shell *shell =
642 container_of(animation, struct desktop_shell,
643 workspaces.animation);
644 struct workspace *from = shell->workspaces.anim_from;
645 struct workspace *to = shell->workspaces.anim_to;
646 uint32_t t;
647 double x, y;
648
649 if (workspace_is_empty(from) && workspace_is_empty(to)) {
650 finish_workspace_change_animation(shell, from, to);
651 return;
652 }
653
654 if (shell->workspaces.anim_timestamp == 0) {
655 if (shell->workspaces.anim_current == 0.0)
656 shell->workspaces.anim_timestamp = msecs;
657 else
658 shell->workspaces.anim_timestamp =
659 msecs -
660 /* Invers of movement function 'y' below. */
661 (asin(1.0 - shell->workspaces.anim_current) *
662 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
663 M_2_PI);
664 }
665
666 t = msecs - shell->workspaces.anim_timestamp;
667
668 /*
669 * x = [0, π/2]
670 * y(x) = sin(x)
671 */
672 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
673 y = sin(x);
674
675 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
676 workspace_damage_all_surfaces(from);
677 workspace_damage_all_surfaces(to);
678
679 workspace_translate_out(from, shell->workspaces.anim_dir * y);
680 workspace_translate_in(to, shell->workspaces.anim_dir * y);
681 shell->workspaces.anim_current = y;
682
683 workspace_damage_all_surfaces(from);
684 workspace_damage_all_surfaces(to);
685 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200686 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200687 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200688}
689
690static void
691animate_workspace_change(struct desktop_shell *shell,
692 unsigned int index,
693 struct workspace *from,
694 struct workspace *to)
695{
696 struct weston_output *output;
697
698 int dir;
699
700 if (index > shell->workspaces.current)
701 dir = -1;
702 else
703 dir = 1;
704
705 shell->workspaces.current = index;
706
707 shell->workspaces.anim_dir = dir;
708 shell->workspaces.anim_from = from;
709 shell->workspaces.anim_to = to;
710 shell->workspaces.anim_current = 0.0;
711 shell->workspaces.anim_timestamp = 0;
712
713 output = container_of(shell->compositor->output_list.next,
714 struct weston_output, link);
715 wl_list_insert(&output->animation_list,
716 &shell->workspaces.animation.link);
717
718 wl_list_insert(&from->layer.link, &to->layer.link);
719
720 workspace_translate_in(to, 0);
721
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400722 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200723
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200724 workspace_damage_all_surfaces(from);
725 workspace_damage_all_surfaces(to);
726}
727
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200728static void
729change_workspace(struct desktop_shell *shell, unsigned int index)
730{
731 struct workspace *from;
732 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200733
734 if (index == shell->workspaces.current)
735 return;
736
737 /* Don't change workspace when there is any fullscreen surfaces. */
738 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
739 return;
740
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200741 from = get_current_workspace(shell);
742 to = get_workspace(shell, index);
743
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200744 if (shell->workspaces.anim_from == to &&
745 shell->workspaces.anim_to == from) {
746 reverse_workspace_change_animation(shell, index, from, to);
747 return;
748 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200749
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200750 if (shell->workspaces.anim_to != NULL)
751 finish_workspace_change_animation(shell,
752 shell->workspaces.anim_from,
753 shell->workspaces.anim_to);
754
755 if (workspace_is_empty(to) && workspace_is_empty(from)) {
756 shell->workspaces.current = index;
757 wl_list_insert(&from->layer.link, &to->layer.link);
758 wl_list_remove(&from->layer.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200759
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400760 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200761 }
762 else
763 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200764}
765
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200766static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400767noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100768 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500769{
770 grab->focus = NULL;
771}
772
773static void
Scott Moreau447013d2012-02-18 05:05:29 -0700774move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100775 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500776{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500777 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100778 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300779 struct shell_surface *shsurf = move->base.shsurf;
780 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100781 int dx = wl_fixed_to_int(pointer->x + move->dx);
782 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300783
784 if (!shsurf)
785 return;
786
787 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500788
Daniel Stone103db7f2012-05-08 17:17:55 +0100789 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200790 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -0400791
792 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500793}
794
795static void
Scott Moreau447013d2012-02-18 05:05:29 -0700796move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100797 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500798{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300799 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
800 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100801 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100802 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500803
Daniel Stone4dbadb12012-05-30 16:31:51 +0100804 if (pointer->button_count == 0 &&
805 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300806 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500807 free(grab);
808 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500809}
810
Scott Moreau447013d2012-02-18 05:05:29 -0700811static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500812 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500813 move_grab_motion,
814 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500815};
816
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -0400817static int
818surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
819{
820 struct weston_move_grab *move;
821
822 if (!shsurf)
823 return -1;
824
825 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
826 return 0;
827
828 move = malloc(sizeof *move);
829 if (!move)
830 return -1;
831
832 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
833 ws->seat.pointer->grab_x;
834 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
835 ws->seat.pointer->grab_y;
836
837 shell_grab_start(&move->base, &move_grab_interface, shsurf,
838 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
839
840 return 0;
841}
842
843static void
844shell_surface_move(struct wl_client *client, struct wl_resource *resource,
845 struct wl_resource *seat_resource, uint32_t serial)
846{
847 struct weston_seat *ws = seat_resource->data;
848 struct shell_surface *shsurf = resource->data;
849
850 if (ws->seat.pointer->button_count == 0 ||
851 ws->seat.pointer->grab_serial != serial ||
852 ws->seat.pointer->focus != &shsurf->surface->surface)
853 return;
854
855 if (surface_move(shsurf, ws) < 0)
856 wl_resource_post_no_memory(resource);
857}
858
859struct weston_resize_grab {
860 struct shell_grab base;
861 uint32_t edges;
862 int32_t width, height;
863};
864
865static void
866resize_grab_motion(struct wl_pointer_grab *grab,
867 uint32_t time, wl_fixed_t x, wl_fixed_t y)
868{
869 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
870 struct wl_pointer *pointer = grab->pointer;
871 struct shell_surface *shsurf = resize->base.shsurf;
872 int32_t width, height;
873 wl_fixed_t from_x, from_y;
874 wl_fixed_t to_x, to_y;
875
876 if (!shsurf)
877 return;
878
879 weston_surface_from_global_fixed(shsurf->surface,
880 pointer->grab_x, pointer->grab_y,
881 &from_x, &from_y);
882 weston_surface_from_global_fixed(shsurf->surface,
883 pointer->x, pointer->y, &to_x, &to_y);
884
885 width = resize->width;
886 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
887 width += wl_fixed_to_int(from_x - to_x);
888 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
889 width += wl_fixed_to_int(to_x - from_x);
890 }
891
892 height = resize->height;
893 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
894 height += wl_fixed_to_int(from_y - to_y);
895 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
896 height += wl_fixed_to_int(to_y - from_y);
897 }
898
899 shsurf->client->send_configure(shsurf->surface,
900 resize->edges, width, height);
901}
902
903static void
904send_configure(struct weston_surface *surface,
905 uint32_t edges, int32_t width, int32_t height)
906{
907 struct shell_surface *shsurf = get_shell_surface(surface);
908
909 wl_shell_surface_send_configure(&shsurf->resource,
910 edges, width, height);
911}
912
913static const struct weston_shell_client shell_client = {
914 send_configure
915};
916
917static void
918resize_grab_button(struct wl_pointer_grab *grab,
919 uint32_t time, uint32_t button, uint32_t state_w)
920{
921 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
922 struct wl_pointer *pointer = grab->pointer;
923 enum wl_pointer_button_state state = state_w;
924
925 if (pointer->button_count == 0 &&
926 state == WL_POINTER_BUTTON_STATE_RELEASED) {
927 shell_grab_end(&resize->base);
928 free(grab);
929 }
930}
931
932static const struct wl_pointer_grab_interface resize_grab_interface = {
933 noop_grab_focus,
934 resize_grab_motion,
935 resize_grab_button,
936};
937
938static int
939surface_resize(struct shell_surface *shsurf,
940 struct weston_seat *ws, uint32_t edges)
941{
942 struct weston_resize_grab *resize;
943
944 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
945 return 0;
946
947 if (edges == 0 || edges > 15 ||
948 (edges & 3) == 3 || (edges & 12) == 12)
949 return 0;
950
951 resize = malloc(sizeof *resize);
952 if (!resize)
953 return -1;
954
955 resize->edges = edges;
956 resize->width = shsurf->surface->geometry.width;
957 resize->height = shsurf->surface->geometry.height;
958
959 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
960 ws->seat.pointer, edges);
961
962 return 0;
963}
964
965static void
966shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
967 struct wl_resource *seat_resource, uint32_t serial,
968 uint32_t edges)
969{
970 struct weston_seat *ws = seat_resource->data;
971 struct shell_surface *shsurf = resource->data;
972
973 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
974 return;
975
976 if (ws->seat.pointer->button_count == 0 ||
977 ws->seat.pointer->grab_serial != serial ||
978 ws->seat.pointer->focus != &shsurf->surface->surface)
979 return;
980
981 if (surface_resize(shsurf, ws, edges) < 0)
982 wl_resource_post_no_memory(resource);
983}
984
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600985static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400986busy_cursor_grab_focus(struct wl_pointer_grab *base,
987 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600988{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400989 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600990
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400991 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300992 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400993 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600994 }
995}
996
997static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400998busy_cursor_grab_motion(struct wl_pointer_grab *grab,
999 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001000{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001001}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001002
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001003static void
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001004busy_cursor_grab_button(struct wl_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001005 uint32_t time, uint32_t button, uint32_t state)
1006{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001007 struct shell_grab *grab = (struct shell_grab *) base;
1008 struct shell_surface *shsurf;
1009 struct weston_surface *surface =
1010 (struct weston_surface *) grab->grab.pointer->current;
1011 struct weston_seat *seat =
1012 (struct weston_seat *) grab->grab.pointer->seat;
1013
1014 shsurf = get_shell_surface(surface);
1015 if (shsurf && button == BTN_LEFT && state) {
1016 activate(shsurf->shell, shsurf->surface, seat);
1017 surface_move(shsurf, seat);
1018 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001019}
1020
1021static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1022 busy_cursor_grab_focus,
1023 busy_cursor_grab_motion,
1024 busy_cursor_grab_button,
1025};
1026
1027static void
1028set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1029{
1030 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001031
1032 grab = malloc(sizeof *grab);
1033 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001034 return;
1035
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001036 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1037 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001038}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001039
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001040static void
1041end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1042{
1043 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1044
1045 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001046 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001047 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001048 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001049}
1050
Scott Moreau9521d5e2012-04-19 13:06:17 -06001051static void
1052ping_timer_destroy(struct shell_surface *shsurf)
1053{
1054 if (!shsurf || !shsurf->ping_timer)
1055 return;
1056
1057 if (shsurf->ping_timer->source)
1058 wl_event_source_remove(shsurf->ping_timer->source);
1059
1060 free(shsurf->ping_timer);
1061 shsurf->ping_timer = NULL;
1062}
1063
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001064static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001065ping_timeout_handler(void *data)
1066{
1067 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001068 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001069
Scott Moreau9521d5e2012-04-19 13:06:17 -06001070 /* Client is not responding */
1071 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001072
1073 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1074 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1075 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001076
1077 return 1;
1078}
1079
1080static void
1081ping_handler(struct weston_surface *surface, uint32_t serial)
1082{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001083 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001084 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001085 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001086
1087 if (!shsurf)
1088 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001089 if (!shsurf->resource.client)
1090 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001091
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001092 if (shsurf->surface == shsurf->shell->grab_surface)
1093 return;
1094
Scott Moreauff1db4a2012-04-17 19:06:18 -06001095 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001096 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001097 if (!shsurf->ping_timer)
1098 return;
1099
1100 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001101 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1102 shsurf->ping_timer->source =
1103 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1104 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1105
1106 wl_shell_surface_send_ping(&shsurf->resource, serial);
1107 }
1108}
1109
1110static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001111handle_pointer_focus(struct wl_listener *listener, void *data)
1112{
1113 struct wl_pointer *pointer = data;
1114 struct weston_surface *surface =
1115 (struct weston_surface *) pointer->focus;
1116 struct weston_compositor *compositor;
1117 struct shell_surface *shsurf;
1118 uint32_t serial;
1119
1120 if (!surface)
1121 return;
1122
1123 compositor = surface->compositor;
1124 shsurf = get_shell_surface(surface);
1125
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001126 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001127 set_busy_cursor(shsurf, pointer);
1128 } else {
1129 serial = wl_display_next_serial(compositor->wl_display);
1130 ping_handler(surface, serial);
1131 }
1132}
1133
1134static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001135create_pointer_focus_listener(struct weston_seat *seat)
1136{
1137 struct wl_listener *listener;
1138
1139 if (!seat->seat.pointer)
1140 return;
1141
1142 listener = malloc(sizeof *listener);
1143 listener->notify = handle_pointer_focus;
1144 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1145}
1146
1147static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001148shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1149 uint32_t serial)
1150{
1151 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001152 struct desktop_shell *shell = shsurf->shell;
1153 struct weston_seat *seat;
1154 struct weston_compositor *ec = shsurf->surface->compositor;
1155 struct wl_pointer *pointer;
1156 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001157
Scott Moreauff1db4a2012-04-17 19:06:18 -06001158 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001159 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001160 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001161 if (was_unresponsive) {
1162 /* Received pong from previously unresponsive client */
1163 wl_list_for_each(seat, &ec->seat_list, link) {
1164 pointer = seat->seat.pointer;
1165 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001166 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001167 pointer->current ==
1168 &shsurf->surface->surface)
1169 end_busy_cursor(shsurf, pointer);
1170 }
1171 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001172 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001173 }
1174}
1175
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001176static void
1177shell_surface_set_title(struct wl_client *client,
1178 struct wl_resource *resource, const char *title)
1179{
1180 struct shell_surface *shsurf = resource->data;
1181
1182 free(shsurf->title);
1183 shsurf->title = strdup(title);
1184}
1185
1186static void
1187shell_surface_set_class(struct wl_client *client,
1188 struct wl_resource *resource, const char *class)
1189{
1190 struct shell_surface *shsurf = resource->data;
1191
1192 free(shsurf->class);
1193 shsurf->class = strdup(class);
1194}
1195
Juan Zhao96879df2012-02-07 08:45:41 +08001196static struct weston_output *
1197get_default_output(struct weston_compositor *compositor)
1198{
1199 return container_of(compositor->output_list.next,
1200 struct weston_output, link);
1201}
1202
Alex Wu4539b082012-03-01 12:57:46 +08001203static void
1204shell_unset_fullscreen(struct shell_surface *shsurf)
1205{
1206 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001207 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1208 shell_surface_is_top_fullscreen(shsurf)) {
1209 weston_output_switch_mode(shsurf->fullscreen_output,
1210 shsurf->fullscreen_output->origin);
1211 }
Alex Wu4539b082012-03-01 12:57:46 +08001212 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1213 shsurf->fullscreen.framerate = 0;
1214 wl_list_remove(&shsurf->fullscreen.transform.link);
1215 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001216 if (shsurf->fullscreen.black_surface)
1217 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001218 shsurf->fullscreen.black_surface = NULL;
1219 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001220 weston_surface_set_position(shsurf->surface,
1221 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001222 if (shsurf->saved_rotation_valid) {
1223 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1224 &shsurf->rotation.transform.link);
1225 shsurf->saved_rotation_valid = false;
1226 }
Alex Wu4539b082012-03-01 12:57:46 +08001227}
1228
Pekka Paalanen98262232011-12-01 10:42:22 +02001229static int
1230reset_shell_surface_type(struct shell_surface *surface)
1231{
1232 switch (surface->type) {
1233 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001234 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001235 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001236 case SHELL_SURFACE_MAXIMIZED:
1237 surface->output = get_default_output(surface->surface->compositor);
1238 weston_surface_set_position(surface->surface,
1239 surface->saved_x,
1240 surface->saved_y);
1241 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001242 case SHELL_SURFACE_NONE:
1243 case SHELL_SURFACE_TOPLEVEL:
1244 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001245 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001246 break;
1247 }
1248
1249 surface->type = SHELL_SURFACE_NONE;
1250 return 0;
1251}
1252
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001253static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001254set_surface_type(struct shell_surface *shsurf)
1255{
1256 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001257 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001258
1259 reset_shell_surface_type(shsurf);
1260
1261 shsurf->type = shsurf->next_type;
1262 shsurf->next_type = SHELL_SURFACE_NONE;
1263
1264 switch (shsurf->type) {
1265 case SHELL_SURFACE_TOPLEVEL:
1266 break;
1267 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001268 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001269 pes->geometry.x + shsurf->transient.x,
1270 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001271 break;
1272
1273 case SHELL_SURFACE_MAXIMIZED:
1274 shsurf->saved_x = surface->geometry.x;
1275 shsurf->saved_y = surface->geometry.y;
1276 shsurf->saved_position_valid = true;
1277 break;
1278
1279 case SHELL_SURFACE_FULLSCREEN:
1280 shsurf->saved_x = surface->geometry.x;
1281 shsurf->saved_y = surface->geometry.y;
1282 shsurf->saved_position_valid = true;
1283
1284 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1285 wl_list_remove(&shsurf->rotation.transform.link);
1286 wl_list_init(&shsurf->rotation.transform.link);
1287 shsurf->surface->geometry.dirty = 1;
1288 shsurf->saved_rotation_valid = true;
1289 }
1290 break;
1291
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001292 default:
1293 break;
1294 }
1295}
1296
1297static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001298set_toplevel(struct shell_surface *shsurf)
1299{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001300 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001301}
1302
1303static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001304shell_surface_set_toplevel(struct wl_client *client,
1305 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001306{
Pekka Paalanen98262232011-12-01 10:42:22 +02001307 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001308
Tiago Vignattibc052c92012-04-19 16:18:18 +03001309 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001310}
1311
1312static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001313set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001314 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001315{
1316 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001317 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001318 shsurf->transient.x = x;
1319 shsurf->transient.y = y;
1320 shsurf->transient.flags = flags;
1321 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1322}
1323
1324static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001325shell_surface_set_transient(struct wl_client *client,
1326 struct wl_resource *resource,
1327 struct wl_resource *parent_resource,
1328 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001329{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001330 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001331 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001332
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001333 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001334}
1335
Tiago Vignattibe143262012-04-16 17:31:41 +03001336static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001337shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001338{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001339 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001340}
1341
1342static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001343get_output_panel_height(struct desktop_shell *shell,
1344 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001345{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001346 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001347 int panel_height = 0;
1348
1349 if (!output)
1350 return 0;
1351
Juan Zhao4ab94682012-07-09 22:24:09 -07001352 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001353 if (surface->output == output) {
1354 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001355 break;
1356 }
1357 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001358
Juan Zhao96879df2012-02-07 08:45:41 +08001359 return panel_height;
1360}
1361
1362static void
1363shell_surface_set_maximized(struct wl_client *client,
1364 struct wl_resource *resource,
1365 struct wl_resource *output_resource )
1366{
1367 struct shell_surface *shsurf = resource->data;
1368 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001369 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001370 uint32_t edges = 0, panel_height = 0;
1371
1372 /* get the default output, if the client set it as NULL
1373 check whether the ouput is available */
1374 if (output_resource)
1375 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001376 else if (es->output)
1377 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001378 else
1379 shsurf->output = get_default_output(es->compositor);
1380
Tiago Vignattibe143262012-04-16 17:31:41 +03001381 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001382 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001383 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001384
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001385 shsurf->client->send_configure(shsurf->surface, edges,
Rob Bradford31b68622012-07-02 19:00:19 +01001386 shsurf->output->current->width,
1387 shsurf->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001388
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001389 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001390}
1391
Alex Wu21858432012-04-01 20:13:08 +08001392static void
1393black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1394
Alex Wu4539b082012-03-01 12:57:46 +08001395static struct weston_surface *
1396create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001397 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001398 GLfloat x, GLfloat y, int w, int h)
1399{
1400 struct weston_surface *surface = NULL;
1401
1402 surface = weston_surface_create(ec);
1403 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001404 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001405 return NULL;
1406 }
1407
Alex Wu21858432012-04-01 20:13:08 +08001408 surface->configure = black_surface_configure;
1409 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001410 weston_surface_configure(surface, x, y, w, h);
1411 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001412 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
1413
Alex Wu4539b082012-03-01 12:57:46 +08001414 return surface;
1415}
1416
1417/* Create black surface and append it to the associated fullscreen surface.
1418 * Handle size dismatch and positioning according to the method. */
1419static void
1420shell_configure_fullscreen(struct shell_surface *shsurf)
1421{
1422 struct weston_output *output = shsurf->fullscreen_output;
1423 struct weston_surface *surface = shsurf->surface;
1424 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001425 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001426
Alex Wu4539b082012-03-01 12:57:46 +08001427 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001428 shsurf->fullscreen.black_surface =
1429 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001430 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001431 output->x, output->y,
1432 output->current->width,
1433 output->current->height);
1434
1435 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1436 wl_list_insert(&surface->layer_link,
1437 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001438 shsurf->fullscreen.black_surface->output = output;
1439
1440 switch (shsurf->fullscreen.type) {
1441 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001442 if (surface->buffer)
1443 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001444 break;
1445 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1446 matrix = &shsurf->fullscreen.transform.matrix;
1447 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001448
1449 output_aspect = (float) output->current->width /
1450 (float) output->current->height;
1451 surface_aspect = (float) surface->geometry.width /
1452 (float) surface->geometry.height;
1453 if (output_aspect < surface_aspect)
1454 scale = (float) output->current->width /
1455 (float) surface->geometry.width;
1456 else
1457 scale = (float) output->current->height /
1458 (float) surface->geometry.height;
1459
Alex Wu4539b082012-03-01 12:57:46 +08001460 weston_matrix_scale(matrix, scale, scale, 1);
1461 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001462 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001463 &shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001464 x = output->x + (output->current->width - surface->geometry.width * scale) / 2;
1465 y = output->y + (output->current->height - surface->geometry.height * scale) / 2;
1466 weston_surface_set_position(surface, x, y);
1467
Alex Wu4539b082012-03-01 12:57:46 +08001468 break;
1469 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001470 if (shell_surface_is_top_fullscreen(shsurf)) {
1471 struct weston_mode mode = {0,
1472 surface->geometry.width,
1473 surface->geometry.height,
1474 shsurf->fullscreen.framerate};
1475
1476 if (weston_output_switch_mode(output, &mode) == 0) {
1477 weston_surface_configure(shsurf->fullscreen.black_surface,
1478 output->x, output->y,
1479 output->current->width,
1480 output->current->height);
1481 weston_surface_set_position(surface, output->x, output->y);
1482 break;
1483 }
1484 }
Alex Wu4539b082012-03-01 12:57:46 +08001485 break;
1486 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1487 break;
1488 default:
1489 break;
1490 }
1491}
1492
1493/* make the fullscreen and black surface at the top */
1494static void
1495shell_stack_fullscreen(struct shell_surface *shsurf)
1496{
Alex Wubd3354b2012-04-17 17:20:49 +08001497 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001498 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001499 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001500
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001501 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001502 wl_list_insert(&shell->fullscreen_layer.surface_list,
1503 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001504 weston_surface_damage(surface);
1505
1506 if (!shsurf->fullscreen.black_surface)
1507 shsurf->fullscreen.black_surface =
1508 create_black_surface(surface->compositor,
1509 surface,
1510 output->x, output->y,
1511 output->current->width,
1512 output->current->height);
1513
1514 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001515 wl_list_insert(&surface->layer_link,
1516 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001517 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001518}
1519
1520static void
1521shell_map_fullscreen(struct shell_surface *shsurf)
1522{
Alex Wu4539b082012-03-01 12:57:46 +08001523 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001524 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001525}
1526
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001527static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001528shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001529 struct wl_resource *resource,
1530 uint32_t method,
1531 uint32_t framerate,
1532 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001533{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001534 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001535 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001536
1537 if (output_resource)
1538 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001539 else if (es->output)
1540 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001541 else
1542 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001543
Alex Wu4539b082012-03-01 12:57:46 +08001544 shsurf->fullscreen_output = shsurf->output;
1545 shsurf->fullscreen.type = method;
1546 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001547 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001548
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001549 shsurf->client->send_configure(shsurf->surface, 0,
1550 shsurf->output->current->width,
1551 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001552}
1553
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001554static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001555popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001556 struct wl_surface *surface,
1557 wl_fixed_t x,
1558 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001559{
Daniel Stone37816df2012-05-16 18:45:18 +01001560 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001561 struct shell_surface *priv =
1562 container_of(grab, struct shell_surface, popup.grab);
1563 struct wl_client *client = priv->surface->surface.resource.client;
1564
Pekka Paalanencb108432012-01-19 16:25:40 +02001565 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001566 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001567 grab->focus = surface;
1568 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001569 wl_pointer_set_focus(pointer, NULL,
1570 wl_fixed_from_int(0),
1571 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001572 grab->focus = NULL;
1573 }
1574}
1575
1576static void
Scott Moreau447013d2012-02-18 05:05:29 -07001577popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001578 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001579{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001580 struct wl_resource *resource;
1581
Daniel Stone37816df2012-05-16 18:45:18 +01001582 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001583 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001584 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001585}
1586
1587static void
Scott Moreau447013d2012-02-18 05:05:29 -07001588popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001589 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001590{
1591 struct wl_resource *resource;
1592 struct shell_surface *shsurf =
1593 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001594 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001595 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001596 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001597
Daniel Stone37816df2012-05-16 18:45:18 +01001598 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001599 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001600 display = wl_client_get_display(resource->client);
1601 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001602 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001603 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001604 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001605 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001606 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001607 wl_pointer_end_grab(grab->pointer);
1608 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001609 }
1610
Daniel Stone4dbadb12012-05-30 16:31:51 +01001611 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001612 shsurf->popup.initial_up = 1;
1613}
1614
Scott Moreau447013d2012-02-18 05:05:29 -07001615static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001616 popup_grab_focus,
1617 popup_grab_motion,
1618 popup_grab_button,
1619};
1620
1621static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001622shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001623{
Daniel Stone37816df2012-05-16 18:45:18 +01001624 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001625 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001626 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001627
1628 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001629 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001630
Pekka Paalanen938269a2012-02-07 14:19:01 +02001631 weston_surface_update_transform(parent);
1632 if (parent->transform.enabled) {
1633 shsurf->popup.parent_transform.matrix =
1634 parent->transform.matrix;
1635 } else {
1636 /* construct x, y translation matrix */
1637 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1638 shsurf->popup.parent_transform.matrix.d[12] =
1639 parent->geometry.x;
1640 shsurf->popup.parent_transform.matrix.d[13] =
1641 parent->geometry.y;
1642 }
1643 wl_list_insert(es->geometry.transformation_list.prev,
1644 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001645 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001646
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001647 shsurf->popup.initial_up = 0;
1648
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001649 /* We don't require the grab to still be active, but if another
1650 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001651 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1652 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001653 } else {
1654 wl_shell_surface_send_popup_done(&shsurf->resource);
1655 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001656}
1657
1658static void
1659shell_surface_set_popup(struct wl_client *client,
1660 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001661 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001662 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001663 struct wl_resource *parent_resource,
1664 int32_t x, int32_t y, uint32_t flags)
1665{
1666 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001667
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001668 shsurf->type = SHELL_SURFACE_POPUP;
1669 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001670 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001671 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001672 shsurf->popup.x = x;
1673 shsurf->popup.y = y;
1674}
1675
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001676static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001677 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001678 shell_surface_move,
1679 shell_surface_resize,
1680 shell_surface_set_toplevel,
1681 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001682 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001683 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001684 shell_surface_set_maximized,
1685 shell_surface_set_title,
1686 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001687};
1688
1689static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001690destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001691{
Daniel Stone37816df2012-05-16 18:45:18 +01001692 if (shsurf->popup.grab.pointer)
1693 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001694
Alex Wubd3354b2012-04-17 17:20:49 +08001695 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1696 shell_surface_is_top_fullscreen(shsurf)) {
1697 weston_output_switch_mode(shsurf->fullscreen_output,
1698 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001699 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001700
Alex Wuaa08e2d2012-03-05 11:01:40 +08001701 if (shsurf->fullscreen.black_surface)
1702 weston_surface_destroy(shsurf->fullscreen.black_surface);
1703
Alex Wubd3354b2012-04-17 17:20:49 +08001704 /* As destroy_resource() use wl_list_for_each_safe(),
1705 * we can always remove the listener.
1706 */
1707 wl_list_remove(&shsurf->surface_destroy_listener.link);
1708 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001709 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001710
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001711 wl_list_remove(&shsurf->link);
1712 free(shsurf);
1713}
1714
1715static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001716shell_destroy_shell_surface(struct wl_resource *resource)
1717{
1718 struct shell_surface *shsurf = resource->data;
1719
1720 destroy_shell_surface(shsurf);
1721}
1722
1723static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001724shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001725{
1726 struct shell_surface *shsurf = container_of(listener,
1727 struct shell_surface,
1728 surface_destroy_listener);
1729
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001730 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001731 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001732 } else {
1733 wl_signal_emit(&shsurf->resource.destroy_signal,
1734 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001735 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001736 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001737}
1738
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001739static void
1740shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1741
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001742static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001743get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001744{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001745 if (surface->configure == shell_surface_configure)
1746 return surface->private;
1747 else
1748 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001749}
1750
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001751static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001752create_shell_surface(void *shell, struct weston_surface *surface,
1753 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001754{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001755 struct shell_surface *shsurf;
1756
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001757 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001758 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001759 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001760 }
1761
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001762 shsurf = calloc(1, sizeof *shsurf);
1763 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001764 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001765 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001766 }
1767
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001768 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001769 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001770
Tiago Vignattibc052c92012-04-19 16:18:18 +03001771 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001772 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001773 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001774 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001775 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001776 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1777 shsurf->fullscreen.framerate = 0;
1778 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001779 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001780 wl_list_init(&shsurf->fullscreen.transform.link);
1781
Tiago Vignattibc052c92012-04-19 16:18:18 +03001782 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001783 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1784 wl_signal_add(&surface->surface.resource.destroy_signal,
1785 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001786
1787 /* init link so its safe to always remove it in destroy_shell_surface */
1788 wl_list_init(&shsurf->link);
1789
Pekka Paalanen460099f2012-01-20 16:48:25 +02001790 /* empty when not in use */
1791 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001792 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001793
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001794 wl_list_init(&shsurf->workspace_transform.link);
1795
Pekka Paalanen98262232011-12-01 10:42:22 +02001796 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001797 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001798
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001799 shsurf->client = client;
1800
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001801 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001802}
1803
1804static void
1805shell_get_shell_surface(struct wl_client *client,
1806 struct wl_resource *resource,
1807 uint32_t id,
1808 struct wl_resource *surface_resource)
1809{
1810 struct weston_surface *surface = surface_resource->data;
1811 struct desktop_shell *shell = resource->data;
1812 struct shell_surface *shsurf;
1813
1814 if (get_shell_surface(surface)) {
1815 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001816 WL_DISPLAY_ERROR_INVALID_OBJECT,
1817 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001818 return;
1819 }
1820
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001821 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001822 if (!shsurf) {
1823 wl_resource_post_error(surface_resource,
1824 WL_DISPLAY_ERROR_INVALID_OBJECT,
1825 "surface->configure already set");
1826 return;
1827 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001828
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001829 shsurf->resource.destroy = shell_destroy_shell_surface;
1830 shsurf->resource.object.id = id;
1831 shsurf->resource.object.interface = &wl_shell_surface_interface;
1832 shsurf->resource.object.implementation =
1833 (void (**)(void)) &shell_surface_implementation;
1834 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001835
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001836 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001837}
1838
1839static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001840 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001841};
1842
Kristian Høgsberg07937562011-04-12 17:25:42 -04001843static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001844handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001845{
1846 proc->pid = 0;
1847}
1848
1849static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001850launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001851{
1852 if (shell->screensaver.binding)
1853 return;
1854
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001855 if (!shell->screensaver.path)
1856 return;
1857
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001858 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001859 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001860 return;
1861 }
1862
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001863 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001864 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001865 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001866 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001867}
1868
1869static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001870terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001871{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001872 if (shell->screensaver.process.pid == 0)
1873 return;
1874
1875 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001876}
1877
1878static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001879configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001880{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001881 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001882
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001883 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1884 if (s->output == es->output && s != es) {
1885 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001886 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001887 }
1888 }
1889
1890 weston_surface_configure(es, es->output->x, es->output->y,
1891 es->buffer->width, es->buffer->height);
1892
1893 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001894 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001895 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001896 }
1897}
1898
1899static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001900background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1901{
1902 struct desktop_shell *shell = es->private;
1903
1904 configure_static_surface(es, &shell->background_layer);
1905}
1906
1907static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001908desktop_shell_set_background(struct wl_client *client,
1909 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001910 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001911 struct wl_resource *surface_resource)
1912{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001913 struct desktop_shell *shell = resource->data;
1914 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001915
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001916 if (surface->configure) {
1917 wl_resource_post_error(surface_resource,
1918 WL_DISPLAY_ERROR_INVALID_OBJECT,
1919 "surface role already assigned");
1920 return;
1921 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001922
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001923 surface->configure = background_configure;
1924 surface->private = shell;
1925 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001926 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001927 surface_resource,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001928 surface->output->current->width,
1929 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001930}
1931
1932static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001933panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1934{
1935 struct desktop_shell *shell = es->private;
1936
1937 configure_static_surface(es, &shell->panel_layer);
1938}
1939
1940static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001941desktop_shell_set_panel(struct wl_client *client,
1942 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001943 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001944 struct wl_resource *surface_resource)
1945{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001946 struct desktop_shell *shell = resource->data;
1947 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001948
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001949 if (surface->configure) {
1950 wl_resource_post_error(surface_resource,
1951 WL_DISPLAY_ERROR_INVALID_OBJECT,
1952 "surface role already assigned");
1953 return;
1954 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001955
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001956 surface->configure = panel_configure;
1957 surface->private = shell;
1958 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001959 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001960 surface_resource,
1961 surface->output->current->width,
1962 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001963}
1964
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001965static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001966lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1967{
1968 struct desktop_shell *shell = surface->private;
1969
1970 center_on_output(surface, get_default_output(shell->compositor));
1971
1972 if (!weston_surface_is_mapped(surface)) {
1973 wl_list_insert(&shell->lock_layer.surface_list,
1974 &surface->layer_link);
1975 weston_surface_assign_output(surface);
1976 weston_compositor_wake(shell->compositor);
1977 }
1978}
1979
1980static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001981handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001982{
Tiago Vignattibe143262012-04-16 17:31:41 +03001983 struct desktop_shell *shell =
1984 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001985
Martin Minarik6d118362012-06-07 18:01:59 +02001986 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001987 shell->lock_surface = NULL;
1988}
1989
1990static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001991desktop_shell_set_lock_surface(struct wl_client *client,
1992 struct wl_resource *resource,
1993 struct wl_resource *surface_resource)
1994{
Tiago Vignattibe143262012-04-16 17:31:41 +03001995 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001996 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001997
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001998 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001999
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002000 if (!shell->locked)
2001 return;
2002
Pekka Paalanen98262232011-12-01 10:42:22 +02002003 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002004
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002005 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2006 wl_signal_add(&surface_resource->destroy_signal,
2007 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002008
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002009 surface->configure = lock_surface_configure;
2010 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002011}
2012
2013static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002014resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002015{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002016 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002017 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002018
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002019 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
2020 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002021
2022 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002023
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002024 wl_list_remove(&shell->lock_layer.link);
2025 wl_list_insert(&shell->compositor->cursor_layer.link,
2026 &shell->fullscreen_layer.link);
2027 wl_list_insert(&shell->fullscreen_layer.link,
2028 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002029 if (shell->showing_input_panels) {
2030 wl_list_insert(&shell->panel_layer.link,
2031 &shell->input_panel_layer.link);
2032 wl_list_insert(&shell->input_panel_layer.link,
2033 &ws->layer.link);
2034 } else {
2035 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2036 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002037
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002038 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002039
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002040 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002041 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002042 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002043 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002044}
2045
2046static void
2047desktop_shell_unlock(struct wl_client *client,
2048 struct wl_resource *resource)
2049{
Tiago Vignattibe143262012-04-16 17:31:41 +03002050 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002051
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002052 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002053
2054 if (shell->locked)
2055 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002056}
2057
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002058static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002059desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002060 struct wl_resource *resource,
2061 struct wl_resource *surface_resource)
2062{
2063 struct desktop_shell *shell = resource->data;
2064
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002065 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002066}
2067
Kristian Høgsberg75840622011-09-06 13:48:16 -04002068static const struct desktop_shell_interface desktop_shell_implementation = {
2069 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002070 desktop_shell_set_panel,
2071 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002072 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002073 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002074};
2075
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002076static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002077get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002078{
2079 struct shell_surface *shsurf;
2080
2081 shsurf = get_shell_surface(surface);
2082 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002083 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002084 return shsurf->type;
2085}
2086
Kristian Høgsberg75840622011-09-06 13:48:16 -04002087static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002088move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002089{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002090 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002091 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002092 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002093
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002094 if (surface == NULL)
2095 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002096
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002097 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002098 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002099 return;
2100
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002101 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002102}
2103
2104static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002105resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002106{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002107 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002108 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002109 uint32_t edges = 0;
2110 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002111 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002112
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002113 if (surface == NULL)
2114 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002115
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002116 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002117 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002118 return;
2119
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002120 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002121 wl_fixed_to_int(seat->pointer->grab_x),
2122 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002123 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002124
Pekka Paalanen60921e52012-01-25 15:55:43 +02002125 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002126 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002127 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002128 edges |= 0;
2129 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002130 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002131
Pekka Paalanen60921e52012-01-25 15:55:43 +02002132 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002133 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002134 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002135 edges |= 0;
2136 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002137 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002138
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002139 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002140}
2141
2142static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002143surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002144 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002145{
Scott Moreau02709af2012-05-22 01:54:10 -06002146 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002147 struct shell_surface *shsurf;
2148 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002149 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002150
2151 if (surface == NULL)
2152 return;
2153
2154 shsurf = get_shell_surface(surface);
2155 if (!shsurf)
2156 return;
2157
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002158 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002159
Scott Moreau02709af2012-05-22 01:54:10 -06002160 if (surface->alpha > 1.0)
2161 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002162 if (surface->alpha < step)
2163 surface->alpha = step;
2164
2165 surface->geometry.dirty = 1;
2166 weston_surface_damage(surface);
2167}
2168
2169static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002170do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002171 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002172{
Daniel Stone37816df2012-05-16 18:45:18 +01002173 struct weston_seat *ws = (struct weston_seat *) seat;
2174 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002175 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002176 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002177
2178 wl_list_for_each(output, &compositor->output_list, link) {
2179 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002180 wl_fixed_to_double(seat->pointer->x),
2181 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002182 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002183 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002184 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002185 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002186 increment = -output->zoom.increment;
2187 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002188 increment = output->zoom.increment *
2189 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002190 else
2191 increment = 0;
2192
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002193 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002194
Scott Moreaue6603982012-06-11 13:07:51 -06002195 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002196 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002197 else if (output->zoom.level > output->zoom.max_level)
2198 output->zoom.level = output->zoom.max_level;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002199 else {
Scott Moreaue6603982012-06-11 13:07:51 -06002200 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002201 output->disable_planes++;
2202 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002203
Scott Moreaue6603982012-06-11 13:07:51 -06002204 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002205
Scott Moreau8dacaab2012-06-17 18:10:58 -06002206 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002207 }
2208 }
2209}
2210
Scott Moreauccbf29d2012-02-22 14:21:41 -07002211static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002212zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002213 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002214{
2215 do_zoom(seat, time, 0, axis, value);
2216}
2217
2218static void
2219zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2220 void *data)
2221{
2222 do_zoom(seat, time, key, 0, 0);
2223}
2224
2225static void
2226terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2227 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002228{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002229 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002230
Daniel Stone325fc2d2012-05-30 16:31:58 +01002231 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002232}
2233
2234static void
Scott Moreau447013d2012-02-18 05:05:29 -07002235rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002236 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002237{
2238 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002239 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002240 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002241 struct shell_surface *shsurf = rotate->base.shsurf;
2242 struct weston_surface *surface;
2243 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2244
2245 if (!shsurf)
2246 return;
2247
2248 surface = shsurf->surface;
2249
2250 cx = 0.5f * surface->geometry.width;
2251 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002252
Daniel Stone37816df2012-05-16 18:45:18 +01002253 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2254 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002255 r = sqrtf(dx * dx + dy * dy);
2256
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002257 wl_list_remove(&shsurf->rotation.transform.link);
2258 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002259
2260 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002261 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002262 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002263
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002264 weston_matrix_init(&rotate->rotation);
2265 rotate->rotation.d[0] = dx / r;
2266 rotate->rotation.d[4] = -dy / r;
2267 rotate->rotation.d[1] = -rotate->rotation.d[4];
2268 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002269
2270 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002271 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002272 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002273 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002274 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002275
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002276 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002277 &shsurf->surface->geometry.transformation_list,
2278 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002279 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002280 wl_list_init(&shsurf->rotation.transform.link);
2281 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002282 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002283 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002284
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002285 /* We need to adjust the position of the surface
2286 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002287 cposx = surface->geometry.x + cx;
2288 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002289 dposx = rotate->center.x - cposx;
2290 dposy = rotate->center.y - cposy;
2291 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002292 weston_surface_set_position(surface,
2293 surface->geometry.x + dposx,
2294 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002295 }
2296
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002297 /* Repaint implies weston_surface_update_transform(), which
2298 * lazily applies the damage due to rotation update.
2299 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002300 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002301}
2302
2303static void
Scott Moreau447013d2012-02-18 05:05:29 -07002304rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002305 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002306{
2307 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002308 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002309 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002310 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002311 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002312
Daniel Stone4dbadb12012-05-30 16:31:51 +01002313 if (pointer->button_count == 0 &&
2314 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002315 if (shsurf)
2316 weston_matrix_multiply(&shsurf->rotation.rotation,
2317 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002318 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002319 free(rotate);
2320 }
2321}
2322
Scott Moreau447013d2012-02-18 05:05:29 -07002323static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002324 noop_grab_focus,
2325 rotate_grab_motion,
2326 rotate_grab_button,
2327};
2328
2329static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002330rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002331 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002332{
2333 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002334 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002335 struct shell_surface *surface;
2336 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002337 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002338 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002339
2340 if (base_surface == NULL)
2341 return;
2342
2343 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002344 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002345 return;
2346
Pekka Paalanen460099f2012-01-20 16:48:25 +02002347 rotate = malloc(sizeof *rotate);
2348 if (!rotate)
2349 return;
2350
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002351 weston_surface_to_global_float(surface->surface,
2352 surface->surface->geometry.width / 2,
2353 surface->surface->geometry.height / 2,
2354 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002355
Daniel Stone37816df2012-05-16 18:45:18 +01002356 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2357 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002358 r = sqrtf(dx * dx + dy * dy);
2359 if (r > 20.0f) {
2360 struct weston_matrix inverse;
2361
2362 weston_matrix_init(&inverse);
2363 inverse.d[0] = dx / r;
2364 inverse.d[4] = dy / r;
2365 inverse.d[1] = -inverse.d[4];
2366 inverse.d[5] = inverse.d[0];
2367 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002368
2369 weston_matrix_init(&rotate->rotation);
2370 rotate->rotation.d[0] = dx / r;
2371 rotate->rotation.d[4] = -dy / r;
2372 rotate->rotation.d[1] = -rotate->rotation.d[4];
2373 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002374 } else {
2375 weston_matrix_init(&surface->rotation.rotation);
2376 weston_matrix_init(&rotate->rotation);
2377 }
2378
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002379 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2380 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002381}
2382
2383static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002384lower_fullscreen_layer(struct desktop_shell *shell)
2385{
2386 struct workspace *ws;
2387 struct weston_surface *surface, *prev;
2388
2389 ws = get_current_workspace(shell);
2390 wl_list_for_each_reverse_safe(surface, prev,
2391 &shell->fullscreen_layer.surface_list,
2392 layer_link)
2393 weston_surface_restack(surface, &ws->layer.surface_list);
2394}
2395
2396static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002397activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002398 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002399{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002400 struct workspace *ws = get_current_workspace(shell);
2401 struct focus_state *state;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002402
Daniel Stone37816df2012-05-16 18:45:18 +01002403 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002404
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002405 wl_list_for_each(state, &ws->focus_list, link)
2406 if (state->seat == seat)
2407 break;
2408
2409 if (&state->link == &ws->focus_list) {
2410 state = focus_state_create(seat, ws);
2411 if (state == NULL)
2412 return;
2413 }
2414
2415 state->keyboard_focus = es;
2416 wl_list_remove(&state->surface_destroy_listener.link);
2417 wl_signal_add(&es->surface.resource.destroy_signal,
2418 &state->surface_destroy_listener);
2419
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002420 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002421 case SHELL_SURFACE_FULLSCREEN:
2422 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002423 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002424 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002425 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002426 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002427 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002428 lower_fullscreen_layer(shell);
2429 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002430 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002431 }
2432}
2433
Alex Wu21858432012-04-01 20:13:08 +08002434/* no-op func for checking black surface */
2435static void
2436black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2437{
2438}
2439
2440static bool
2441is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2442{
2443 if (es->configure == black_surface_configure) {
2444 if (fs_surface)
2445 *fs_surface = (struct weston_surface *)es->private;
2446 return true;
2447 }
2448 return false;
2449}
2450
Kristian Høgsberg75840622011-09-06 13:48:16 -04002451static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002452click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002453 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002454{
Daniel Stone37816df2012-05-16 18:45:18 +01002455 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002456 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002457 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002458 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002459
Daniel Stone37816df2012-05-16 18:45:18 +01002460 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002461 if (!focus)
2462 return;
2463
Alex Wu21858432012-04-01 20:13:08 +08002464 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002465 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002466
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002467 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2468 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002469
Daniel Stone325fc2d2012-05-30 16:31:58 +01002470 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002471 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002472}
2473
2474static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002475lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002476{
Tiago Vignattibe143262012-04-16 17:31:41 +03002477 struct desktop_shell *shell =
2478 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002479 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002480 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002481
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002482 if (shell->locked) {
2483 wl_list_for_each(output, &shell->compositor->output_list, link)
2484 /* TODO: find a way to jump to other DPMS levels */
2485 if (output->set_dpms)
2486 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002487 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002488 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002489
2490 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002491
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002492 /* Hide all surfaces by removing the fullscreen, panel and
2493 * toplevel layers. This way nothing else can show or receive
2494 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002495
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002496 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002497 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002498 if (shell->showing_input_panels)
2499 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002500 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002501 wl_list_insert(&shell->compositor->cursor_layer.link,
2502 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002503
Pekka Paalanen77346a62011-11-30 16:26:35 +02002504 launch_screensaver(shell);
2505
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002506 /* TODO: disable bindings that should not work while locked. */
2507
2508 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002509}
2510
2511static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002512unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002513{
Tiago Vignattibe143262012-04-16 17:31:41 +03002514 struct desktop_shell *shell =
2515 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002516
Pekka Paalanend81c2162011-11-16 13:47:34 +02002517 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002518 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002519 return;
2520 }
2521
2522 /* If desktop-shell client has gone away, unlock immediately. */
2523 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002524 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002525 return;
2526 }
2527
2528 if (shell->prepare_event_sent)
2529 return;
2530
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002531 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002532 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002533}
2534
2535static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002536show_input_panels(struct wl_listener *listener, void *data)
2537{
2538 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002539 container_of(listener, struct desktop_shell,
2540 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002541 struct input_panel_surface *surface, *next;
2542 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002543
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002544 shell->showing_input_panels = true;
2545
2546 wl_list_insert(&shell->panel_layer.link,
2547 &shell->input_panel_layer.link);
2548
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002549 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002550 &shell->input_panel.surfaces, link) {
2551 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002552 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002553 &ws->layer_link);
2554 weston_surface_assign_output(ws);
2555 weston_surface_damage(ws);
2556 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002557 }
2558}
2559
2560static void
2561hide_input_panels(struct wl_listener *listener, void *data)
2562{
2563 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002564 container_of(listener, struct desktop_shell,
2565 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002566 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002567
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002568 shell->showing_input_panels = false;
2569
2570 wl_list_remove(&shell->input_panel_layer.link);
2571
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002572 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002573 &shell->input_panel_layer.surface_list, layer_link)
2574 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002575}
2576
2577static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002578center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002579{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002580 struct weston_mode *mode = output->current;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002581 GLfloat x = (mode->width - surface->buffer->width) / 2;
2582 GLfloat y = (mode->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002583
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002584 weston_surface_configure(surface, output->x + x, output->y + y,
2585 surface->buffer->width,
2586 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002587}
2588
2589static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002590map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002591 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002592{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002593 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002594 struct shell_surface *shsurf = get_shell_surface(surface);
2595 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002596 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002597 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002598 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002599 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002600
Pekka Paalanen60921e52012-01-25 15:55:43 +02002601 surface->geometry.width = width;
2602 surface->geometry.height = height;
2603 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002604
2605 /* initial positioning, see also configure() */
2606 switch (surface_type) {
2607 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002608 weston_surface_set_position(surface, 10 + random() % 400,
2609 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002610 break;
Alex Wu4539b082012-03-01 12:57:46 +08002611 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002612 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002613 shell_map_fullscreen(shsurf);
2614 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002615 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002616 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002617 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002618 weston_surface_set_position(surface, shsurf->output->x,
2619 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002620 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002621 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002622 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002623 case SHELL_SURFACE_NONE:
2624 weston_surface_set_position(surface,
2625 surface->geometry.x + sx,
2626 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002627 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002628 default:
2629 ;
2630 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002631
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002632 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002633 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002634 case SHELL_SURFACE_POPUP:
2635 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002636 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002637 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2638 break;
Alex Wu4539b082012-03-01 12:57:46 +08002639 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002640 case SHELL_SURFACE_NONE:
2641 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002642 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002643 ws = get_current_workspace(shell);
2644 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002645 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002646 }
2647
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002648 if (surface_type != SHELL_SURFACE_NONE) {
2649 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002650 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2651 surface->output = shsurf->output;
2652 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002653
Juan Zhao7bb92f02011-12-15 11:31:51 -05002654 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002655 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002656 if (shsurf->transient.flags ==
2657 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2658 break;
2659 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002660 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002661 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002662 if (!shell->locked) {
2663 wl_list_for_each(seat, &compositor->seat_list, link)
2664 activate(shell, surface, seat);
2665 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002666 break;
2667 default:
2668 break;
2669 }
2670
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002671 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002672 {
2673 switch (shell->win_animation_type) {
2674 case ANIMATION_FADE:
2675 weston_fade_run(surface, NULL, NULL);
2676 break;
2677 case ANIMATION_ZOOM:
2678 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2679 break;
2680 default:
2681 break;
2682 }
2683 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002684}
2685
2686static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002687configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002688 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002689{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002690 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2691 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002692
Pekka Paalanen77346a62011-11-30 16:26:35 +02002693 shsurf = get_shell_surface(surface);
2694 if (shsurf)
2695 surface_type = shsurf->type;
2696
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002697 surface->geometry.x = x;
2698 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002699 surface->geometry.width = width;
2700 surface->geometry.height = height;
2701 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002702
2703 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002704 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002705 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002706 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002707 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002708 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002709 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002710 surface->geometry.x = surface->output->x;
2711 surface->geometry.y = surface->output->y +
2712 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002713 break;
Alex Wu4539b082012-03-01 12:57:46 +08002714 case SHELL_SURFACE_TOPLEVEL:
2715 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002716 default:
2717 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002718 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002719
Alex Wu4539b082012-03-01 12:57:46 +08002720 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002721 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002722 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002723
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002724 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08002725 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002726 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002727}
2728
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002729static void
2730shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2731{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002732 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002733 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002734 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002735
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002736 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002737 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002738 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002739 type_changed = 1;
2740 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002741
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002742 if (!weston_surface_is_mapped(es)) {
2743 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002744 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002745 es->geometry.width != es->buffer->width ||
2746 es->geometry.height != es->buffer->height) {
2747 GLfloat from_x, from_y;
2748 GLfloat to_x, to_y;
2749
2750 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2751 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2752 configure(shell, es,
2753 es->geometry.x + to_x - from_x,
2754 es->geometry.y + to_y - from_y,
2755 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002756 }
2757}
2758
Tiago Vignattibe143262012-04-16 17:31:41 +03002759static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002760
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002761static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002762desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002763{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002764 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002765 struct desktop_shell *shell =
2766 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002767
2768 shell->child.process.pid = 0;
2769 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002770
2771 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2772 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002773 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002774 shell->child.deathstamp = time;
2775 shell->child.deathcount = 0;
2776 }
2777
2778 shell->child.deathcount++;
2779 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002780 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002781 return;
2782 }
2783
Martin Minarik6d118362012-06-07 18:01:59 +02002784 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002785 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002786}
2787
2788static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002789launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002790{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002791 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002792
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002793 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002794 &shell->child.process,
2795 shell_exe,
2796 desktop_shell_sigchld);
2797
2798 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002799 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002800 return 0;
2801}
2802
2803static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002804bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2805{
Tiago Vignattibe143262012-04-16 17:31:41 +03002806 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002807
2808 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002809 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002810}
2811
Kristian Høgsberg75840622011-09-06 13:48:16 -04002812static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002813unbind_desktop_shell(struct wl_resource *resource)
2814{
Tiago Vignattibe143262012-04-16 17:31:41 +03002815 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002816
2817 if (shell->locked)
2818 resume_desktop(shell);
2819
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002820 shell->child.desktop_shell = NULL;
2821 shell->prepare_event_sent = false;
2822 free(resource);
2823}
2824
2825static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002826bind_desktop_shell(struct wl_client *client,
2827 void *data, uint32_t version, uint32_t id)
2828{
Tiago Vignattibe143262012-04-16 17:31:41 +03002829 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002830 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002831
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002832 resource = wl_client_add_object(client, &desktop_shell_interface,
2833 &desktop_shell_implementation,
2834 id, shell);
2835
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002836 if (client == shell->child.client) {
2837 resource->destroy = unbind_desktop_shell;
2838 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002839 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002840 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002841
2842 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2843 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002844 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002845}
2846
Pekka Paalanen6e168112011-11-24 11:34:05 +02002847static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002848screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2849{
2850 struct desktop_shell *shell = surface->private;
2851
2852 if (!shell->locked)
2853 return;
2854
2855 center_on_output(surface, surface->output);
2856
2857 if (wl_list_empty(&surface->layer_link)) {
2858 wl_list_insert(shell->lock_layer.surface_list.prev,
2859 &surface->layer_link);
2860 weston_surface_assign_output(surface);
2861 shell->compositor->idle_time = shell->screensaver.duration;
2862 weston_compositor_wake(shell->compositor);
2863 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2864 }
2865}
2866
2867static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02002868screensaver_set_surface(struct wl_client *client,
2869 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002870 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02002871 struct wl_resource *output_resource)
2872{
Tiago Vignattibe143262012-04-16 17:31:41 +03002873 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002874 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002875 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002876
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002877 surface->configure = screensaver_configure;
2878 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002879 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002880}
2881
2882static const struct screensaver_interface screensaver_implementation = {
2883 screensaver_set_surface
2884};
2885
2886static void
2887unbind_screensaver(struct wl_resource *resource)
2888{
Tiago Vignattibe143262012-04-16 17:31:41 +03002889 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002890
Pekka Paalanen77346a62011-11-30 16:26:35 +02002891 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002892 free(resource);
2893}
2894
2895static void
2896bind_screensaver(struct wl_client *client,
2897 void *data, uint32_t version, uint32_t id)
2898{
Tiago Vignattibe143262012-04-16 17:31:41 +03002899 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002900 struct wl_resource *resource;
2901
2902 resource = wl_client_add_object(client, &screensaver_interface,
2903 &screensaver_implementation,
2904 id, shell);
2905
Pekka Paalanen77346a62011-11-30 16:26:35 +02002906 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002907 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002908 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002909 return;
2910 }
2911
2912 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2913 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002914 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002915}
2916
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002917static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002918input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2919{
2920 struct weston_mode *mode = surface->output->current;
2921 GLfloat x = (mode->width - surface->buffer->width) / 2;
2922 GLfloat y = mode->height - surface->buffer->height;
2923
2924 /* Don't map the input panel here, wait for
2925 * show_input_panels signal. */
2926
2927 weston_surface_configure(surface,
2928 surface->output->x + x,
2929 surface->output->y + y,
2930 surface->buffer->width,
2931 surface->buffer->height);
2932}
2933
2934static void
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002935destroy_input_panel_surface(struct wl_listener *listener,
2936 void *data)
2937{
2938 struct input_panel_surface *input_panel_surface =
2939 container_of(listener, struct input_panel_surface, listener);
2940
2941 wl_list_remove(&listener->link);
2942 wl_list_remove(&input_panel_surface->link);
2943
2944 free(input_panel_surface);
2945}
2946
2947static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002948input_panel_set_surface(struct wl_client *client,
2949 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002950 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002951 struct wl_resource *output_resource)
2952{
2953 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002954 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002955 struct weston_output *output = output_resource->data;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002956 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002957
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002958 surface->configure = input_panel_configure;
2959 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002960 surface->output = output;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002961
2962 input_panel_surface = malloc(sizeof *input_panel_surface);
2963 if (!input_panel_surface) {
2964 wl_resource_post_no_memory(resource);
2965 return;
2966 }
2967
2968 input_panel_surface->surface = surface;
2969 input_panel_surface->listener.notify = destroy_input_panel_surface;
2970
2971 wl_signal_add(&surface_resource->destroy_signal,
2972 &input_panel_surface->listener);
2973
2974 wl_list_insert(&shell->input_panel.surfaces,
2975 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002976}
2977
2978static const struct input_panel_interface input_panel_implementation = {
2979 input_panel_set_surface
2980};
2981
2982static void
2983unbind_input_panel(struct wl_resource *resource)
2984{
2985 struct desktop_shell *shell = resource->data;
2986
2987 shell->input_panel.binding = NULL;
2988 free(resource);
2989}
2990
2991static void
2992bind_input_panel(struct wl_client *client,
2993 void *data, uint32_t version, uint32_t id)
2994{
2995 struct desktop_shell *shell = data;
2996 struct wl_resource *resource;
2997
2998 resource = wl_client_add_object(client, &input_panel_interface,
2999 &input_panel_implementation,
3000 id, shell);
3001
3002 if (shell->input_panel.binding == NULL) {
3003 resource->destroy = unbind_input_panel;
3004 shell->input_panel.binding = resource;
3005 return;
3006 }
3007
3008 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3009 "interface object already bound");
3010 wl_resource_destroy(resource);
3011}
3012
Kristian Høgsberg07045392012-02-19 18:52:44 -05003013struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003014 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003015 struct weston_surface *current;
3016 struct wl_listener listener;
3017 struct wl_keyboard_grab grab;
3018};
3019
3020static void
3021switcher_next(struct switcher *switcher)
3022{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003023 struct weston_surface *surface;
3024 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003025 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003026 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003027
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003028 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003029 switch (get_shell_surface_type(surface)) {
3030 case SHELL_SURFACE_TOPLEVEL:
3031 case SHELL_SURFACE_FULLSCREEN:
3032 case SHELL_SURFACE_MAXIMIZED:
3033 if (first == NULL)
3034 first = surface;
3035 if (prev == switcher->current)
3036 next = surface;
3037 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003038 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003039 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003040 weston_surface_damage(surface);
3041 break;
3042 default:
3043 break;
3044 }
Alex Wu1659daa2012-04-01 20:13:09 +08003045
3046 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003047 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003048 surface->geometry.dirty = 1;
3049 weston_surface_damage(surface);
3050 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003051 }
3052
3053 if (next == NULL)
3054 next = first;
3055
Alex Wu07b26062012-03-12 16:06:01 +08003056 if (next == NULL)
3057 return;
3058
Kristian Høgsberg07045392012-02-19 18:52:44 -05003059 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003060 wl_signal_add(&next->surface.resource.destroy_signal,
3061 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003062
3063 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003064 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003065
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003066 shsurf = get_shell_surface(switcher->current);
3067 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003068 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003069}
3070
3071static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003072switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003073{
3074 struct switcher *switcher =
3075 container_of(listener, struct switcher, listener);
3076
3077 switcher_next(switcher);
3078}
3079
3080static void
Daniel Stone351eb612012-05-31 15:27:47 -04003081switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003082{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003083 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003084 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003085 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003086
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003087 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003088 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003089 weston_surface_damage(surface);
3090 }
3091
Alex Wu07b26062012-03-12 16:06:01 +08003092 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003093 activate(switcher->shell, switcher->current,
3094 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003095 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003096 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003097 free(switcher);
3098}
3099
3100static void
3101switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003102 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003103{
3104 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003105 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003106
Daniel Stonec9785ea2012-05-30 16:31:52 +01003107 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003108 switcher_next(switcher);
3109}
3110
3111static void
3112switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3113 uint32_t mods_depressed, uint32_t mods_latched,
3114 uint32_t mods_locked, uint32_t group)
3115{
3116 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003117 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003118
Daniel Stone351eb612012-05-31 15:27:47 -04003119 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3120 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003121}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003122
3123static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003124 switcher_key,
3125 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003126};
3127
3128static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003129switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3130 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003131{
Tiago Vignattibe143262012-04-16 17:31:41 +03003132 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003133 struct switcher *switcher;
3134
3135 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003136 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003137 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003138 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003139 wl_list_init(&switcher->listener.link);
3140
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003141 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003142 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003143 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3144 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003145 switcher_next(switcher);
3146}
3147
Pekka Paalanen3c647232011-12-22 13:43:43 +02003148static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003149backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3150 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003151{
3152 struct weston_compositor *compositor = data;
3153 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003154 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003155
3156 /* TODO: we're limiting to simple use cases, where we assume just
3157 * control on the primary display. We'd have to extend later if we
3158 * ever get support for setting backlights on random desktop LCD
3159 * panels though */
3160 output = get_default_output(compositor);
3161 if (!output)
3162 return;
3163
3164 if (!output->set_backlight)
3165 return;
3166
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003167 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3168 backlight_new = output->backlight_current - 25;
3169 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3170 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003171
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003172 if (backlight_new < 5)
3173 backlight_new = 5;
3174 if (backlight_new > 255)
3175 backlight_new = 255;
3176
3177 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003178 output->set_backlight(output, output->backlight_current);
3179}
3180
3181static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003182debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3183 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003184{
Tiago Vignattibe143262012-04-16 17:31:41 +03003185 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003186 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003187 struct weston_surface *surface;
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003188 struct weston_plane plane;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003189
3190 if (shell->debug_repaint_surface) {
3191 weston_surface_destroy(shell->debug_repaint_surface);
3192 shell->debug_repaint_surface = NULL;
3193 } else {
3194 surface = weston_surface_create(compositor);
3195 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3196 weston_surface_configure(surface, 0, 0, 8192, 8192);
3197 wl_list_insert(&compositor->fade_layer.surface_list,
3198 &surface->layer_link);
3199 weston_surface_assign_output(surface);
3200 pixman_region32_init(&surface->input);
3201
3202 /* Here's the dirty little trick that makes the
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003203 * repaint debugging work: we move the surface to a
3204 * different plane and force an update_transform to
3205 * update dependent state and clear the
3206 * geometry.dirty bit. This way the call to
3207 * damage_below() in update_transform() does not
3208 * add damage to the primary plane. */
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003209
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003210 weston_plane_init(&plane, 0, 0);
3211 surface->plane = &plane;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003212 weston_surface_update_transform(surface);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003213 shell->debug_repaint_surface = surface;
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003214 surface->plane = &compositor->primary_plane;
3215 weston_plane_release(&plane);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003216 }
3217}
3218
3219static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003220force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3221 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003222{
3223 struct wl_client *client;
3224 pid_t pid;
3225 uid_t uid;
3226 gid_t gid;
3227
Daniel Stone325fc2d2012-05-30 16:31:58 +01003228 client = seat->keyboard->focus->resource.client;
3229 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003230
Daniel Stone325fc2d2012-05-30 16:31:58 +01003231 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003232}
3233
3234static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003235workspace_up_binding(struct wl_seat *seat, uint32_t time,
3236 uint32_t key, void *data)
3237{
3238 struct desktop_shell *shell = data;
3239 unsigned int new_index = shell->workspaces.current;
3240
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003241 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003242 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003243 if (new_index != 0)
3244 new_index--;
3245
3246 change_workspace(shell, new_index);
3247}
3248
3249static void
3250workspace_down_binding(struct wl_seat *seat, uint32_t time,
3251 uint32_t key, void *data)
3252{
3253 struct desktop_shell *shell = data;
3254 unsigned int new_index = shell->workspaces.current;
3255
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003256 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003257 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003258 if (new_index < shell->workspaces.num - 1)
3259 new_index++;
3260
3261 change_workspace(shell, new_index);
3262}
3263
3264static void
3265workspace_f_binding(struct wl_seat *seat, uint32_t time,
3266 uint32_t key, void *data)
3267{
3268 struct desktop_shell *shell = data;
3269 unsigned int new_index;
3270
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003271 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003272 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003273 new_index = key - KEY_F1;
3274 if (new_index >= shell->workspaces.num)
3275 new_index = shell->workspaces.num - 1;
3276
3277 change_workspace(shell, new_index);
3278}
3279
3280
3281static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003282shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003283{
Tiago Vignattibe143262012-04-16 17:31:41 +03003284 struct desktop_shell *shell =
3285 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003286 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003287
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003288 if (shell->child.client)
3289 wl_client_destroy(shell->child.client);
3290
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003291 wl_list_remove(&shell->lock_listener.link);
3292 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003293 wl_list_remove(&shell->show_input_panel_listener.link);
3294 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003295
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003296 wl_array_for_each(ws, &shell->workspaces.array)
3297 workspace_destroy(*ws);
3298 wl_array_release(&shell->workspaces.array);
3299
Pekka Paalanen3c647232011-12-22 13:43:43 +02003300 free(shell->screensaver.path);
3301 free(shell);
3302}
3303
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003304static void
3305shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3306{
3307 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003308 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003309
3310 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003311 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3312 MODIFIER_CTRL | MODIFIER_ALT,
3313 terminate_binding, ec);
3314 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3315 click_to_activate_binding,
3316 shell);
3317 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3318 MODIFIER_SUPER | MODIFIER_ALT,
3319 surface_opacity_binding, NULL);
3320 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3321 MODIFIER_SUPER, zoom_axis_binding,
3322 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003323
3324 /* configurable bindings */
3325 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003326 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3327 zoom_key_binding, NULL);
3328 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3329 zoom_key_binding, NULL);
3330 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3331 shell);
3332 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3333 resize_binding, shell);
3334 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3335 rotate_binding, NULL);
3336 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3337 shell);
3338 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3339 ec);
3340 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3341 backlight_binding, ec);
3342 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3343 ec);
3344 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3345 backlight_binding, ec);
Kristian Høgsberg73694c82012-06-28 14:13:10 -04003346 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003347 debug_repaint_binding, shell);
3348 weston_compositor_add_key_binding(ec, KEY_K, mod,
3349 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003350 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3351 workspace_up_binding, shell);
3352 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3353 workspace_down_binding, shell);
3354
3355 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3356 if (shell->workspaces.num > 1) {
3357 num_workspace_bindings = shell->workspaces.num;
3358 if (num_workspace_bindings > 6)
3359 num_workspace_bindings = 6;
3360 for (i = 0; i < num_workspace_bindings; i++)
3361 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3362 workspace_f_binding,
3363 shell);
3364 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003365}
3366
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003367int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003368shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003369
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003370WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003371shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003372{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003373 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003374 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003375 struct workspace **pws;
3376 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003377
3378 shell = malloc(sizeof *shell);
3379 if (shell == NULL)
3380 return -1;
3381
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003382 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003383 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003384
3385 shell->destroy_listener.notify = shell_destroy;
3386 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3387 shell->lock_listener.notify = lock;
3388 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3389 shell->unlock_listener.notify = unlock;
3390 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003391 shell->show_input_panel_listener.notify = show_input_panels;
3392 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3393 shell->hide_input_panel_listener.notify = hide_input_panels;
3394 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003395 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003396 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003397 ec->shell_interface.create_shell_surface = create_shell_surface;
3398 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003399 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003400 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003401 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003402
Pekka Paalanen77346a62011-11-30 16:26:35 +02003403 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003404 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003405
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003406 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3407 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003408 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3409 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003410 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003411
3412 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003413
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003414 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003415
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003416 for (i = 0; i < shell->workspaces.num; i++) {
3417 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3418 if (pws == NULL)
3419 return -1;
3420
3421 *pws = workspace_create();
3422 if (*pws == NULL)
3423 return -1;
3424 }
3425 activate_workspace(shell, 0);
3426
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003427 wl_list_init(&shell->workspaces.animation.link);
3428 shell->workspaces.animation.frame = animate_workspace_change_frame;
3429
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003430 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3431 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003432 return -1;
3433
Kristian Høgsberg75840622011-09-06 13:48:16 -04003434 if (wl_display_add_global(ec->wl_display,
3435 &desktop_shell_interface,
3436 shell, bind_desktop_shell) == NULL)
3437 return -1;
3438
Pekka Paalanen6e168112011-11-24 11:34:05 +02003439 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3440 shell, bind_screensaver) == NULL)
3441 return -1;
3442
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003443 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3444 shell, bind_input_panel) == NULL)
3445 return -1;
3446
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003447 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003448 if (launch_desktop_shell_process(shell) != 0)
3449 return -1;
3450
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003451 wl_list_for_each(seat, &ec->seat_list, link)
3452 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003453
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003454 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003455
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003456 return 0;
3457}