blob: 9c8a8a935f79bbb5a9d27e438a015615305fd2ba [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
Scott Moreauff1db4a2012-04-17 19:06:18 -06001135shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1136 uint32_t serial)
1137{
1138 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001139 struct desktop_shell *shell = shsurf->shell;
1140 struct weston_seat *seat;
1141 struct weston_compositor *ec = shsurf->surface->compositor;
1142 struct wl_pointer *pointer;
1143 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001144
Scott Moreauff1db4a2012-04-17 19:06:18 -06001145 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001146 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001147 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001148 if (was_unresponsive) {
1149 /* Received pong from previously unresponsive client */
1150 wl_list_for_each(seat, &ec->seat_list, link) {
1151 pointer = seat->seat.pointer;
1152 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001153 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001154 pointer->current ==
1155 &shsurf->surface->surface)
1156 end_busy_cursor(shsurf, pointer);
1157 }
1158 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001159 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001160 }
1161}
1162
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001163static void
1164shell_surface_set_title(struct wl_client *client,
1165 struct wl_resource *resource, const char *title)
1166{
1167 struct shell_surface *shsurf = resource->data;
1168
1169 free(shsurf->title);
1170 shsurf->title = strdup(title);
1171}
1172
1173static void
1174shell_surface_set_class(struct wl_client *client,
1175 struct wl_resource *resource, const char *class)
1176{
1177 struct shell_surface *shsurf = resource->data;
1178
1179 free(shsurf->class);
1180 shsurf->class = strdup(class);
1181}
1182
Juan Zhao96879df2012-02-07 08:45:41 +08001183static struct weston_output *
1184get_default_output(struct weston_compositor *compositor)
1185{
1186 return container_of(compositor->output_list.next,
1187 struct weston_output, link);
1188}
1189
Alex Wu4539b082012-03-01 12:57:46 +08001190static void
1191shell_unset_fullscreen(struct shell_surface *shsurf)
1192{
1193 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001194 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1195 shell_surface_is_top_fullscreen(shsurf)) {
1196 weston_output_switch_mode(shsurf->fullscreen_output,
1197 shsurf->fullscreen_output->origin);
1198 }
Alex Wu4539b082012-03-01 12:57:46 +08001199 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1200 shsurf->fullscreen.framerate = 0;
1201 wl_list_remove(&shsurf->fullscreen.transform.link);
1202 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001203 if (shsurf->fullscreen.black_surface)
1204 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001205 shsurf->fullscreen.black_surface = NULL;
1206 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001207 weston_surface_set_position(shsurf->surface,
1208 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001209 if (shsurf->saved_rotation_valid) {
1210 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1211 &shsurf->rotation.transform.link);
1212 shsurf->saved_rotation_valid = false;
1213 }
Alex Wu4539b082012-03-01 12:57:46 +08001214}
1215
Pekka Paalanen98262232011-12-01 10:42:22 +02001216static int
1217reset_shell_surface_type(struct shell_surface *surface)
1218{
1219 switch (surface->type) {
1220 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001221 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001222 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001223 case SHELL_SURFACE_MAXIMIZED:
1224 surface->output = get_default_output(surface->surface->compositor);
1225 weston_surface_set_position(surface->surface,
1226 surface->saved_x,
1227 surface->saved_y);
1228 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001229 case SHELL_SURFACE_NONE:
1230 case SHELL_SURFACE_TOPLEVEL:
1231 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001232 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001233 break;
1234 }
1235
1236 surface->type = SHELL_SURFACE_NONE;
1237 return 0;
1238}
1239
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001240static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001241set_surface_type(struct shell_surface *shsurf)
1242{
1243 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001244 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001245
1246 reset_shell_surface_type(shsurf);
1247
1248 shsurf->type = shsurf->next_type;
1249 shsurf->next_type = SHELL_SURFACE_NONE;
1250
1251 switch (shsurf->type) {
1252 case SHELL_SURFACE_TOPLEVEL:
1253 break;
1254 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001255 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001256 pes->geometry.x + shsurf->transient.x,
1257 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001258 break;
1259
1260 case SHELL_SURFACE_MAXIMIZED:
1261 shsurf->saved_x = surface->geometry.x;
1262 shsurf->saved_y = surface->geometry.y;
1263 shsurf->saved_position_valid = true;
1264 break;
1265
1266 case SHELL_SURFACE_FULLSCREEN:
1267 shsurf->saved_x = surface->geometry.x;
1268 shsurf->saved_y = surface->geometry.y;
1269 shsurf->saved_position_valid = true;
1270
1271 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1272 wl_list_remove(&shsurf->rotation.transform.link);
1273 wl_list_init(&shsurf->rotation.transform.link);
1274 shsurf->surface->geometry.dirty = 1;
1275 shsurf->saved_rotation_valid = true;
1276 }
1277 break;
1278
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001279 default:
1280 break;
1281 }
1282}
1283
1284static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001285set_toplevel(struct shell_surface *shsurf)
1286{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001287 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001288}
1289
1290static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001291shell_surface_set_toplevel(struct wl_client *client,
1292 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001293{
Pekka Paalanen98262232011-12-01 10:42:22 +02001294 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001295
Tiago Vignattibc052c92012-04-19 16:18:18 +03001296 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001297}
1298
1299static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001300set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001301 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001302{
1303 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001304 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001305 shsurf->transient.x = x;
1306 shsurf->transient.y = y;
1307 shsurf->transient.flags = flags;
1308 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1309}
1310
1311static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001312shell_surface_set_transient(struct wl_client *client,
1313 struct wl_resource *resource,
1314 struct wl_resource *parent_resource,
1315 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001316{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001317 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001318 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001319
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001320 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001321}
1322
Tiago Vignattibe143262012-04-16 17:31:41 +03001323static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001324shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001325{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001326 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001327}
1328
1329static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001330get_output_panel_height(struct desktop_shell *shell,
1331 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001332{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001333 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001334 int panel_height = 0;
1335
1336 if (!output)
1337 return 0;
1338
Juan Zhao4ab94682012-07-09 22:24:09 -07001339 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001340 if (surface->output == output) {
1341 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001342 break;
1343 }
1344 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001345
Juan Zhao96879df2012-02-07 08:45:41 +08001346 return panel_height;
1347}
1348
1349static void
1350shell_surface_set_maximized(struct wl_client *client,
1351 struct wl_resource *resource,
1352 struct wl_resource *output_resource )
1353{
1354 struct shell_surface *shsurf = resource->data;
1355 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001356 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001357 uint32_t edges = 0, panel_height = 0;
1358
1359 /* get the default output, if the client set it as NULL
1360 check whether the ouput is available */
1361 if (output_resource)
1362 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001363 else if (es->output)
1364 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001365 else
1366 shsurf->output = get_default_output(es->compositor);
1367
Tiago Vignattibe143262012-04-16 17:31:41 +03001368 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001369 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001370 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001371
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001372 shsurf->client->send_configure(shsurf->surface, edges,
Rob Bradford31b68622012-07-02 19:00:19 +01001373 shsurf->output->current->width,
1374 shsurf->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001375
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001376 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001377}
1378
Alex Wu21858432012-04-01 20:13:08 +08001379static void
1380black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1381
Alex Wu4539b082012-03-01 12:57:46 +08001382static struct weston_surface *
1383create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001384 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001385 GLfloat x, GLfloat y, int w, int h)
1386{
1387 struct weston_surface *surface = NULL;
1388
1389 surface = weston_surface_create(ec);
1390 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001391 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001392 return NULL;
1393 }
1394
Alex Wu21858432012-04-01 20:13:08 +08001395 surface->configure = black_surface_configure;
1396 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001397 weston_surface_configure(surface, x, y, w, h);
1398 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001399 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
1400
Alex Wu4539b082012-03-01 12:57:46 +08001401 return surface;
1402}
1403
1404/* Create black surface and append it to the associated fullscreen surface.
1405 * Handle size dismatch and positioning according to the method. */
1406static void
1407shell_configure_fullscreen(struct shell_surface *shsurf)
1408{
1409 struct weston_output *output = shsurf->fullscreen_output;
1410 struct weston_surface *surface = shsurf->surface;
1411 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001412 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001413
Alex Wu4539b082012-03-01 12:57:46 +08001414 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001415 shsurf->fullscreen.black_surface =
1416 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001417 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001418 output->x, output->y,
1419 output->current->width,
1420 output->current->height);
1421
1422 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1423 wl_list_insert(&surface->layer_link,
1424 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001425 shsurf->fullscreen.black_surface->output = output;
1426
1427 switch (shsurf->fullscreen.type) {
1428 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001429 if (surface->buffer)
1430 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001431 break;
1432 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1433 matrix = &shsurf->fullscreen.transform.matrix;
1434 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001435
1436 output_aspect = (float) output->current->width /
1437 (float) output->current->height;
1438 surface_aspect = (float) surface->geometry.width /
1439 (float) surface->geometry.height;
1440 if (output_aspect < surface_aspect)
1441 scale = (float) output->current->width /
1442 (float) surface->geometry.width;
1443 else
1444 scale = (float) output->current->height /
1445 (float) surface->geometry.height;
1446
Alex Wu4539b082012-03-01 12:57:46 +08001447 weston_matrix_scale(matrix, scale, scale, 1);
1448 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001449 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001450 &shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001451 x = output->x + (output->current->width - surface->geometry.width * scale) / 2;
1452 y = output->y + (output->current->height - surface->geometry.height * scale) / 2;
1453 weston_surface_set_position(surface, x, y);
1454
Alex Wu4539b082012-03-01 12:57:46 +08001455 break;
1456 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001457 if (shell_surface_is_top_fullscreen(shsurf)) {
1458 struct weston_mode mode = {0,
1459 surface->geometry.width,
1460 surface->geometry.height,
1461 shsurf->fullscreen.framerate};
1462
1463 if (weston_output_switch_mode(output, &mode) == 0) {
1464 weston_surface_configure(shsurf->fullscreen.black_surface,
1465 output->x, output->y,
1466 output->current->width,
1467 output->current->height);
1468 weston_surface_set_position(surface, output->x, output->y);
1469 break;
1470 }
1471 }
Alex Wu4539b082012-03-01 12:57:46 +08001472 break;
1473 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1474 break;
1475 default:
1476 break;
1477 }
1478}
1479
1480/* make the fullscreen and black surface at the top */
1481static void
1482shell_stack_fullscreen(struct shell_surface *shsurf)
1483{
Alex Wubd3354b2012-04-17 17:20:49 +08001484 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001485 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001486 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001487
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001488 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001489 wl_list_insert(&shell->fullscreen_layer.surface_list,
1490 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001491 weston_surface_damage(surface);
1492
1493 if (!shsurf->fullscreen.black_surface)
1494 shsurf->fullscreen.black_surface =
1495 create_black_surface(surface->compositor,
1496 surface,
1497 output->x, output->y,
1498 output->current->width,
1499 output->current->height);
1500
1501 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001502 wl_list_insert(&surface->layer_link,
1503 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001504 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001505}
1506
1507static void
1508shell_map_fullscreen(struct shell_surface *shsurf)
1509{
Alex Wu4539b082012-03-01 12:57:46 +08001510 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001511 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001512}
1513
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001514static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001515shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001516 struct wl_resource *resource,
1517 uint32_t method,
1518 uint32_t framerate,
1519 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001520{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001521 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001522 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001523
1524 if (output_resource)
1525 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001526 else if (es->output)
1527 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001528 else
1529 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001530
Alex Wu4539b082012-03-01 12:57:46 +08001531 shsurf->fullscreen_output = shsurf->output;
1532 shsurf->fullscreen.type = method;
1533 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001534 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001535
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001536 shsurf->client->send_configure(shsurf->surface, 0,
1537 shsurf->output->current->width,
1538 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001539}
1540
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001541static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001542popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001543 struct wl_surface *surface,
1544 wl_fixed_t x,
1545 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001546{
Daniel Stone37816df2012-05-16 18:45:18 +01001547 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001548 struct shell_surface *priv =
1549 container_of(grab, struct shell_surface, popup.grab);
1550 struct wl_client *client = priv->surface->surface.resource.client;
1551
Pekka Paalanencb108432012-01-19 16:25:40 +02001552 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001553 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001554 grab->focus = surface;
1555 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001556 wl_pointer_set_focus(pointer, NULL,
1557 wl_fixed_from_int(0),
1558 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001559 grab->focus = NULL;
1560 }
1561}
1562
1563static void
Scott Moreau447013d2012-02-18 05:05:29 -07001564popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001565 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001566{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001567 struct wl_resource *resource;
1568
Daniel Stone37816df2012-05-16 18:45:18 +01001569 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001570 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001571 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001572}
1573
1574static void
Scott Moreau447013d2012-02-18 05:05:29 -07001575popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001576 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001577{
1578 struct wl_resource *resource;
1579 struct shell_surface *shsurf =
1580 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001581 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001582 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001583 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001584
Daniel Stone37816df2012-05-16 18:45:18 +01001585 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001586 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001587 display = wl_client_get_display(resource->client);
1588 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001589 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001590 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001591 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001592 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001593 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001594 wl_pointer_end_grab(grab->pointer);
1595 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001596 }
1597
Daniel Stone4dbadb12012-05-30 16:31:51 +01001598 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001599 shsurf->popup.initial_up = 1;
1600}
1601
Scott Moreau447013d2012-02-18 05:05:29 -07001602static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001603 popup_grab_focus,
1604 popup_grab_motion,
1605 popup_grab_button,
1606};
1607
1608static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001609shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001610{
Daniel Stone37816df2012-05-16 18:45:18 +01001611 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001612 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001613 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001614
1615 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001616 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001617
Pekka Paalanen938269a2012-02-07 14:19:01 +02001618 weston_surface_update_transform(parent);
1619 if (parent->transform.enabled) {
1620 shsurf->popup.parent_transform.matrix =
1621 parent->transform.matrix;
1622 } else {
1623 /* construct x, y translation matrix */
1624 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1625 shsurf->popup.parent_transform.matrix.d[12] =
1626 parent->geometry.x;
1627 shsurf->popup.parent_transform.matrix.d[13] =
1628 parent->geometry.y;
1629 }
1630 wl_list_insert(es->geometry.transformation_list.prev,
1631 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001632 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001633
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001634 shsurf->popup.initial_up = 0;
1635
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001636 /* We don't require the grab to still be active, but if another
1637 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001638 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1639 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001640 } else {
1641 wl_shell_surface_send_popup_done(&shsurf->resource);
1642 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001643}
1644
1645static void
1646shell_surface_set_popup(struct wl_client *client,
1647 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001648 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001649 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001650 struct wl_resource *parent_resource,
1651 int32_t x, int32_t y, uint32_t flags)
1652{
1653 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001654
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001655 shsurf->type = SHELL_SURFACE_POPUP;
1656 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001657 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001658 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001659 shsurf->popup.x = x;
1660 shsurf->popup.y = y;
1661}
1662
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001663static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001664 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001665 shell_surface_move,
1666 shell_surface_resize,
1667 shell_surface_set_toplevel,
1668 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001669 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001670 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001671 shell_surface_set_maximized,
1672 shell_surface_set_title,
1673 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001674};
1675
1676static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001677destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001678{
Daniel Stone37816df2012-05-16 18:45:18 +01001679 if (shsurf->popup.grab.pointer)
1680 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001681
Alex Wubd3354b2012-04-17 17:20:49 +08001682 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1683 shell_surface_is_top_fullscreen(shsurf)) {
1684 weston_output_switch_mode(shsurf->fullscreen_output,
1685 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001686 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001687
Alex Wuaa08e2d2012-03-05 11:01:40 +08001688 if (shsurf->fullscreen.black_surface)
1689 weston_surface_destroy(shsurf->fullscreen.black_surface);
1690
Alex Wubd3354b2012-04-17 17:20:49 +08001691 /* As destroy_resource() use wl_list_for_each_safe(),
1692 * we can always remove the listener.
1693 */
1694 wl_list_remove(&shsurf->surface_destroy_listener.link);
1695 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001696 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001697
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001698 wl_list_remove(&shsurf->link);
1699 free(shsurf);
1700}
1701
1702static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001703shell_destroy_shell_surface(struct wl_resource *resource)
1704{
1705 struct shell_surface *shsurf = resource->data;
1706
1707 destroy_shell_surface(shsurf);
1708}
1709
1710static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001711shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001712{
1713 struct shell_surface *shsurf = container_of(listener,
1714 struct shell_surface,
1715 surface_destroy_listener);
1716
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001717 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001718 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001719 } else {
1720 wl_signal_emit(&shsurf->resource.destroy_signal,
1721 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001722 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001723 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001724}
1725
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001726static void
1727shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1728
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001729static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001730get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001731{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001732 if (surface->configure == shell_surface_configure)
1733 return surface->private;
1734 else
1735 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001736}
1737
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001738static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001739create_shell_surface(void *shell, struct weston_surface *surface,
1740 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001741{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001742 struct shell_surface *shsurf;
1743
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001744 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001745 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001746 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001747 }
1748
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001749 shsurf = calloc(1, sizeof *shsurf);
1750 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001751 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001752 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001753 }
1754
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001755 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001756 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001757
Tiago Vignattibc052c92012-04-19 16:18:18 +03001758 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001759 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001760 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001761 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001762 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001763 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1764 shsurf->fullscreen.framerate = 0;
1765 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001766 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001767 wl_list_init(&shsurf->fullscreen.transform.link);
1768
Tiago Vignattibc052c92012-04-19 16:18:18 +03001769 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001770 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1771 wl_signal_add(&surface->surface.resource.destroy_signal,
1772 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001773
1774 /* init link so its safe to always remove it in destroy_shell_surface */
1775 wl_list_init(&shsurf->link);
1776
Pekka Paalanen460099f2012-01-20 16:48:25 +02001777 /* empty when not in use */
1778 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001779 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001780
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001781 wl_list_init(&shsurf->workspace_transform.link);
1782
Pekka Paalanen98262232011-12-01 10:42:22 +02001783 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001784 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001785
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001786 shsurf->client = client;
1787
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001788 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001789}
1790
1791static void
1792shell_get_shell_surface(struct wl_client *client,
1793 struct wl_resource *resource,
1794 uint32_t id,
1795 struct wl_resource *surface_resource)
1796{
1797 struct weston_surface *surface = surface_resource->data;
1798 struct desktop_shell *shell = resource->data;
1799 struct shell_surface *shsurf;
1800
1801 if (get_shell_surface(surface)) {
1802 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001803 WL_DISPLAY_ERROR_INVALID_OBJECT,
1804 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001805 return;
1806 }
1807
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001808 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001809 if (!shsurf) {
1810 wl_resource_post_error(surface_resource,
1811 WL_DISPLAY_ERROR_INVALID_OBJECT,
1812 "surface->configure already set");
1813 return;
1814 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001815
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001816 shsurf->resource.destroy = shell_destroy_shell_surface;
1817 shsurf->resource.object.id = id;
1818 shsurf->resource.object.interface = &wl_shell_surface_interface;
1819 shsurf->resource.object.implementation =
1820 (void (**)(void)) &shell_surface_implementation;
1821 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001822
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001823 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001824}
1825
1826static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001827 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001828};
1829
Kristian Høgsberg07937562011-04-12 17:25:42 -04001830static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001831handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001832{
1833 proc->pid = 0;
1834}
1835
1836static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001837launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001838{
1839 if (shell->screensaver.binding)
1840 return;
1841
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001842 if (!shell->screensaver.path)
1843 return;
1844
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001845 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001846 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001847 return;
1848 }
1849
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001850 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001851 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001852 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001853 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001854}
1855
1856static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001857terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001858{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001859 if (shell->screensaver.process.pid == 0)
1860 return;
1861
1862 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001863}
1864
1865static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001866configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001867{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001868 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001869
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001870 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1871 if (s->output == es->output && s != es) {
1872 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001873 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001874 }
1875 }
1876
1877 weston_surface_configure(es, es->output->x, es->output->y,
1878 es->buffer->width, es->buffer->height);
1879
1880 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001881 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001882 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001883 }
1884}
1885
1886static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001887background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1888{
1889 struct desktop_shell *shell = es->private;
1890
1891 configure_static_surface(es, &shell->background_layer);
1892}
1893
1894static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001895desktop_shell_set_background(struct wl_client *client,
1896 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001897 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001898 struct wl_resource *surface_resource)
1899{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001900 struct desktop_shell *shell = resource->data;
1901 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001902
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001903 if (surface->configure) {
1904 wl_resource_post_error(surface_resource,
1905 WL_DISPLAY_ERROR_INVALID_OBJECT,
1906 "surface role already assigned");
1907 return;
1908 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001909
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001910 surface->configure = background_configure;
1911 surface->private = shell;
1912 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001913 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001914 surface_resource,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001915 surface->output->current->width,
1916 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001917}
1918
1919static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001920panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1921{
1922 struct desktop_shell *shell = es->private;
1923
1924 configure_static_surface(es, &shell->panel_layer);
1925}
1926
1927static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001928desktop_shell_set_panel(struct wl_client *client,
1929 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001930 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001931 struct wl_resource *surface_resource)
1932{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001933 struct desktop_shell *shell = resource->data;
1934 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001935
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001936 if (surface->configure) {
1937 wl_resource_post_error(surface_resource,
1938 WL_DISPLAY_ERROR_INVALID_OBJECT,
1939 "surface role already assigned");
1940 return;
1941 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001942
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001943 surface->configure = panel_configure;
1944 surface->private = shell;
1945 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001946 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001947 surface_resource,
1948 surface->output->current->width,
1949 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001950}
1951
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001952static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001953lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1954{
1955 struct desktop_shell *shell = surface->private;
1956
1957 center_on_output(surface, get_default_output(shell->compositor));
1958
1959 if (!weston_surface_is_mapped(surface)) {
1960 wl_list_insert(&shell->lock_layer.surface_list,
1961 &surface->layer_link);
1962 weston_surface_assign_output(surface);
1963 weston_compositor_wake(shell->compositor);
1964 }
1965}
1966
1967static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001968handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001969{
Tiago Vignattibe143262012-04-16 17:31:41 +03001970 struct desktop_shell *shell =
1971 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001972
Martin Minarik6d118362012-06-07 18:01:59 +02001973 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001974 shell->lock_surface = NULL;
1975}
1976
1977static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001978desktop_shell_set_lock_surface(struct wl_client *client,
1979 struct wl_resource *resource,
1980 struct wl_resource *surface_resource)
1981{
Tiago Vignattibe143262012-04-16 17:31:41 +03001982 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001983 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001984
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001985 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001986
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001987 if (!shell->locked)
1988 return;
1989
Pekka Paalanen98262232011-12-01 10:42:22 +02001990 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001991
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001992 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1993 wl_signal_add(&surface_resource->destroy_signal,
1994 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001995
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001996 surface->configure = lock_surface_configure;
1997 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001998}
1999
2000static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002001resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002002{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002003 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002004 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002005
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002006 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
2007 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002008
2009 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002010
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002011 wl_list_remove(&shell->lock_layer.link);
2012 wl_list_insert(&shell->compositor->cursor_layer.link,
2013 &shell->fullscreen_layer.link);
2014 wl_list_insert(&shell->fullscreen_layer.link,
2015 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002016 if (shell->showing_input_panels) {
2017 wl_list_insert(&shell->panel_layer.link,
2018 &shell->input_panel_layer.link);
2019 wl_list_insert(&shell->input_panel_layer.link,
2020 &ws->layer.link);
2021 } else {
2022 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2023 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002024
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002025 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002026
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002027 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002028 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002029 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002030 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002031}
2032
2033static void
2034desktop_shell_unlock(struct wl_client *client,
2035 struct wl_resource *resource)
2036{
Tiago Vignattibe143262012-04-16 17:31:41 +03002037 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002038
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002039 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002040
2041 if (shell->locked)
2042 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002043}
2044
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002045static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002046desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002047 struct wl_resource *resource,
2048 struct wl_resource *surface_resource)
2049{
2050 struct desktop_shell *shell = resource->data;
2051
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002052 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002053}
2054
Kristian Høgsberg75840622011-09-06 13:48:16 -04002055static const struct desktop_shell_interface desktop_shell_implementation = {
2056 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002057 desktop_shell_set_panel,
2058 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002059 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002060 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002061};
2062
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002063static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002064get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002065{
2066 struct shell_surface *shsurf;
2067
2068 shsurf = get_shell_surface(surface);
2069 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002070 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002071 return shsurf->type;
2072}
2073
Kristian Høgsberg75840622011-09-06 13:48:16 -04002074static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002075move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002076{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002077 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002078 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002079 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002080
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002081 if (surface == NULL)
2082 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002083
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002084 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002085 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002086 return;
2087
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002088 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002089}
2090
2091static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002092resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002093{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002094 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002095 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002096 uint32_t edges = 0;
2097 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002098 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002099
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002100 if (surface == NULL)
2101 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002102
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002103 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002104 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002105 return;
2106
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002107 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002108 wl_fixed_to_int(seat->pointer->grab_x),
2109 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002110 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002111
Pekka Paalanen60921e52012-01-25 15:55:43 +02002112 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002113 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002114 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002115 edges |= 0;
2116 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002117 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002118
Pekka Paalanen60921e52012-01-25 15:55:43 +02002119 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002120 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002121 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002122 edges |= 0;
2123 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002124 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002125
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002126 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002127}
2128
2129static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002130surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002131 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002132{
Scott Moreau02709af2012-05-22 01:54:10 -06002133 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002134 struct shell_surface *shsurf;
2135 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002136 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002137
2138 if (surface == NULL)
2139 return;
2140
2141 shsurf = get_shell_surface(surface);
2142 if (!shsurf)
2143 return;
2144
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002145 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002146
Scott Moreau02709af2012-05-22 01:54:10 -06002147 if (surface->alpha > 1.0)
2148 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002149 if (surface->alpha < step)
2150 surface->alpha = step;
2151
2152 surface->geometry.dirty = 1;
2153 weston_surface_damage(surface);
2154}
2155
2156static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002157do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002158 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002159{
Daniel Stone37816df2012-05-16 18:45:18 +01002160 struct weston_seat *ws = (struct weston_seat *) seat;
2161 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002162 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002163 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002164
2165 wl_list_for_each(output, &compositor->output_list, link) {
2166 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002167 wl_fixed_to_double(seat->pointer->x),
2168 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002169 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002170 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002171 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002172 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002173 increment = -output->zoom.increment;
2174 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002175 increment = output->zoom.increment *
2176 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002177 else
2178 increment = 0;
2179
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002180 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002181
Scott Moreaue6603982012-06-11 13:07:51 -06002182 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002183 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002184 else if (output->zoom.level > output->zoom.max_level)
2185 output->zoom.level = output->zoom.max_level;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002186 else {
Scott Moreaue6603982012-06-11 13:07:51 -06002187 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002188 output->disable_planes++;
2189 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002190
Scott Moreaue6603982012-06-11 13:07:51 -06002191 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002192
Scott Moreau8dacaab2012-06-17 18:10:58 -06002193 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002194 }
2195 }
2196}
2197
Scott Moreauccbf29d2012-02-22 14:21:41 -07002198static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002199zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002200 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002201{
2202 do_zoom(seat, time, 0, axis, value);
2203}
2204
2205static void
2206zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2207 void *data)
2208{
2209 do_zoom(seat, time, key, 0, 0);
2210}
2211
2212static void
2213terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2214 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002215{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002216 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002217
Daniel Stone325fc2d2012-05-30 16:31:58 +01002218 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002219}
2220
2221static void
Scott Moreau447013d2012-02-18 05:05:29 -07002222rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002223 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002224{
2225 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002226 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002227 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002228 struct shell_surface *shsurf = rotate->base.shsurf;
2229 struct weston_surface *surface;
2230 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2231
2232 if (!shsurf)
2233 return;
2234
2235 surface = shsurf->surface;
2236
2237 cx = 0.5f * surface->geometry.width;
2238 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002239
Daniel Stone37816df2012-05-16 18:45:18 +01002240 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2241 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002242 r = sqrtf(dx * dx + dy * dy);
2243
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002244 wl_list_remove(&shsurf->rotation.transform.link);
2245 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002246
2247 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002248 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002249 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002250
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002251 weston_matrix_init(&rotate->rotation);
2252 rotate->rotation.d[0] = dx / r;
2253 rotate->rotation.d[4] = -dy / r;
2254 rotate->rotation.d[1] = -rotate->rotation.d[4];
2255 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002256
2257 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002258 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002259 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002260 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002261 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002262
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002263 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002264 &shsurf->surface->geometry.transformation_list,
2265 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002266 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002267 wl_list_init(&shsurf->rotation.transform.link);
2268 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002269 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002270 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002271
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002272 /* We need to adjust the position of the surface
2273 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002274 cposx = surface->geometry.x + cx;
2275 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002276 dposx = rotate->center.x - cposx;
2277 dposy = rotate->center.y - cposy;
2278 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002279 weston_surface_set_position(surface,
2280 surface->geometry.x + dposx,
2281 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002282 }
2283
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002284 /* Repaint implies weston_surface_update_transform(), which
2285 * lazily applies the damage due to rotation update.
2286 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002287 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002288}
2289
2290static void
Scott Moreau447013d2012-02-18 05:05:29 -07002291rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002292 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002293{
2294 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002295 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002296 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002297 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002298 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002299
Daniel Stone4dbadb12012-05-30 16:31:51 +01002300 if (pointer->button_count == 0 &&
2301 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002302 if (shsurf)
2303 weston_matrix_multiply(&shsurf->rotation.rotation,
2304 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002305 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002306 free(rotate);
2307 }
2308}
2309
Scott Moreau447013d2012-02-18 05:05:29 -07002310static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002311 noop_grab_focus,
2312 rotate_grab_motion,
2313 rotate_grab_button,
2314};
2315
2316static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002317rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002318 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002319{
2320 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002321 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002322 struct shell_surface *surface;
2323 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002324 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002325 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002326
2327 if (base_surface == NULL)
2328 return;
2329
2330 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002331 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002332 return;
2333
Pekka Paalanen460099f2012-01-20 16:48:25 +02002334 rotate = malloc(sizeof *rotate);
2335 if (!rotate)
2336 return;
2337
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002338 weston_surface_to_global_float(surface->surface,
2339 surface->surface->geometry.width / 2,
2340 surface->surface->geometry.height / 2,
2341 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002342
Daniel Stone37816df2012-05-16 18:45:18 +01002343 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2344 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002345 r = sqrtf(dx * dx + dy * dy);
2346 if (r > 20.0f) {
2347 struct weston_matrix inverse;
2348
2349 weston_matrix_init(&inverse);
2350 inverse.d[0] = dx / r;
2351 inverse.d[4] = dy / r;
2352 inverse.d[1] = -inverse.d[4];
2353 inverse.d[5] = inverse.d[0];
2354 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002355
2356 weston_matrix_init(&rotate->rotation);
2357 rotate->rotation.d[0] = dx / r;
2358 rotate->rotation.d[4] = -dy / r;
2359 rotate->rotation.d[1] = -rotate->rotation.d[4];
2360 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002361 } else {
2362 weston_matrix_init(&surface->rotation.rotation);
2363 weston_matrix_init(&rotate->rotation);
2364 }
2365
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002366 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2367 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002368}
2369
2370static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002371lower_fullscreen_layer(struct desktop_shell *shell)
2372{
2373 struct workspace *ws;
2374 struct weston_surface *surface, *prev;
2375
2376 ws = get_current_workspace(shell);
2377 wl_list_for_each_reverse_safe(surface, prev,
2378 &shell->fullscreen_layer.surface_list,
2379 layer_link)
2380 weston_surface_restack(surface, &ws->layer.surface_list);
2381}
2382
2383static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002384activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002385 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002386{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002387 struct workspace *ws = get_current_workspace(shell);
2388 struct focus_state *state;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002389
Daniel Stone37816df2012-05-16 18:45:18 +01002390 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002391
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002392 wl_list_for_each(state, &ws->focus_list, link)
2393 if (state->seat == seat)
2394 break;
2395
2396 if (&state->link == &ws->focus_list) {
2397 state = focus_state_create(seat, ws);
2398 if (state == NULL)
2399 return;
2400 }
2401
2402 state->keyboard_focus = es;
2403 wl_list_remove(&state->surface_destroy_listener.link);
2404 wl_signal_add(&es->surface.resource.destroy_signal,
2405 &state->surface_destroy_listener);
2406
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002407 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002408 case SHELL_SURFACE_FULLSCREEN:
2409 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002410 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002411 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002412 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002413 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002414 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002415 lower_fullscreen_layer(shell);
2416 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002417 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002418 }
2419}
2420
Alex Wu21858432012-04-01 20:13:08 +08002421/* no-op func for checking black surface */
2422static void
2423black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2424{
2425}
2426
2427static bool
2428is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2429{
2430 if (es->configure == black_surface_configure) {
2431 if (fs_surface)
2432 *fs_surface = (struct weston_surface *)es->private;
2433 return true;
2434 }
2435 return false;
2436}
2437
Kristian Høgsberg75840622011-09-06 13:48:16 -04002438static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002439click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002440 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002441{
Daniel Stone37816df2012-05-16 18:45:18 +01002442 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002443 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002444 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002445 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002446
Daniel Stone37816df2012-05-16 18:45:18 +01002447 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002448 if (!focus)
2449 return;
2450
Alex Wu21858432012-04-01 20:13:08 +08002451 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002452 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002453
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002454 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2455 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002456
Daniel Stone325fc2d2012-05-30 16:31:58 +01002457 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002458 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002459}
2460
2461static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002462lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002463{
Tiago Vignattibe143262012-04-16 17:31:41 +03002464 struct desktop_shell *shell =
2465 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002466 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002467 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002468
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002469 if (shell->locked) {
2470 wl_list_for_each(output, &shell->compositor->output_list, link)
2471 /* TODO: find a way to jump to other DPMS levels */
2472 if (output->set_dpms)
2473 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002474 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002475 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002476
2477 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002478
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002479 /* Hide all surfaces by removing the fullscreen, panel and
2480 * toplevel layers. This way nothing else can show or receive
2481 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002482
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002483 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002484 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002485 if (shell->showing_input_panels)
2486 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002487 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002488 wl_list_insert(&shell->compositor->cursor_layer.link,
2489 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002490
Pekka Paalanen77346a62011-11-30 16:26:35 +02002491 launch_screensaver(shell);
2492
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002493 /* TODO: disable bindings that should not work while locked. */
2494
2495 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002496}
2497
2498static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002499unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002500{
Tiago Vignattibe143262012-04-16 17:31:41 +03002501 struct desktop_shell *shell =
2502 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002503
Pekka Paalanend81c2162011-11-16 13:47:34 +02002504 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002505 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002506 return;
2507 }
2508
2509 /* If desktop-shell client has gone away, unlock immediately. */
2510 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002511 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002512 return;
2513 }
2514
2515 if (shell->prepare_event_sent)
2516 return;
2517
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002518 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002519 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002520}
2521
2522static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002523show_input_panels(struct wl_listener *listener, void *data)
2524{
2525 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002526 container_of(listener, struct desktop_shell,
2527 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002528 struct input_panel_surface *surface, *next;
2529 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002530
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002531 shell->showing_input_panels = true;
2532
2533 wl_list_insert(&shell->panel_layer.link,
2534 &shell->input_panel_layer.link);
2535
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002536 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002537 &shell->input_panel.surfaces, link) {
2538 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002539 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002540 &ws->layer_link);
2541 weston_surface_assign_output(ws);
2542 weston_surface_damage(ws);
2543 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002544 }
2545}
2546
2547static void
2548hide_input_panels(struct wl_listener *listener, void *data)
2549{
2550 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002551 container_of(listener, struct desktop_shell,
2552 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002553 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002554
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002555 shell->showing_input_panels = false;
2556
2557 wl_list_remove(&shell->input_panel_layer.link);
2558
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002559 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002560 &shell->input_panel_layer.surface_list, layer_link)
2561 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002562}
2563
2564static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002565center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002566{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002567 struct weston_mode *mode = output->current;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002568 GLfloat x = (mode->width - surface->buffer->width) / 2;
2569 GLfloat y = (mode->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002570
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002571 weston_surface_configure(surface, output->x + x, output->y + y,
2572 surface->buffer->width,
2573 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002574}
2575
2576static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002577map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002578 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002579{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002580 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002581 struct shell_surface *shsurf = get_shell_surface(surface);
2582 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002583 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002584 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002585 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002586 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002587
Pekka Paalanen60921e52012-01-25 15:55:43 +02002588 surface->geometry.width = width;
2589 surface->geometry.height = height;
2590 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002591
2592 /* initial positioning, see also configure() */
2593 switch (surface_type) {
2594 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002595 weston_surface_set_position(surface, 10 + random() % 400,
2596 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002597 break;
Alex Wu4539b082012-03-01 12:57:46 +08002598 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002599 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002600 shell_map_fullscreen(shsurf);
2601 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002602 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002603 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002604 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002605 weston_surface_set_position(surface, shsurf->output->x,
2606 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002607 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002608 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002609 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002610 case SHELL_SURFACE_NONE:
2611 weston_surface_set_position(surface,
2612 surface->geometry.x + sx,
2613 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002614 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002615 default:
2616 ;
2617 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002618
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002619 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002620 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002621 case SHELL_SURFACE_POPUP:
2622 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002623 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002624 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2625 break;
Alex Wu4539b082012-03-01 12:57:46 +08002626 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002627 case SHELL_SURFACE_NONE:
2628 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002629 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002630 ws = get_current_workspace(shell);
2631 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002632 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002633 }
2634
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002635 if (surface_type != SHELL_SURFACE_NONE) {
2636 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002637 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2638 surface->output = shsurf->output;
2639 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002640
Juan Zhao7bb92f02011-12-15 11:31:51 -05002641 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002642 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002643 if (shsurf->transient.flags ==
2644 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2645 break;
2646 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002647 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002648 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002649 if (!shell->locked) {
2650 wl_list_for_each(seat, &compositor->seat_list, link)
2651 activate(shell, surface, seat);
2652 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002653 break;
2654 default:
2655 break;
2656 }
2657
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002658 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002659 {
2660 switch (shell->win_animation_type) {
2661 case ANIMATION_FADE:
2662 weston_fade_run(surface, NULL, NULL);
2663 break;
2664 case ANIMATION_ZOOM:
2665 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2666 break;
2667 default:
2668 break;
2669 }
2670 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002671}
2672
2673static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002674configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002675 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002676{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002677 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2678 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002679
Pekka Paalanen77346a62011-11-30 16:26:35 +02002680 shsurf = get_shell_surface(surface);
2681 if (shsurf)
2682 surface_type = shsurf->type;
2683
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002684 surface->geometry.x = x;
2685 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002686 surface->geometry.width = width;
2687 surface->geometry.height = height;
2688 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002689
2690 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002691 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002692 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002693 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002694 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002695 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002696 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002697 surface->geometry.x = surface->output->x;
2698 surface->geometry.y = surface->output->y +
2699 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002700 break;
Alex Wu4539b082012-03-01 12:57:46 +08002701 case SHELL_SURFACE_TOPLEVEL:
2702 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002703 default:
2704 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002705 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002706
Alex Wu4539b082012-03-01 12:57:46 +08002707 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002708 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002709 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002710
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002711 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08002712 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002713 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002714}
2715
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002716static void
2717shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2718{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002719 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002720 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002721 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002722
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002723 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002724 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002725 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002726 type_changed = 1;
2727 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002728
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002729 if (!weston_surface_is_mapped(es)) {
2730 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002731 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002732 es->geometry.width != es->buffer->width ||
2733 es->geometry.height != es->buffer->height) {
2734 GLfloat from_x, from_y;
2735 GLfloat to_x, to_y;
2736
2737 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2738 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2739 configure(shell, es,
2740 es->geometry.x + to_x - from_x,
2741 es->geometry.y + to_y - from_y,
2742 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002743 }
2744}
2745
Tiago Vignattibe143262012-04-16 17:31:41 +03002746static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002747
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002748static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002749desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002750{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002751 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002752 struct desktop_shell *shell =
2753 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002754
2755 shell->child.process.pid = 0;
2756 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002757
2758 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2759 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002760 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002761 shell->child.deathstamp = time;
2762 shell->child.deathcount = 0;
2763 }
2764
2765 shell->child.deathcount++;
2766 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002767 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002768 return;
2769 }
2770
Martin Minarik6d118362012-06-07 18:01:59 +02002771 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002772 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002773}
2774
2775static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002776launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002777{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002778 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002779
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002780 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002781 &shell->child.process,
2782 shell_exe,
2783 desktop_shell_sigchld);
2784
2785 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002786 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002787 return 0;
2788}
2789
2790static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002791bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2792{
Tiago Vignattibe143262012-04-16 17:31:41 +03002793 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002794
2795 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002796 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002797}
2798
Kristian Høgsberg75840622011-09-06 13:48:16 -04002799static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002800unbind_desktop_shell(struct wl_resource *resource)
2801{
Tiago Vignattibe143262012-04-16 17:31:41 +03002802 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002803
2804 if (shell->locked)
2805 resume_desktop(shell);
2806
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002807 shell->child.desktop_shell = NULL;
2808 shell->prepare_event_sent = false;
2809 free(resource);
2810}
2811
2812static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002813bind_desktop_shell(struct wl_client *client,
2814 void *data, uint32_t version, uint32_t id)
2815{
Tiago Vignattibe143262012-04-16 17:31:41 +03002816 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002817 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002818
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002819 resource = wl_client_add_object(client, &desktop_shell_interface,
2820 &desktop_shell_implementation,
2821 id, shell);
2822
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002823 if (client == shell->child.client) {
2824 resource->destroy = unbind_desktop_shell;
2825 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002826 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002827 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002828
2829 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2830 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002831 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002832}
2833
Pekka Paalanen6e168112011-11-24 11:34:05 +02002834static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002835screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2836{
2837 struct desktop_shell *shell = surface->private;
2838
2839 if (!shell->locked)
2840 return;
2841
2842 center_on_output(surface, surface->output);
2843
2844 if (wl_list_empty(&surface->layer_link)) {
2845 wl_list_insert(shell->lock_layer.surface_list.prev,
2846 &surface->layer_link);
2847 weston_surface_assign_output(surface);
2848 shell->compositor->idle_time = shell->screensaver.duration;
2849 weston_compositor_wake(shell->compositor);
2850 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2851 }
2852}
2853
2854static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02002855screensaver_set_surface(struct wl_client *client,
2856 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002857 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02002858 struct wl_resource *output_resource)
2859{
Tiago Vignattibe143262012-04-16 17:31:41 +03002860 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002861 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002862 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002863
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002864 surface->configure = screensaver_configure;
2865 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002866 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002867}
2868
2869static const struct screensaver_interface screensaver_implementation = {
2870 screensaver_set_surface
2871};
2872
2873static void
2874unbind_screensaver(struct wl_resource *resource)
2875{
Tiago Vignattibe143262012-04-16 17:31:41 +03002876 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002877
Pekka Paalanen77346a62011-11-30 16:26:35 +02002878 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002879 free(resource);
2880}
2881
2882static void
2883bind_screensaver(struct wl_client *client,
2884 void *data, uint32_t version, uint32_t id)
2885{
Tiago Vignattibe143262012-04-16 17:31:41 +03002886 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002887 struct wl_resource *resource;
2888
2889 resource = wl_client_add_object(client, &screensaver_interface,
2890 &screensaver_implementation,
2891 id, shell);
2892
Pekka Paalanen77346a62011-11-30 16:26:35 +02002893 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002894 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002895 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002896 return;
2897 }
2898
2899 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2900 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002901 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002902}
2903
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002904static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002905input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2906{
2907 struct weston_mode *mode = surface->output->current;
2908 GLfloat x = (mode->width - surface->buffer->width) / 2;
2909 GLfloat y = mode->height - surface->buffer->height;
2910
2911 /* Don't map the input panel here, wait for
2912 * show_input_panels signal. */
2913
2914 weston_surface_configure(surface,
2915 surface->output->x + x,
2916 surface->output->y + y,
2917 surface->buffer->width,
2918 surface->buffer->height);
2919}
2920
2921static void
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002922destroy_input_panel_surface(struct wl_listener *listener,
2923 void *data)
2924{
2925 struct input_panel_surface *input_panel_surface =
2926 container_of(listener, struct input_panel_surface, listener);
2927
2928 wl_list_remove(&listener->link);
2929 wl_list_remove(&input_panel_surface->link);
2930
2931 free(input_panel_surface);
2932}
2933
2934static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002935input_panel_set_surface(struct wl_client *client,
2936 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002937 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002938 struct wl_resource *output_resource)
2939{
2940 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002941 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002942 struct weston_output *output = output_resource->data;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002943 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002944
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002945 surface->configure = input_panel_configure;
2946 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002947 surface->output = output;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002948
2949 input_panel_surface = malloc(sizeof *input_panel_surface);
2950 if (!input_panel_surface) {
2951 wl_resource_post_no_memory(resource);
2952 return;
2953 }
2954
2955 input_panel_surface->surface = surface;
2956 input_panel_surface->listener.notify = destroy_input_panel_surface;
2957
2958 wl_signal_add(&surface_resource->destroy_signal,
2959 &input_panel_surface->listener);
2960
2961 wl_list_insert(&shell->input_panel.surfaces,
2962 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002963}
2964
2965static const struct input_panel_interface input_panel_implementation = {
2966 input_panel_set_surface
2967};
2968
2969static void
2970unbind_input_panel(struct wl_resource *resource)
2971{
2972 struct desktop_shell *shell = resource->data;
2973
2974 shell->input_panel.binding = NULL;
2975 free(resource);
2976}
2977
2978static void
2979bind_input_panel(struct wl_client *client,
2980 void *data, uint32_t version, uint32_t id)
2981{
2982 struct desktop_shell *shell = data;
2983 struct wl_resource *resource;
2984
2985 resource = wl_client_add_object(client, &input_panel_interface,
2986 &input_panel_implementation,
2987 id, shell);
2988
2989 if (shell->input_panel.binding == NULL) {
2990 resource->destroy = unbind_input_panel;
2991 shell->input_panel.binding = resource;
2992 return;
2993 }
2994
2995 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2996 "interface object already bound");
2997 wl_resource_destroy(resource);
2998}
2999
Kristian Høgsberg07045392012-02-19 18:52:44 -05003000struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003001 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003002 struct weston_surface *current;
3003 struct wl_listener listener;
3004 struct wl_keyboard_grab grab;
3005};
3006
3007static void
3008switcher_next(struct switcher *switcher)
3009{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003010 struct weston_surface *surface;
3011 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003012 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003013 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003014
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003015 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003016 switch (get_shell_surface_type(surface)) {
3017 case SHELL_SURFACE_TOPLEVEL:
3018 case SHELL_SURFACE_FULLSCREEN:
3019 case SHELL_SURFACE_MAXIMIZED:
3020 if (first == NULL)
3021 first = surface;
3022 if (prev == switcher->current)
3023 next = surface;
3024 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003025 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003026 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003027 weston_surface_damage(surface);
3028 break;
3029 default:
3030 break;
3031 }
Alex Wu1659daa2012-04-01 20:13:09 +08003032
3033 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003034 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003035 surface->geometry.dirty = 1;
3036 weston_surface_damage(surface);
3037 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003038 }
3039
3040 if (next == NULL)
3041 next = first;
3042
Alex Wu07b26062012-03-12 16:06:01 +08003043 if (next == NULL)
3044 return;
3045
Kristian Høgsberg07045392012-02-19 18:52:44 -05003046 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003047 wl_signal_add(&next->surface.resource.destroy_signal,
3048 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003049
3050 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003051 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003052
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003053 shsurf = get_shell_surface(switcher->current);
3054 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003055 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003056}
3057
3058static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003059switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003060{
3061 struct switcher *switcher =
3062 container_of(listener, struct switcher, listener);
3063
3064 switcher_next(switcher);
3065}
3066
3067static void
Daniel Stone351eb612012-05-31 15:27:47 -04003068switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003069{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003070 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003071 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003072 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003073
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003074 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003075 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003076 weston_surface_damage(surface);
3077 }
3078
Alex Wu07b26062012-03-12 16:06:01 +08003079 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003080 activate(switcher->shell, switcher->current,
3081 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003082 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003083 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003084 free(switcher);
3085}
3086
3087static void
3088switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003089 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003090{
3091 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003092 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003093
Daniel Stonec9785ea2012-05-30 16:31:52 +01003094 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003095 switcher_next(switcher);
3096}
3097
3098static void
3099switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3100 uint32_t mods_depressed, uint32_t mods_latched,
3101 uint32_t mods_locked, uint32_t group)
3102{
3103 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003104 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003105
Daniel Stone351eb612012-05-31 15:27:47 -04003106 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3107 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003108}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003109
3110static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003111 switcher_key,
3112 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003113};
3114
3115static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003116switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3117 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003118{
Tiago Vignattibe143262012-04-16 17:31:41 +03003119 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003120 struct switcher *switcher;
3121
3122 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003123 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003124 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003125 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003126 wl_list_init(&switcher->listener.link);
3127
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003128 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003129 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003130 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3131 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003132 switcher_next(switcher);
3133}
3134
Pekka Paalanen3c647232011-12-22 13:43:43 +02003135static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003136backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3137 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003138{
3139 struct weston_compositor *compositor = data;
3140 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003141 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003142
3143 /* TODO: we're limiting to simple use cases, where we assume just
3144 * control on the primary display. We'd have to extend later if we
3145 * ever get support for setting backlights on random desktop LCD
3146 * panels though */
3147 output = get_default_output(compositor);
3148 if (!output)
3149 return;
3150
3151 if (!output->set_backlight)
3152 return;
3153
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003154 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3155 backlight_new = output->backlight_current - 25;
3156 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3157 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003158
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003159 if (backlight_new < 5)
3160 backlight_new = 5;
3161 if (backlight_new > 255)
3162 backlight_new = 255;
3163
3164 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003165 output->set_backlight(output, output->backlight_current);
3166}
3167
3168static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003169debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3170 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003171{
Tiago Vignattibe143262012-04-16 17:31:41 +03003172 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003173 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003174 struct weston_surface *surface;
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003175 struct weston_plane plane;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003176
3177 if (shell->debug_repaint_surface) {
3178 weston_surface_destroy(shell->debug_repaint_surface);
3179 shell->debug_repaint_surface = NULL;
3180 } else {
3181 surface = weston_surface_create(compositor);
3182 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3183 weston_surface_configure(surface, 0, 0, 8192, 8192);
3184 wl_list_insert(&compositor->fade_layer.surface_list,
3185 &surface->layer_link);
3186 weston_surface_assign_output(surface);
3187 pixman_region32_init(&surface->input);
3188
3189 /* Here's the dirty little trick that makes the
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003190 * repaint debugging work: we move the surface to a
3191 * different plane and force an update_transform to
3192 * update dependent state and clear the
3193 * geometry.dirty bit. This way the call to
3194 * damage_below() in update_transform() does not
3195 * add damage to the primary plane. */
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003196
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003197 weston_plane_init(&plane, 0, 0);
3198 surface->plane = &plane;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003199 weston_surface_update_transform(surface);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003200 shell->debug_repaint_surface = surface;
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003201 surface->plane = &compositor->primary_plane;
3202 weston_plane_release(&plane);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003203 }
3204}
3205
3206static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003207force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3208 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003209{
3210 struct wl_client *client;
3211 pid_t pid;
3212 uid_t uid;
3213 gid_t gid;
3214
Daniel Stone325fc2d2012-05-30 16:31:58 +01003215 client = seat->keyboard->focus->resource.client;
3216 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003217
Daniel Stone325fc2d2012-05-30 16:31:58 +01003218 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003219}
3220
3221static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003222workspace_up_binding(struct wl_seat *seat, uint32_t time,
3223 uint32_t key, void *data)
3224{
3225 struct desktop_shell *shell = data;
3226 unsigned int new_index = shell->workspaces.current;
3227
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003228 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003229 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003230 if (new_index != 0)
3231 new_index--;
3232
3233 change_workspace(shell, new_index);
3234}
3235
3236static void
3237workspace_down_binding(struct wl_seat *seat, uint32_t time,
3238 uint32_t key, void *data)
3239{
3240 struct desktop_shell *shell = data;
3241 unsigned int new_index = shell->workspaces.current;
3242
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003243 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003244 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003245 if (new_index < shell->workspaces.num - 1)
3246 new_index++;
3247
3248 change_workspace(shell, new_index);
3249}
3250
3251static void
3252workspace_f_binding(struct wl_seat *seat, uint32_t time,
3253 uint32_t key, void *data)
3254{
3255 struct desktop_shell *shell = data;
3256 unsigned int new_index;
3257
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003258 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003259 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003260 new_index = key - KEY_F1;
3261 if (new_index >= shell->workspaces.num)
3262 new_index = shell->workspaces.num - 1;
3263
3264 change_workspace(shell, new_index);
3265}
3266
3267
3268static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003269shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003270{
Tiago Vignattibe143262012-04-16 17:31:41 +03003271 struct desktop_shell *shell =
3272 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003273 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003274
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003275 if (shell->child.client)
3276 wl_client_destroy(shell->child.client);
3277
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003278 wl_list_remove(&shell->lock_listener.link);
3279 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003280 wl_list_remove(&shell->show_input_panel_listener.link);
3281 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003282
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003283 wl_array_for_each(ws, &shell->workspaces.array)
3284 workspace_destroy(*ws);
3285 wl_array_release(&shell->workspaces.array);
3286
Pekka Paalanen3c647232011-12-22 13:43:43 +02003287 free(shell->screensaver.path);
3288 free(shell);
3289}
3290
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003291static void
3292shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3293{
3294 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003295 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003296
3297 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003298 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3299 MODIFIER_CTRL | MODIFIER_ALT,
3300 terminate_binding, ec);
3301 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3302 click_to_activate_binding,
3303 shell);
3304 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3305 MODIFIER_SUPER | MODIFIER_ALT,
3306 surface_opacity_binding, NULL);
3307 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3308 MODIFIER_SUPER, zoom_axis_binding,
3309 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003310
3311 /* configurable bindings */
3312 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003313 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3314 zoom_key_binding, NULL);
3315 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3316 zoom_key_binding, NULL);
3317 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3318 shell);
3319 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3320 resize_binding, shell);
3321 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3322 rotate_binding, NULL);
3323 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3324 shell);
3325 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3326 ec);
3327 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3328 backlight_binding, ec);
3329 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3330 ec);
3331 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3332 backlight_binding, ec);
Kristian Høgsberg73694c82012-06-28 14:13:10 -04003333 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003334 debug_repaint_binding, shell);
3335 weston_compositor_add_key_binding(ec, KEY_K, mod,
3336 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003337 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3338 workspace_up_binding, shell);
3339 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3340 workspace_down_binding, shell);
3341
3342 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3343 if (shell->workspaces.num > 1) {
3344 num_workspace_bindings = shell->workspaces.num;
3345 if (num_workspace_bindings > 6)
3346 num_workspace_bindings = 6;
3347 for (i = 0; i < num_workspace_bindings; i++)
3348 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3349 workspace_f_binding,
3350 shell);
3351 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003352}
3353
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003354int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003355shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003356
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003357WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003358shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003359{
Tiago Vignattibe143262012-04-16 17:31:41 +03003360 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003361 struct workspace **pws;
3362 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003363
3364 shell = malloc(sizeof *shell);
3365 if (shell == NULL)
3366 return -1;
3367
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003368 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003369 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003370
3371 shell->destroy_listener.notify = shell_destroy;
3372 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3373 shell->lock_listener.notify = lock;
3374 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3375 shell->unlock_listener.notify = unlock;
3376 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003377 shell->show_input_panel_listener.notify = show_input_panels;
3378 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3379 shell->hide_input_panel_listener.notify = hide_input_panels;
3380 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003381 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003382 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003383 ec->shell_interface.create_shell_surface = create_shell_surface;
3384 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003385 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003386 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003387 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003388
Pekka Paalanen77346a62011-11-30 16:26:35 +02003389 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003390 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003391
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003392 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3393 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003394 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3395 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003396 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003397
3398 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003399
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003400 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003401
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003402 for (i = 0; i < shell->workspaces.num; i++) {
3403 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3404 if (pws == NULL)
3405 return -1;
3406
3407 *pws = workspace_create();
3408 if (*pws == NULL)
3409 return -1;
3410 }
3411 activate_workspace(shell, 0);
3412
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003413 wl_list_init(&shell->workspaces.animation.link);
3414 shell->workspaces.animation.frame = animate_workspace_change_frame;
3415
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003416 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3417 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003418 return -1;
3419
Kristian Høgsberg75840622011-09-06 13:48:16 -04003420 if (wl_display_add_global(ec->wl_display,
3421 &desktop_shell_interface,
3422 shell, bind_desktop_shell) == NULL)
3423 return -1;
3424
Pekka Paalanen6e168112011-11-24 11:34:05 +02003425 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3426 shell, bind_screensaver) == NULL)
3427 return -1;
3428
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003429 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3430 shell, bind_input_panel) == NULL)
3431 return -1;
3432
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003433 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003434 if (launch_desktop_shell_process(shell) != 0)
3435 return -1;
3436
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003437 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003438 if (ec->seat->seat.pointer)
3439 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3440 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003441
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003442 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003443
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003444 return 0;
3445}