blob: 5312709438b4fa451cd21305483993f541ffa885 [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;
85
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040086 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030087 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040088
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020089 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050090 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020091 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020092 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020093
94 unsigned deathcount;
95 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020096 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020097
98 bool locked;
99 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200100
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400101 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500102 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100103
Pekka Paalanen77346a62011-11-30 16:26:35 +0200104 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200105 struct wl_array array;
106 unsigned int current;
107 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200108
109 struct weston_animation animation;
110 int anim_dir;
111 uint32_t anim_timestamp;
112 double anim_current;
113 struct workspace *anim_from;
114 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200115 } workspaces;
116
117 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200118 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200119 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200120 struct wl_resource *binding;
121 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500122 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200123 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500124
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200125 struct {
126 struct wl_resource *binding;
127 struct wl_list surfaces;
128 } input_panel;
129
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300130 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800131 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500132 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400133};
134
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500135enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200136 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500137 SHELL_SURFACE_TOPLEVEL,
138 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500139 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800140 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500141 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200142};
143
Scott Moreauff1db4a2012-04-17 19:06:18 -0600144struct ping_timer {
145 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600146 uint32_t serial;
147};
148
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200149struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200150 struct wl_resource resource;
151
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500152 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200153 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400154 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300155 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200156
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400157 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400158 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500159 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800160 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800161 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600162 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100163
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500164 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200165 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500166 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200167 } rotation;
168
169 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700170 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500171 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200172 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500173 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100174 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400175 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500176 } popup;
177
Alex Wu4539b082012-03-01 12:57:46 +0800178 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300179 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400180 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300181 } transient;
182
183 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800184 enum wl_shell_surface_fullscreen_method type;
185 struct weston_transform transform; /* matrix from x, y */
186 uint32_t framerate;
187 struct weston_surface *black_surface;
188 } fullscreen;
189
Scott Moreauff1db4a2012-04-17 19:06:18 -0600190 struct ping_timer *ping_timer;
191
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200192 struct weston_transform workspace_transform;
193
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500194 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500195 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100196 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400197
198 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200199};
200
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300201struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700202 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300203 struct shell_surface *shsurf;
204 struct wl_listener shsurf_destroy_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300205 struct wl_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300206};
207
208struct weston_move_grab {
209 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100210 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500211};
212
Pekka Paalanen460099f2012-01-20 16:48:25 +0200213struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300214 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500215 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200216 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400217 GLfloat x;
218 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200219 } center;
220};
221
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400222static void
223activate(struct desktop_shell *shell, struct weston_surface *es,
224 struct weston_seat *seat);
225
226static struct workspace *
227get_current_workspace(struct desktop_shell *shell);
228
Alex Wubd3354b2012-04-17 17:20:49 +0800229static struct shell_surface *
230get_shell_surface(struct weston_surface *surface);
231
232static struct desktop_shell *
233shell_surface_get_shell(struct shell_surface *shsurf);
234
235static bool
236shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
237{
238 struct desktop_shell *shell;
239 struct weston_surface *top_fs_es;
240
241 shell = shell_surface_get_shell(shsurf);
242
243 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
244 return false;
245
246 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
247 struct weston_surface,
248 layer_link);
249 return (shsurf == get_shell_surface(top_fs_es));
250}
251
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500252static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400253destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300254{
255 struct shell_grab *grab;
256
257 grab = container_of(listener, struct shell_grab,
258 shsurf_destroy_listener);
259
260 grab->shsurf = NULL;
261}
262
263static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300264shell_grab_start(struct shell_grab *grab,
265 const struct wl_pointer_grab_interface *interface,
266 struct shell_surface *shsurf,
267 struct wl_pointer *pointer,
268 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300269{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300270 struct desktop_shell *shell = shsurf->shell;
271
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300272 grab->grab.interface = interface;
273 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400274 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
275 wl_signal_add(&shsurf->resource.destroy_signal,
276 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300277
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300278 grab->pointer = pointer;
279 grab->grab.focus = &shsurf->surface->surface;
280
281 wl_pointer_start_grab(pointer, &grab->grab);
282 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
283 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
284 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300285}
286
287static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300288shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300289{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400290 if (grab->shsurf)
291 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300292
293 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300294}
295
296static void
Alex Wu4539b082012-03-01 12:57:46 +0800297center_on_output(struct weston_surface *surface,
298 struct weston_output *output);
299
Daniel Stone496ca172012-05-30 16:31:42 +0100300static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300301get_modifier(char *modifier)
302{
303 if (!modifier)
304 return MODIFIER_SUPER;
305
306 if (!strcmp("ctrl", modifier))
307 return MODIFIER_CTRL;
308 else if (!strcmp("alt", modifier))
309 return MODIFIER_ALT;
310 else if (!strcmp("super", modifier))
311 return MODIFIER_SUPER;
312 else
313 return MODIFIER_SUPER;
314}
315
Juan Zhaoe10d2792012-04-25 19:09:52 +0800316static enum animation_type
317get_animation_type(char *animation)
318{
319 if (!animation)
320 return ANIMATION_NONE;
321
322 if (!strcmp("zoom", animation))
323 return ANIMATION_ZOOM;
324 else if (!strcmp("fade", animation))
325 return ANIMATION_FADE;
326 else
327 return ANIMATION_NONE;
328}
329
Alex Wu4539b082012-03-01 12:57:46 +0800330static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300331shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200332{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200333 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200334 char *path = NULL;
335 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200336 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300337 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800338 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200339
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400340 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300341 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800342 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200343 { "num-workspaces",
344 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200345 };
346
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400347 struct config_key saver_keys[] = {
348 { "path", CONFIG_KEY_STRING, &path },
349 { "duration", CONFIG_KEY_INTEGER, &duration },
350 };
351
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200352 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400353 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200354 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
355 };
356
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200357 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500358 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200359 free(config_file);
360
Pekka Paalanen7296e792011-12-07 16:22:00 +0200361 shell->screensaver.path = path;
362 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300363 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800364 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200365 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
366}
367
368static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200369focus_state_destroy(struct focus_state *state)
370{
371 wl_list_remove(&state->seat_destroy_listener.link);
372 wl_list_remove(&state->surface_destroy_listener.link);
373 free(state);
374}
375
376static void
377focus_state_seat_destroy(struct wl_listener *listener, void *data)
378{
379 struct focus_state *state = container_of(listener,
380 struct focus_state,
381 seat_destroy_listener);
382
383 wl_list_remove(&state->link);
384 focus_state_destroy(state);
385}
386
387static void
388focus_state_surface_destroy(struct wl_listener *listener, void *data)
389{
390 struct focus_state *state = container_of(listener,
391 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400392 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400393 struct desktop_shell *shell;
394 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200395
Kristian Høgsberge3778222012-07-31 17:29:30 -0400396 next = NULL;
397 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
398 if (surface == state->keyboard_focus)
399 continue;
400
401 next = surface;
402 break;
403 }
404
405 if (next) {
406 shell = state->seat->compositor->shell_interface.shell;
407 activate(shell, next, state->seat);
408 } else {
409 wl_list_remove(&state->link);
410 focus_state_destroy(state);
411 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200412}
413
414static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400415focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200416{
Jonas Ådahl04769742012-06-13 00:01:24 +0200417 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200418
419 state = malloc(sizeof *state);
420 if (state == NULL)
421 return NULL;
422
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400423 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200424 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400425 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200426
427 state->seat_destroy_listener.notify = focus_state_seat_destroy;
428 state->surface_destroy_listener.notify = focus_state_surface_destroy;
429 wl_signal_add(&seat->seat.destroy_signal,
430 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400431 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200432
433 return state;
434}
435
436static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400437restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200438{
439 struct focus_state *state, *next;
440
441 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
442 if (state->keyboard_focus)
443 wl_keyboard_set_focus(state->seat->seat.keyboard,
444 &state->keyboard_focus->surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200445 }
446}
447
448static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200449workspace_destroy(struct workspace *ws)
450{
Jonas Ådahl04769742012-06-13 00:01:24 +0200451 struct focus_state *state, *next;
452
453 wl_list_for_each_safe(state, next, &ws->focus_list, link)
454 focus_state_destroy(state);
455
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200456 free(ws);
457}
458
Jonas Ådahl04769742012-06-13 00:01:24 +0200459static void
460seat_destroyed(struct wl_listener *listener, void *data)
461{
462 struct weston_seat *seat = data;
463 struct focus_state *state, *next;
464 struct workspace *ws = container_of(listener,
465 struct workspace,
466 seat_destroyed_listener);
467
468 wl_list_for_each_safe(state, next, &ws->focus_list, link)
469 if (state->seat == seat)
470 wl_list_remove(&state->link);
471}
472
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200473static struct workspace *
474workspace_create(void)
475{
476 struct workspace *ws = malloc(sizeof *ws);
477 if (ws == NULL)
478 return NULL;
479
480 weston_layer_init(&ws->layer, NULL);
481
Jonas Ådahl04769742012-06-13 00:01:24 +0200482 wl_list_init(&ws->focus_list);
483 wl_list_init(&ws->seat_destroyed_listener.link);
484 ws->seat_destroyed_listener.notify = seat_destroyed;
485
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200486 return ws;
487}
488
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200489static int
490workspace_is_empty(struct workspace *ws)
491{
492 return wl_list_empty(&ws->layer.surface_list);
493}
494
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200495static struct workspace *
496get_workspace(struct desktop_shell *shell, unsigned int index)
497{
498 struct workspace **pws = shell->workspaces.array.data;
499 pws += index;
500 return *pws;
501}
502
503static struct workspace *
504get_current_workspace(struct desktop_shell *shell)
505{
506 return get_workspace(shell, shell->workspaces.current);
507}
508
509static void
510activate_workspace(struct desktop_shell *shell, unsigned int index)
511{
512 struct workspace *ws;
513
514 ws = get_workspace(shell, index);
515 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
516
517 shell->workspaces.current = index;
518}
519
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200520static unsigned int
521get_output_height(struct weston_output *output)
522{
523 return abs(output->region.extents.y1 - output->region.extents.y2);
524}
525
526static void
527surface_translate(struct weston_surface *surface, double d)
528{
529 struct shell_surface *shsurf = get_shell_surface(surface);
530 struct weston_transform *transform;
531
532 transform = &shsurf->workspace_transform;
533 if (wl_list_empty(&transform->link))
534 wl_list_insert(surface->geometry.transformation_list.prev,
535 &shsurf->workspace_transform.link);
536
537 weston_matrix_init(&shsurf->workspace_transform.matrix);
538 weston_matrix_translate(&shsurf->workspace_transform.matrix,
539 0.0, d, 0.0);
540 surface->geometry.dirty = 1;
541}
542
543static void
544workspace_translate_out(struct workspace *ws, double fraction)
545{
546 struct weston_surface *surface;
547 unsigned int height;
548 double d;
549
550 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
551 height = get_output_height(surface->output);
552 d = height * fraction;
553
554 surface_translate(surface, d);
555 }
556}
557
558static void
559workspace_translate_in(struct workspace *ws, double fraction)
560{
561 struct weston_surface *surface;
562 unsigned int height;
563 double d;
564
565 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
566 height = get_output_height(surface->output);
567
568 if (fraction > 0)
569 d = -(height - height * fraction);
570 else
571 d = height + height * fraction;
572
573 surface_translate(surface, d);
574 }
575}
576
577static void
578workspace_damage_all_surfaces(struct workspace *ws)
579{
580 struct weston_surface *surface;
581
582 wl_list_for_each(surface, &ws->layer.surface_list, layer_link)
583 weston_surface_damage(surface);
584}
585
586static void
587reverse_workspace_change_animation(struct desktop_shell *shell,
588 unsigned int index,
589 struct workspace *from,
590 struct workspace *to)
591{
592 shell->workspaces.current = index;
593
594 shell->workspaces.anim_to = to;
595 shell->workspaces.anim_from = from;
596 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
597 shell->workspaces.anim_timestamp = 0;
598
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400599 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200600
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200601 workspace_damage_all_surfaces(from);
602 workspace_damage_all_surfaces(to);
603}
604
605static void
606workspace_deactivate_transforms(struct workspace *ws)
607{
608 struct weston_surface *surface;
609 struct shell_surface *shsurf;
610
611 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
612 shsurf = get_shell_surface(surface);
613 wl_list_remove(&shsurf->workspace_transform.link);
614 wl_list_init(&shsurf->workspace_transform.link);
615 shsurf->surface->geometry.dirty = 1;
616 }
617}
618
619static void
620finish_workspace_change_animation(struct desktop_shell *shell,
621 struct workspace *from,
622 struct workspace *to)
623{
624 workspace_damage_all_surfaces(from);
625 workspace_damage_all_surfaces(to);
626
627 wl_list_remove(&shell->workspaces.animation.link);
628 workspace_deactivate_transforms(from);
629 workspace_deactivate_transforms(to);
630 shell->workspaces.anim_to = NULL;
631
632 wl_list_remove(&shell->workspaces.anim_from->layer.link);
633}
634
635static void
636animate_workspace_change_frame(struct weston_animation *animation,
637 struct weston_output *output, uint32_t msecs)
638{
639 struct desktop_shell *shell =
640 container_of(animation, struct desktop_shell,
641 workspaces.animation);
642 struct workspace *from = shell->workspaces.anim_from;
643 struct workspace *to = shell->workspaces.anim_to;
644 uint32_t t;
645 double x, y;
646
647 if (workspace_is_empty(from) && workspace_is_empty(to)) {
648 finish_workspace_change_animation(shell, from, to);
649 return;
650 }
651
652 if (shell->workspaces.anim_timestamp == 0) {
653 if (shell->workspaces.anim_current == 0.0)
654 shell->workspaces.anim_timestamp = msecs;
655 else
656 shell->workspaces.anim_timestamp =
657 msecs -
658 /* Invers of movement function 'y' below. */
659 (asin(1.0 - shell->workspaces.anim_current) *
660 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
661 M_2_PI);
662 }
663
664 t = msecs - shell->workspaces.anim_timestamp;
665
666 /*
667 * x = [0, π/2]
668 * y(x) = sin(x)
669 */
670 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
671 y = sin(x);
672
673 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
674 workspace_damage_all_surfaces(from);
675 workspace_damage_all_surfaces(to);
676
677 workspace_translate_out(from, shell->workspaces.anim_dir * y);
678 workspace_translate_in(to, shell->workspaces.anim_dir * y);
679 shell->workspaces.anim_current = y;
680
681 workspace_damage_all_surfaces(from);
682 workspace_damage_all_surfaces(to);
683 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200684 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200685 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200686}
687
688static void
689animate_workspace_change(struct desktop_shell *shell,
690 unsigned int index,
691 struct workspace *from,
692 struct workspace *to)
693{
694 struct weston_output *output;
695
696 int dir;
697
698 if (index > shell->workspaces.current)
699 dir = -1;
700 else
701 dir = 1;
702
703 shell->workspaces.current = index;
704
705 shell->workspaces.anim_dir = dir;
706 shell->workspaces.anim_from = from;
707 shell->workspaces.anim_to = to;
708 shell->workspaces.anim_current = 0.0;
709 shell->workspaces.anim_timestamp = 0;
710
711 output = container_of(shell->compositor->output_list.next,
712 struct weston_output, link);
713 wl_list_insert(&output->animation_list,
714 &shell->workspaces.animation.link);
715
716 wl_list_insert(&from->layer.link, &to->layer.link);
717
718 workspace_translate_in(to, 0);
719
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400720 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200721
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200722 workspace_damage_all_surfaces(from);
723 workspace_damage_all_surfaces(to);
724}
725
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200726static void
727change_workspace(struct desktop_shell *shell, unsigned int index)
728{
729 struct workspace *from;
730 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200731
732 if (index == shell->workspaces.current)
733 return;
734
735 /* Don't change workspace when there is any fullscreen surfaces. */
736 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
737 return;
738
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200739 from = get_current_workspace(shell);
740 to = get_workspace(shell, index);
741
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200742 if (shell->workspaces.anim_from == to &&
743 shell->workspaces.anim_to == from) {
744 reverse_workspace_change_animation(shell, index, from, to);
745 return;
746 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200747
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200748 if (shell->workspaces.anim_to != NULL)
749 finish_workspace_change_animation(shell,
750 shell->workspaces.anim_from,
751 shell->workspaces.anim_to);
752
753 if (workspace_is_empty(to) && workspace_is_empty(from)) {
754 shell->workspaces.current = index;
755 wl_list_insert(&from->layer.link, &to->layer.link);
756 wl_list_remove(&from->layer.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200757
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400758 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200759 }
760 else
761 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200762}
763
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200764static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400765noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100766 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500767{
768 grab->focus = NULL;
769}
770
771static void
Scott Moreau447013d2012-02-18 05:05:29 -0700772move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100773 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500774{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500775 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100776 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300777 struct shell_surface *shsurf = move->base.shsurf;
778 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100779 int dx = wl_fixed_to_int(pointer->x + move->dx);
780 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300781
782 if (!shsurf)
783 return;
784
785 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500786
Daniel Stone103db7f2012-05-08 17:17:55 +0100787 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200788 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -0400789
790 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500791}
792
793static void
Scott Moreau447013d2012-02-18 05:05:29 -0700794move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100795 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500796{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300797 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
798 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100799 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100800 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500801
Daniel Stone4dbadb12012-05-30 16:31:51 +0100802 if (pointer->button_count == 0 &&
803 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300804 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500805 free(grab);
806 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500807}
808
Scott Moreau447013d2012-02-18 05:05:29 -0700809static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500810 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500811 move_grab_motion,
812 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500813};
814
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -0400815static int
816surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
817{
818 struct weston_move_grab *move;
819
820 if (!shsurf)
821 return -1;
822
823 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
824 return 0;
825
826 move = malloc(sizeof *move);
827 if (!move)
828 return -1;
829
830 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
831 ws->seat.pointer->grab_x;
832 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
833 ws->seat.pointer->grab_y;
834
835 shell_grab_start(&move->base, &move_grab_interface, shsurf,
836 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
837
838 return 0;
839}
840
841static void
842shell_surface_move(struct wl_client *client, struct wl_resource *resource,
843 struct wl_resource *seat_resource, uint32_t serial)
844{
845 struct weston_seat *ws = seat_resource->data;
846 struct shell_surface *shsurf = resource->data;
847
848 if (ws->seat.pointer->button_count == 0 ||
849 ws->seat.pointer->grab_serial != serial ||
850 ws->seat.pointer->focus != &shsurf->surface->surface)
851 return;
852
853 if (surface_move(shsurf, ws) < 0)
854 wl_resource_post_no_memory(resource);
855}
856
857struct weston_resize_grab {
858 struct shell_grab base;
859 uint32_t edges;
860 int32_t width, height;
861};
862
863static void
864resize_grab_motion(struct wl_pointer_grab *grab,
865 uint32_t time, wl_fixed_t x, wl_fixed_t y)
866{
867 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
868 struct wl_pointer *pointer = grab->pointer;
869 struct shell_surface *shsurf = resize->base.shsurf;
870 int32_t width, height;
871 wl_fixed_t from_x, from_y;
872 wl_fixed_t to_x, to_y;
873
874 if (!shsurf)
875 return;
876
877 weston_surface_from_global_fixed(shsurf->surface,
878 pointer->grab_x, pointer->grab_y,
879 &from_x, &from_y);
880 weston_surface_from_global_fixed(shsurf->surface,
881 pointer->x, pointer->y, &to_x, &to_y);
882
883 width = resize->width;
884 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
885 width += wl_fixed_to_int(from_x - to_x);
886 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
887 width += wl_fixed_to_int(to_x - from_x);
888 }
889
890 height = resize->height;
891 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
892 height += wl_fixed_to_int(from_y - to_y);
893 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
894 height += wl_fixed_to_int(to_y - from_y);
895 }
896
897 shsurf->client->send_configure(shsurf->surface,
898 resize->edges, width, height);
899}
900
901static void
902send_configure(struct weston_surface *surface,
903 uint32_t edges, int32_t width, int32_t height)
904{
905 struct shell_surface *shsurf = get_shell_surface(surface);
906
907 wl_shell_surface_send_configure(&shsurf->resource,
908 edges, width, height);
909}
910
911static const struct weston_shell_client shell_client = {
912 send_configure
913};
914
915static void
916resize_grab_button(struct wl_pointer_grab *grab,
917 uint32_t time, uint32_t button, uint32_t state_w)
918{
919 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
920 struct wl_pointer *pointer = grab->pointer;
921 enum wl_pointer_button_state state = state_w;
922
923 if (pointer->button_count == 0 &&
924 state == WL_POINTER_BUTTON_STATE_RELEASED) {
925 shell_grab_end(&resize->base);
926 free(grab);
927 }
928}
929
930static const struct wl_pointer_grab_interface resize_grab_interface = {
931 noop_grab_focus,
932 resize_grab_motion,
933 resize_grab_button,
934};
935
936static int
937surface_resize(struct shell_surface *shsurf,
938 struct weston_seat *ws, uint32_t edges)
939{
940 struct weston_resize_grab *resize;
941
942 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
943 return 0;
944
945 if (edges == 0 || edges > 15 ||
946 (edges & 3) == 3 || (edges & 12) == 12)
947 return 0;
948
949 resize = malloc(sizeof *resize);
950 if (!resize)
951 return -1;
952
953 resize->edges = edges;
954 resize->width = shsurf->surface->geometry.width;
955 resize->height = shsurf->surface->geometry.height;
956
957 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
958 ws->seat.pointer, edges);
959
960 return 0;
961}
962
963static void
964shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
965 struct wl_resource *seat_resource, uint32_t serial,
966 uint32_t edges)
967{
968 struct weston_seat *ws = seat_resource->data;
969 struct shell_surface *shsurf = resource->data;
970
971 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
972 return;
973
974 if (ws->seat.pointer->button_count == 0 ||
975 ws->seat.pointer->grab_serial != serial ||
976 ws->seat.pointer->focus != &shsurf->surface->surface)
977 return;
978
979 if (surface_resize(shsurf, ws, edges) < 0)
980 wl_resource_post_no_memory(resource);
981}
982
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600983static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400984busy_cursor_grab_focus(struct wl_pointer_grab *base,
985 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600986{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400987 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600988
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400989 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300990 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400991 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600992 }
993}
994
995static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400996busy_cursor_grab_motion(struct wl_pointer_grab *grab,
997 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600998{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400999}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001000
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001001static void
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001002busy_cursor_grab_button(struct wl_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001003 uint32_t time, uint32_t button, uint32_t state)
1004{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001005 struct shell_grab *grab = (struct shell_grab *) base;
1006 struct shell_surface *shsurf;
1007 struct weston_surface *surface =
1008 (struct weston_surface *) grab->grab.pointer->current;
1009 struct weston_seat *seat =
1010 (struct weston_seat *) grab->grab.pointer->seat;
1011
1012 shsurf = get_shell_surface(surface);
1013 if (shsurf && button == BTN_LEFT && state) {
1014 activate(shsurf->shell, shsurf->surface, seat);
1015 surface_move(shsurf, seat);
1016 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001017}
1018
1019static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1020 busy_cursor_grab_focus,
1021 busy_cursor_grab_motion,
1022 busy_cursor_grab_button,
1023};
1024
1025static void
1026set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1027{
1028 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001029
1030 grab = malloc(sizeof *grab);
1031 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001032 return;
1033
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001034 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1035 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001036}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001037
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001038static void
1039end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1040{
1041 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1042
1043 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001044 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001045 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001046 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001047}
1048
Scott Moreau9521d5e2012-04-19 13:06:17 -06001049static void
1050ping_timer_destroy(struct shell_surface *shsurf)
1051{
1052 if (!shsurf || !shsurf->ping_timer)
1053 return;
1054
1055 if (shsurf->ping_timer->source)
1056 wl_event_source_remove(shsurf->ping_timer->source);
1057
1058 free(shsurf->ping_timer);
1059 shsurf->ping_timer = NULL;
1060}
1061
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001062static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001063ping_timeout_handler(void *data)
1064{
1065 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001066 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001067
Scott Moreau9521d5e2012-04-19 13:06:17 -06001068 /* Client is not responding */
1069 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001070
1071 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1072 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1073 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001074
1075 return 1;
1076}
1077
1078static void
1079ping_handler(struct weston_surface *surface, uint32_t serial)
1080{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001081 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001082 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001083 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001084
1085 if (!shsurf)
1086 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001087 if (!shsurf->resource.client)
1088 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001089
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001090 if (shsurf->surface == shsurf->shell->grab_surface)
1091 return;
1092
Scott Moreauff1db4a2012-04-17 19:06:18 -06001093 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001094 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001095 if (!shsurf->ping_timer)
1096 return;
1097
1098 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001099 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1100 shsurf->ping_timer->source =
1101 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1102 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1103
1104 wl_shell_surface_send_ping(&shsurf->resource, serial);
1105 }
1106}
1107
1108static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001109handle_pointer_focus(struct wl_listener *listener, void *data)
1110{
1111 struct wl_pointer *pointer = data;
1112 struct weston_surface *surface =
1113 (struct weston_surface *) pointer->focus;
1114 struct weston_compositor *compositor;
1115 struct shell_surface *shsurf;
1116 uint32_t serial;
1117
1118 if (!surface)
1119 return;
1120
1121 compositor = surface->compositor;
1122 shsurf = get_shell_surface(surface);
1123
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001124 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001125 set_busy_cursor(shsurf, pointer);
1126 } else {
1127 serial = wl_display_next_serial(compositor->wl_display);
1128 ping_handler(surface, serial);
1129 }
1130}
1131
1132static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001133shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1134 uint32_t serial)
1135{
1136 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001137 struct desktop_shell *shell = shsurf->shell;
1138 struct weston_seat *seat;
1139 struct weston_compositor *ec = shsurf->surface->compositor;
1140 struct wl_pointer *pointer;
1141 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001142
Scott Moreauff1db4a2012-04-17 19:06:18 -06001143 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001144 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001145 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001146 if (was_unresponsive) {
1147 /* Received pong from previously unresponsive client */
1148 wl_list_for_each(seat, &ec->seat_list, link) {
1149 pointer = seat->seat.pointer;
1150 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001151 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001152 pointer->current ==
1153 &shsurf->surface->surface)
1154 end_busy_cursor(shsurf, pointer);
1155 }
1156 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001157 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001158 }
1159}
1160
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001161static void
1162shell_surface_set_title(struct wl_client *client,
1163 struct wl_resource *resource, const char *title)
1164{
1165 struct shell_surface *shsurf = resource->data;
1166
1167 free(shsurf->title);
1168 shsurf->title = strdup(title);
1169}
1170
1171static void
1172shell_surface_set_class(struct wl_client *client,
1173 struct wl_resource *resource, const char *class)
1174{
1175 struct shell_surface *shsurf = resource->data;
1176
1177 free(shsurf->class);
1178 shsurf->class = strdup(class);
1179}
1180
Juan Zhao96879df2012-02-07 08:45:41 +08001181static struct weston_output *
1182get_default_output(struct weston_compositor *compositor)
1183{
1184 return container_of(compositor->output_list.next,
1185 struct weston_output, link);
1186}
1187
Alex Wu4539b082012-03-01 12:57:46 +08001188static void
1189shell_unset_fullscreen(struct shell_surface *shsurf)
1190{
1191 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001192 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1193 shell_surface_is_top_fullscreen(shsurf)) {
1194 weston_output_switch_mode(shsurf->fullscreen_output,
1195 shsurf->fullscreen_output->origin);
1196 }
Alex Wu4539b082012-03-01 12:57:46 +08001197 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1198 shsurf->fullscreen.framerate = 0;
1199 wl_list_remove(&shsurf->fullscreen.transform.link);
1200 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001201 if (shsurf->fullscreen.black_surface)
1202 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001203 shsurf->fullscreen.black_surface = NULL;
1204 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001205 weston_surface_set_position(shsurf->surface,
1206 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001207 if (shsurf->saved_rotation_valid) {
1208 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1209 &shsurf->rotation.transform.link);
1210 shsurf->saved_rotation_valid = false;
1211 }
Alex Wu4539b082012-03-01 12:57:46 +08001212}
1213
Pekka Paalanen98262232011-12-01 10:42:22 +02001214static int
1215reset_shell_surface_type(struct shell_surface *surface)
1216{
1217 switch (surface->type) {
1218 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001219 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001220 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001221 case SHELL_SURFACE_MAXIMIZED:
1222 surface->output = get_default_output(surface->surface->compositor);
1223 weston_surface_set_position(surface->surface,
1224 surface->saved_x,
1225 surface->saved_y);
1226 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001227 case SHELL_SURFACE_NONE:
1228 case SHELL_SURFACE_TOPLEVEL:
1229 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001230 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001231 break;
1232 }
1233
1234 surface->type = SHELL_SURFACE_NONE;
1235 return 0;
1236}
1237
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001238static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001239set_surface_type(struct shell_surface *shsurf)
1240{
1241 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001242 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001243
1244 reset_shell_surface_type(shsurf);
1245
1246 shsurf->type = shsurf->next_type;
1247 shsurf->next_type = SHELL_SURFACE_NONE;
1248
1249 switch (shsurf->type) {
1250 case SHELL_SURFACE_TOPLEVEL:
1251 break;
1252 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001253 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001254 pes->geometry.x + shsurf->transient.x,
1255 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001256 break;
1257
1258 case SHELL_SURFACE_MAXIMIZED:
1259 shsurf->saved_x = surface->geometry.x;
1260 shsurf->saved_y = surface->geometry.y;
1261 shsurf->saved_position_valid = true;
1262 break;
1263
1264 case SHELL_SURFACE_FULLSCREEN:
1265 shsurf->saved_x = surface->geometry.x;
1266 shsurf->saved_y = surface->geometry.y;
1267 shsurf->saved_position_valid = true;
1268
1269 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1270 wl_list_remove(&shsurf->rotation.transform.link);
1271 wl_list_init(&shsurf->rotation.transform.link);
1272 shsurf->surface->geometry.dirty = 1;
1273 shsurf->saved_rotation_valid = true;
1274 }
1275 break;
1276
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001277 default:
1278 break;
1279 }
1280}
1281
1282static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001283set_toplevel(struct shell_surface *shsurf)
1284{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001285 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001286}
1287
1288static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001289shell_surface_set_toplevel(struct wl_client *client,
1290 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001291{
Pekka Paalanen98262232011-12-01 10:42:22 +02001292 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001293
Tiago Vignattibc052c92012-04-19 16:18:18 +03001294 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001295}
1296
1297static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001298set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001299 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001300{
1301 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001302 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001303 shsurf->transient.x = x;
1304 shsurf->transient.y = y;
1305 shsurf->transient.flags = flags;
1306 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1307}
1308
1309static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001310shell_surface_set_transient(struct wl_client *client,
1311 struct wl_resource *resource,
1312 struct wl_resource *parent_resource,
1313 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001314{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001315 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001316 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001317
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001318 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001319}
1320
Tiago Vignattibe143262012-04-16 17:31:41 +03001321static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001322shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001323{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001324 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001325}
1326
1327static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001328get_output_panel_height(struct desktop_shell *shell,
1329 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001330{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001331 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001332 int panel_height = 0;
1333
1334 if (!output)
1335 return 0;
1336
Juan Zhao4ab94682012-07-09 22:24:09 -07001337 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001338 if (surface->output == output) {
1339 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001340 break;
1341 }
1342 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001343
Juan Zhao96879df2012-02-07 08:45:41 +08001344 return panel_height;
1345}
1346
1347static void
1348shell_surface_set_maximized(struct wl_client *client,
1349 struct wl_resource *resource,
1350 struct wl_resource *output_resource )
1351{
1352 struct shell_surface *shsurf = resource->data;
1353 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001354 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001355 uint32_t edges = 0, panel_height = 0;
1356
1357 /* get the default output, if the client set it as NULL
1358 check whether the ouput is available */
1359 if (output_resource)
1360 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001361 else if (es->output)
1362 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001363 else
1364 shsurf->output = get_default_output(es->compositor);
1365
Tiago Vignattibe143262012-04-16 17:31:41 +03001366 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001367 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001368 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001369
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001370 shsurf->client->send_configure(shsurf->surface, edges,
Rob Bradford31b68622012-07-02 19:00:19 +01001371 shsurf->output->current->width,
1372 shsurf->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001373
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001374 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001375}
1376
Alex Wu21858432012-04-01 20:13:08 +08001377static void
1378black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1379
Alex Wu4539b082012-03-01 12:57:46 +08001380static struct weston_surface *
1381create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001382 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001383 GLfloat x, GLfloat y, int w, int h)
1384{
1385 struct weston_surface *surface = NULL;
1386
1387 surface = weston_surface_create(ec);
1388 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001389 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001390 return NULL;
1391 }
1392
Alex Wu21858432012-04-01 20:13:08 +08001393 surface->configure = black_surface_configure;
1394 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001395 weston_surface_configure(surface, x, y, w, h);
1396 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001397 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
1398
Alex Wu4539b082012-03-01 12:57:46 +08001399 return surface;
1400}
1401
1402/* Create black surface and append it to the associated fullscreen surface.
1403 * Handle size dismatch and positioning according to the method. */
1404static void
1405shell_configure_fullscreen(struct shell_surface *shsurf)
1406{
1407 struct weston_output *output = shsurf->fullscreen_output;
1408 struct weston_surface *surface = shsurf->surface;
1409 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001410 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001411
Alex Wu4539b082012-03-01 12:57:46 +08001412 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001413 shsurf->fullscreen.black_surface =
1414 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001415 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001416 output->x, output->y,
1417 output->current->width,
1418 output->current->height);
1419
1420 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1421 wl_list_insert(&surface->layer_link,
1422 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001423 shsurf->fullscreen.black_surface->output = output;
1424
1425 switch (shsurf->fullscreen.type) {
1426 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001427 if (surface->buffer)
1428 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001429 break;
1430 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1431 matrix = &shsurf->fullscreen.transform.matrix;
1432 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001433
1434 output_aspect = (float) output->current->width /
1435 (float) output->current->height;
1436 surface_aspect = (float) surface->geometry.width /
1437 (float) surface->geometry.height;
1438 if (output_aspect < surface_aspect)
1439 scale = (float) output->current->width /
1440 (float) surface->geometry.width;
1441 else
1442 scale = (float) output->current->height /
1443 (float) surface->geometry.height;
1444
Alex Wu4539b082012-03-01 12:57:46 +08001445 weston_matrix_scale(matrix, scale, scale, 1);
1446 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001447 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001448 &shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001449 x = output->x + (output->current->width - surface->geometry.width * scale) / 2;
1450 y = output->y + (output->current->height - surface->geometry.height * scale) / 2;
1451 weston_surface_set_position(surface, x, y);
1452
Alex Wu4539b082012-03-01 12:57:46 +08001453 break;
1454 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001455 if (shell_surface_is_top_fullscreen(shsurf)) {
1456 struct weston_mode mode = {0,
1457 surface->geometry.width,
1458 surface->geometry.height,
1459 shsurf->fullscreen.framerate};
1460
1461 if (weston_output_switch_mode(output, &mode) == 0) {
1462 weston_surface_configure(shsurf->fullscreen.black_surface,
1463 output->x, output->y,
1464 output->current->width,
1465 output->current->height);
1466 weston_surface_set_position(surface, output->x, output->y);
1467 break;
1468 }
1469 }
Alex Wu4539b082012-03-01 12:57:46 +08001470 break;
1471 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1472 break;
1473 default:
1474 break;
1475 }
1476}
1477
1478/* make the fullscreen and black surface at the top */
1479static void
1480shell_stack_fullscreen(struct shell_surface *shsurf)
1481{
Alex Wubd3354b2012-04-17 17:20:49 +08001482 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001483 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001484 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001485
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001486 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001487 wl_list_insert(&shell->fullscreen_layer.surface_list,
1488 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001489 weston_surface_damage(surface);
1490
1491 if (!shsurf->fullscreen.black_surface)
1492 shsurf->fullscreen.black_surface =
1493 create_black_surface(surface->compositor,
1494 surface,
1495 output->x, output->y,
1496 output->current->width,
1497 output->current->height);
1498
1499 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001500 wl_list_insert(&surface->layer_link,
1501 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001502 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001503}
1504
1505static void
1506shell_map_fullscreen(struct shell_surface *shsurf)
1507{
Alex Wu4539b082012-03-01 12:57:46 +08001508 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001509 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001510}
1511
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001512static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001513shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001514 struct wl_resource *resource,
1515 uint32_t method,
1516 uint32_t framerate,
1517 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001518{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001519 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001520 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001521
1522 if (output_resource)
1523 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001524 else if (es->output)
1525 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001526 else
1527 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001528
Alex Wu4539b082012-03-01 12:57:46 +08001529 shsurf->fullscreen_output = shsurf->output;
1530 shsurf->fullscreen.type = method;
1531 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001532 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001533
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001534 shsurf->client->send_configure(shsurf->surface, 0,
1535 shsurf->output->current->width,
1536 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001537}
1538
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001539static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001540popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001541 struct wl_surface *surface,
1542 wl_fixed_t x,
1543 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001544{
Daniel Stone37816df2012-05-16 18:45:18 +01001545 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001546 struct shell_surface *priv =
1547 container_of(grab, struct shell_surface, popup.grab);
1548 struct wl_client *client = priv->surface->surface.resource.client;
1549
Pekka Paalanencb108432012-01-19 16:25:40 +02001550 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001551 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001552 grab->focus = surface;
1553 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001554 wl_pointer_set_focus(pointer, NULL,
1555 wl_fixed_from_int(0),
1556 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001557 grab->focus = NULL;
1558 }
1559}
1560
1561static void
Scott Moreau447013d2012-02-18 05:05:29 -07001562popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001563 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001564{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001565 struct wl_resource *resource;
1566
Daniel Stone37816df2012-05-16 18:45:18 +01001567 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001568 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001569 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001570}
1571
1572static void
Scott Moreau447013d2012-02-18 05:05:29 -07001573popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001574 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001575{
1576 struct wl_resource *resource;
1577 struct shell_surface *shsurf =
1578 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001579 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001580 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001581 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001582
Daniel Stone37816df2012-05-16 18:45:18 +01001583 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001584 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001585 display = wl_client_get_display(resource->client);
1586 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001587 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001588 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001589 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001590 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001591 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001592 wl_pointer_end_grab(grab->pointer);
1593 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001594 }
1595
Daniel Stone4dbadb12012-05-30 16:31:51 +01001596 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001597 shsurf->popup.initial_up = 1;
1598}
1599
Scott Moreau447013d2012-02-18 05:05:29 -07001600static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001601 popup_grab_focus,
1602 popup_grab_motion,
1603 popup_grab_button,
1604};
1605
1606static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001607shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001608{
Daniel Stone37816df2012-05-16 18:45:18 +01001609 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001610 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001611 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001612
1613 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001614 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001615
Pekka Paalanen938269a2012-02-07 14:19:01 +02001616 weston_surface_update_transform(parent);
1617 if (parent->transform.enabled) {
1618 shsurf->popup.parent_transform.matrix =
1619 parent->transform.matrix;
1620 } else {
1621 /* construct x, y translation matrix */
1622 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1623 shsurf->popup.parent_transform.matrix.d[12] =
1624 parent->geometry.x;
1625 shsurf->popup.parent_transform.matrix.d[13] =
1626 parent->geometry.y;
1627 }
1628 wl_list_insert(es->geometry.transformation_list.prev,
1629 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001630 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001631
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001632 shsurf->popup.initial_up = 0;
1633
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001634 /* We don't require the grab to still be active, but if another
1635 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001636 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1637 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001638 } else {
1639 wl_shell_surface_send_popup_done(&shsurf->resource);
1640 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001641}
1642
1643static void
1644shell_surface_set_popup(struct wl_client *client,
1645 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001646 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001647 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001648 struct wl_resource *parent_resource,
1649 int32_t x, int32_t y, uint32_t flags)
1650{
1651 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001652
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001653 shsurf->type = SHELL_SURFACE_POPUP;
1654 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001655 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001656 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001657 shsurf->popup.x = x;
1658 shsurf->popup.y = y;
1659}
1660
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001661static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001662 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001663 shell_surface_move,
1664 shell_surface_resize,
1665 shell_surface_set_toplevel,
1666 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001667 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001668 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001669 shell_surface_set_maximized,
1670 shell_surface_set_title,
1671 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001672};
1673
1674static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001675destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001676{
Daniel Stone37816df2012-05-16 18:45:18 +01001677 if (shsurf->popup.grab.pointer)
1678 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001679
Alex Wubd3354b2012-04-17 17:20:49 +08001680 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1681 shell_surface_is_top_fullscreen(shsurf)) {
1682 weston_output_switch_mode(shsurf->fullscreen_output,
1683 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001684 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001685
Alex Wuaa08e2d2012-03-05 11:01:40 +08001686 if (shsurf->fullscreen.black_surface)
1687 weston_surface_destroy(shsurf->fullscreen.black_surface);
1688
Alex Wubd3354b2012-04-17 17:20:49 +08001689 /* As destroy_resource() use wl_list_for_each_safe(),
1690 * we can always remove the listener.
1691 */
1692 wl_list_remove(&shsurf->surface_destroy_listener.link);
1693 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001694 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001695
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001696 wl_list_remove(&shsurf->link);
1697 free(shsurf);
1698}
1699
1700static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001701shell_destroy_shell_surface(struct wl_resource *resource)
1702{
1703 struct shell_surface *shsurf = resource->data;
1704
1705 destroy_shell_surface(shsurf);
1706}
1707
1708static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001709shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001710{
1711 struct shell_surface *shsurf = container_of(listener,
1712 struct shell_surface,
1713 surface_destroy_listener);
1714
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001715 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001716 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001717 } else {
1718 wl_signal_emit(&shsurf->resource.destroy_signal,
1719 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001720 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001721 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001722}
1723
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001724static void
1725shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1726
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001727static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001728get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001729{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001730 if (surface->configure == shell_surface_configure)
1731 return surface->private;
1732 else
1733 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001734}
1735
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001736static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001737create_shell_surface(void *shell, struct weston_surface *surface,
1738 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001739{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001740 struct shell_surface *shsurf;
1741
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001742 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001743 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001744 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001745 }
1746
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001747 shsurf = calloc(1, sizeof *shsurf);
1748 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001749 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001750 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001751 }
1752
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001753 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001754 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001755
Tiago Vignattibc052c92012-04-19 16:18:18 +03001756 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001757 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001758 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001759 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001760 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001761 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1762 shsurf->fullscreen.framerate = 0;
1763 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001764 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001765 wl_list_init(&shsurf->fullscreen.transform.link);
1766
Tiago Vignattibc052c92012-04-19 16:18:18 +03001767 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001768 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1769 wl_signal_add(&surface->surface.resource.destroy_signal,
1770 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001771
1772 /* init link so its safe to always remove it in destroy_shell_surface */
1773 wl_list_init(&shsurf->link);
1774
Pekka Paalanen460099f2012-01-20 16:48:25 +02001775 /* empty when not in use */
1776 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001777 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001778
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001779 wl_list_init(&shsurf->workspace_transform.link);
1780
Pekka Paalanen98262232011-12-01 10:42:22 +02001781 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001782 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001783
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001784 shsurf->client = client;
1785
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001786 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001787}
1788
1789static void
1790shell_get_shell_surface(struct wl_client *client,
1791 struct wl_resource *resource,
1792 uint32_t id,
1793 struct wl_resource *surface_resource)
1794{
1795 struct weston_surface *surface = surface_resource->data;
1796 struct desktop_shell *shell = resource->data;
1797 struct shell_surface *shsurf;
1798
1799 if (get_shell_surface(surface)) {
1800 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001801 WL_DISPLAY_ERROR_INVALID_OBJECT,
1802 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001803 return;
1804 }
1805
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001806 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001807 if (!shsurf) {
1808 wl_resource_post_error(surface_resource,
1809 WL_DISPLAY_ERROR_INVALID_OBJECT,
1810 "surface->configure already set");
1811 return;
1812 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001813
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001814 shsurf->resource.destroy = shell_destroy_shell_surface;
1815 shsurf->resource.object.id = id;
1816 shsurf->resource.object.interface = &wl_shell_surface_interface;
1817 shsurf->resource.object.implementation =
1818 (void (**)(void)) &shell_surface_implementation;
1819 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001820
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001821 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001822}
1823
1824static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001825 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001826};
1827
Kristian Høgsberg07937562011-04-12 17:25:42 -04001828static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001829handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001830{
1831 proc->pid = 0;
1832}
1833
1834static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001835launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001836{
1837 if (shell->screensaver.binding)
1838 return;
1839
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001840 if (!shell->screensaver.path)
1841 return;
1842
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001843 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001844 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001845 return;
1846 }
1847
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001848 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001849 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001850 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001851 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001852}
1853
1854static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001855terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001856{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001857 if (shell->screensaver.process.pid == 0)
1858 return;
1859
1860 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001861}
1862
1863static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001864configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001865{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001866 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001867
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001868 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1869 if (s->output == es->output && s != es) {
1870 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001871 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001872 }
1873 }
1874
1875 weston_surface_configure(es, es->output->x, es->output->y,
1876 es->buffer->width, es->buffer->height);
1877
1878 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001879 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001880 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001881 }
1882}
1883
1884static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001885background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1886{
1887 struct desktop_shell *shell = es->private;
1888
1889 configure_static_surface(es, &shell->background_layer);
1890}
1891
1892static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001893desktop_shell_set_background(struct wl_client *client,
1894 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001895 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001896 struct wl_resource *surface_resource)
1897{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001898 struct desktop_shell *shell = resource->data;
1899 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001900
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001901 if (surface->configure) {
1902 wl_resource_post_error(surface_resource,
1903 WL_DISPLAY_ERROR_INVALID_OBJECT,
1904 "surface role already assigned");
1905 return;
1906 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001907
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001908 surface->configure = background_configure;
1909 surface->private = shell;
1910 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001911 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001912 surface_resource,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001913 surface->output->current->width,
1914 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001915}
1916
1917static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001918panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1919{
1920 struct desktop_shell *shell = es->private;
1921
1922 configure_static_surface(es, &shell->panel_layer);
1923}
1924
1925static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001926desktop_shell_set_panel(struct wl_client *client,
1927 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001928 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001929 struct wl_resource *surface_resource)
1930{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001931 struct desktop_shell *shell = resource->data;
1932 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001933
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001934 if (surface->configure) {
1935 wl_resource_post_error(surface_resource,
1936 WL_DISPLAY_ERROR_INVALID_OBJECT,
1937 "surface role already assigned");
1938 return;
1939 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001940
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001941 surface->configure = panel_configure;
1942 surface->private = shell;
1943 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001944 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001945 surface_resource,
1946 surface->output->current->width,
1947 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001948}
1949
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001950static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001951lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1952{
1953 struct desktop_shell *shell = surface->private;
1954
1955 center_on_output(surface, get_default_output(shell->compositor));
1956
1957 if (!weston_surface_is_mapped(surface)) {
1958 wl_list_insert(&shell->lock_layer.surface_list,
1959 &surface->layer_link);
1960 weston_surface_assign_output(surface);
1961 weston_compositor_wake(shell->compositor);
1962 }
1963}
1964
1965static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001966handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001967{
Tiago Vignattibe143262012-04-16 17:31:41 +03001968 struct desktop_shell *shell =
1969 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001970
Martin Minarik6d118362012-06-07 18:01:59 +02001971 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001972 shell->lock_surface = NULL;
1973}
1974
1975static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001976desktop_shell_set_lock_surface(struct wl_client *client,
1977 struct wl_resource *resource,
1978 struct wl_resource *surface_resource)
1979{
Tiago Vignattibe143262012-04-16 17:31:41 +03001980 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001981 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001982
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001983 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001984
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001985 if (!shell->locked)
1986 return;
1987
Pekka Paalanen98262232011-12-01 10:42:22 +02001988 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001989
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001990 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1991 wl_signal_add(&surface_resource->destroy_signal,
1992 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001993
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001994 surface->configure = lock_surface_configure;
1995 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001996}
1997
1998static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001999resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002000{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002001 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002002 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002003
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002004 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
2005 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002006
2007 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002008
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002009 wl_list_remove(&shell->lock_layer.link);
2010 wl_list_insert(&shell->compositor->cursor_layer.link,
2011 &shell->fullscreen_layer.link);
2012 wl_list_insert(&shell->fullscreen_layer.link,
2013 &shell->panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002014 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002015
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002016 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002017
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002018 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002019 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002020 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002021 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002022}
2023
2024static void
2025desktop_shell_unlock(struct wl_client *client,
2026 struct wl_resource *resource)
2027{
Tiago Vignattibe143262012-04-16 17:31:41 +03002028 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002029
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002030 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002031
2032 if (shell->locked)
2033 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002034}
2035
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002036static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002037desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002038 struct wl_resource *resource,
2039 struct wl_resource *surface_resource)
2040{
2041 struct desktop_shell *shell = resource->data;
2042
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002043 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002044}
2045
Kristian Høgsberg75840622011-09-06 13:48:16 -04002046static const struct desktop_shell_interface desktop_shell_implementation = {
2047 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002048 desktop_shell_set_panel,
2049 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002050 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002051 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002052};
2053
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002054static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002055get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002056{
2057 struct shell_surface *shsurf;
2058
2059 shsurf = get_shell_surface(surface);
2060 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002061 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002062 return shsurf->type;
2063}
2064
Kristian Høgsberg75840622011-09-06 13:48:16 -04002065static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002066move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002067{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002068 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002069 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002070 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002071
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002072 if (surface == NULL)
2073 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002074
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002075 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002076 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002077 return;
2078
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002079 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002080}
2081
2082static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002083resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002084{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002085 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002086 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002087 uint32_t edges = 0;
2088 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002089 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002090
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002091 if (surface == NULL)
2092 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002093
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002094 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002095 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002096 return;
2097
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002098 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002099 wl_fixed_to_int(seat->pointer->grab_x),
2100 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002101 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002102
Pekka Paalanen60921e52012-01-25 15:55:43 +02002103 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002104 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002105 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002106 edges |= 0;
2107 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002108 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002109
Pekka Paalanen60921e52012-01-25 15:55:43 +02002110 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002111 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002112 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002113 edges |= 0;
2114 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002115 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002116
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002117 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002118}
2119
2120static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002121surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002122 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002123{
Scott Moreau02709af2012-05-22 01:54:10 -06002124 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002125 struct shell_surface *shsurf;
2126 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002127 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002128
2129 if (surface == NULL)
2130 return;
2131
2132 shsurf = get_shell_surface(surface);
2133 if (!shsurf)
2134 return;
2135
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002136 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002137
Scott Moreau02709af2012-05-22 01:54:10 -06002138 if (surface->alpha > 1.0)
2139 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002140 if (surface->alpha < step)
2141 surface->alpha = step;
2142
2143 surface->geometry.dirty = 1;
2144 weston_surface_damage(surface);
2145}
2146
2147static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002148do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002149 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002150{
Daniel Stone37816df2012-05-16 18:45:18 +01002151 struct weston_seat *ws = (struct weston_seat *) seat;
2152 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002153 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002154 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002155
2156 wl_list_for_each(output, &compositor->output_list, link) {
2157 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002158 wl_fixed_to_double(seat->pointer->x),
2159 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002160 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002161 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002162 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002163 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002164 increment = -output->zoom.increment;
2165 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002166 increment = output->zoom.increment *
2167 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002168 else
2169 increment = 0;
2170
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002171 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002172
Scott Moreaue6603982012-06-11 13:07:51 -06002173 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002174 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002175 else if (output->zoom.level > output->zoom.max_level)
2176 output->zoom.level = output->zoom.max_level;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002177 else {
Scott Moreaue6603982012-06-11 13:07:51 -06002178 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002179 output->disable_planes++;
2180 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002181
Scott Moreaue6603982012-06-11 13:07:51 -06002182 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002183
Scott Moreau8dacaab2012-06-17 18:10:58 -06002184 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002185 }
2186 }
2187}
2188
Scott Moreauccbf29d2012-02-22 14:21:41 -07002189static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002190zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002191 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002192{
2193 do_zoom(seat, time, 0, axis, value);
2194}
2195
2196static void
2197zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2198 void *data)
2199{
2200 do_zoom(seat, time, key, 0, 0);
2201}
2202
2203static void
2204terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2205 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002206{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002207 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002208
Daniel Stone325fc2d2012-05-30 16:31:58 +01002209 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002210}
2211
2212static void
Scott Moreau447013d2012-02-18 05:05:29 -07002213rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002214 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002215{
2216 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002217 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002218 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002219 struct shell_surface *shsurf = rotate->base.shsurf;
2220 struct weston_surface *surface;
2221 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2222
2223 if (!shsurf)
2224 return;
2225
2226 surface = shsurf->surface;
2227
2228 cx = 0.5f * surface->geometry.width;
2229 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002230
Daniel Stone37816df2012-05-16 18:45:18 +01002231 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2232 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002233 r = sqrtf(dx * dx + dy * dy);
2234
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002235 wl_list_remove(&shsurf->rotation.transform.link);
2236 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002237
2238 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002239 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002240 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002241
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002242 weston_matrix_init(&rotate->rotation);
2243 rotate->rotation.d[0] = dx / r;
2244 rotate->rotation.d[4] = -dy / r;
2245 rotate->rotation.d[1] = -rotate->rotation.d[4];
2246 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002247
2248 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002249 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002250 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002251 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002252 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002253
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002254 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002255 &shsurf->surface->geometry.transformation_list,
2256 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002257 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002258 wl_list_init(&shsurf->rotation.transform.link);
2259 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002260 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002261 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002262
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002263 /* We need to adjust the position of the surface
2264 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002265 cposx = surface->geometry.x + cx;
2266 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002267 dposx = rotate->center.x - cposx;
2268 dposy = rotate->center.y - cposy;
2269 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002270 weston_surface_set_position(surface,
2271 surface->geometry.x + dposx,
2272 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002273 }
2274
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002275 /* Repaint implies weston_surface_update_transform(), which
2276 * lazily applies the damage due to rotation update.
2277 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002278 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002279}
2280
2281static void
Scott Moreau447013d2012-02-18 05:05:29 -07002282rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002283 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002284{
2285 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002286 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002287 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002288 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002289 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002290
Daniel Stone4dbadb12012-05-30 16:31:51 +01002291 if (pointer->button_count == 0 &&
2292 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002293 if (shsurf)
2294 weston_matrix_multiply(&shsurf->rotation.rotation,
2295 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002296 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002297 free(rotate);
2298 }
2299}
2300
Scott Moreau447013d2012-02-18 05:05:29 -07002301static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002302 noop_grab_focus,
2303 rotate_grab_motion,
2304 rotate_grab_button,
2305};
2306
2307static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002308rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002309 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002310{
2311 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002312 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002313 struct shell_surface *surface;
2314 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002315 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002316 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002317
2318 if (base_surface == NULL)
2319 return;
2320
2321 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002322 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002323 return;
2324
Pekka Paalanen460099f2012-01-20 16:48:25 +02002325 rotate = malloc(sizeof *rotate);
2326 if (!rotate)
2327 return;
2328
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002329 weston_surface_to_global_float(surface->surface,
2330 surface->surface->geometry.width / 2,
2331 surface->surface->geometry.height / 2,
2332 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002333
Daniel Stone37816df2012-05-16 18:45:18 +01002334 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2335 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002336 r = sqrtf(dx * dx + dy * dy);
2337 if (r > 20.0f) {
2338 struct weston_matrix inverse;
2339
2340 weston_matrix_init(&inverse);
2341 inverse.d[0] = dx / r;
2342 inverse.d[4] = dy / r;
2343 inverse.d[1] = -inverse.d[4];
2344 inverse.d[5] = inverse.d[0];
2345 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002346
2347 weston_matrix_init(&rotate->rotation);
2348 rotate->rotation.d[0] = dx / r;
2349 rotate->rotation.d[4] = -dy / r;
2350 rotate->rotation.d[1] = -rotate->rotation.d[4];
2351 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002352 } else {
2353 weston_matrix_init(&surface->rotation.rotation);
2354 weston_matrix_init(&rotate->rotation);
2355 }
2356
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002357 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2358 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002359}
2360
2361static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002362lower_fullscreen_layer(struct desktop_shell *shell)
2363{
2364 struct workspace *ws;
2365 struct weston_surface *surface, *prev;
2366
2367 ws = get_current_workspace(shell);
2368 wl_list_for_each_reverse_safe(surface, prev,
2369 &shell->fullscreen_layer.surface_list,
2370 layer_link)
2371 weston_surface_restack(surface, &ws->layer.surface_list);
2372}
2373
2374static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002375activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002376 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002377{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002378 struct workspace *ws = get_current_workspace(shell);
2379 struct focus_state *state;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002380
Daniel Stone37816df2012-05-16 18:45:18 +01002381 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002382
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002383 wl_list_for_each(state, &ws->focus_list, link)
2384 if (state->seat == seat)
2385 break;
2386
2387 if (&state->link == &ws->focus_list) {
2388 state = focus_state_create(seat, ws);
2389 if (state == NULL)
2390 return;
2391 }
2392
2393 state->keyboard_focus = es;
2394 wl_list_remove(&state->surface_destroy_listener.link);
2395 wl_signal_add(&es->surface.resource.destroy_signal,
2396 &state->surface_destroy_listener);
2397
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002398 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002399 case SHELL_SURFACE_FULLSCREEN:
2400 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002401 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002402 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002403 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002404 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002405 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002406 lower_fullscreen_layer(shell);
2407 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002408 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002409 }
2410}
2411
Alex Wu21858432012-04-01 20:13:08 +08002412/* no-op func for checking black surface */
2413static void
2414black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2415{
2416}
2417
2418static bool
2419is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2420{
2421 if (es->configure == black_surface_configure) {
2422 if (fs_surface)
2423 *fs_surface = (struct weston_surface *)es->private;
2424 return true;
2425 }
2426 return false;
2427}
2428
Kristian Høgsberg75840622011-09-06 13:48:16 -04002429static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002430click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002431 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002432{
Daniel Stone37816df2012-05-16 18:45:18 +01002433 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002434 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002435 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002436 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002437
Daniel Stone37816df2012-05-16 18:45:18 +01002438 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002439 if (!focus)
2440 return;
2441
Alex Wu21858432012-04-01 20:13:08 +08002442 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002443 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002444
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002445 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2446 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002447
Daniel Stone325fc2d2012-05-30 16:31:58 +01002448 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002449 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002450}
2451
2452static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002453lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002454{
Tiago Vignattibe143262012-04-16 17:31:41 +03002455 struct desktop_shell *shell =
2456 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002457 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002458 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002459
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002460 if (shell->locked) {
2461 wl_list_for_each(output, &shell->compositor->output_list, link)
2462 /* TODO: find a way to jump to other DPMS levels */
2463 if (output->set_dpms)
2464 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002465 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002466 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002467
2468 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002469
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002470 /* Hide all surfaces by removing the fullscreen, panel and
2471 * toplevel layers. This way nothing else can show or receive
2472 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002473
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002474 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002475 wl_list_remove(&shell->fullscreen_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002476 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002477 wl_list_insert(&shell->compositor->cursor_layer.link,
2478 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002479
Pekka Paalanen77346a62011-11-30 16:26:35 +02002480 launch_screensaver(shell);
2481
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002482 /* TODO: disable bindings that should not work while locked. */
2483
2484 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002485}
2486
2487static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002488unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002489{
Tiago Vignattibe143262012-04-16 17:31:41 +03002490 struct desktop_shell *shell =
2491 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002492
Pekka Paalanend81c2162011-11-16 13:47:34 +02002493 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002494 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002495 return;
2496 }
2497
2498 /* If desktop-shell client has gone away, unlock immediately. */
2499 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002500 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002501 return;
2502 }
2503
2504 if (shell->prepare_event_sent)
2505 return;
2506
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002507 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002508 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002509}
2510
2511static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002512show_input_panels(struct wl_listener *listener, void *data)
2513{
2514 struct desktop_shell *shell =
2515 container_of(listener, struct desktop_shell, show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002516 struct input_panel_surface *surface, *next;
2517 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002518
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002519 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002520 &shell->input_panel.surfaces, link) {
2521 ws = surface->surface;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002522 wl_list_insert(&shell->panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002523 &ws->layer_link);
2524 weston_surface_assign_output(ws);
2525 weston_surface_damage(ws);
2526 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002527 }
2528}
2529
2530static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002531input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy);
2532
2533static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002534hide_input_panels(struct wl_listener *listener, void *data)
2535{
2536 struct desktop_shell *shell =
2537 container_of(listener, struct desktop_shell, hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002538 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002539
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002540 wl_list_for_each_safe(surface, next,
2541 &shell->panel_layer.surface_list, layer_link)
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002542 if (surface->configure == input_panel_configure)
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002543 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002544}
2545
2546static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002547center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002548{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002549 struct weston_mode *mode = output->current;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002550 GLfloat x = (mode->width - surface->buffer->width) / 2;
2551 GLfloat y = (mode->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002552
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002553 weston_surface_configure(surface, output->x + x, output->y + y,
2554 surface->buffer->width,
2555 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002556}
2557
2558static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002559map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002560 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002561{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002562 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002563 struct shell_surface *shsurf = get_shell_surface(surface);
2564 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002565 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002566 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002567 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002568 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002569
Pekka Paalanen60921e52012-01-25 15:55:43 +02002570 surface->geometry.width = width;
2571 surface->geometry.height = height;
2572 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002573
2574 /* initial positioning, see also configure() */
2575 switch (surface_type) {
2576 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002577 weston_surface_set_position(surface, 10 + random() % 400,
2578 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002579 break;
Alex Wu4539b082012-03-01 12:57:46 +08002580 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002581 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002582 shell_map_fullscreen(shsurf);
2583 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002584 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002585 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002586 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002587 weston_surface_set_position(surface, shsurf->output->x,
2588 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002589 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002590 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002591 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002592 case SHELL_SURFACE_NONE:
2593 weston_surface_set_position(surface,
2594 surface->geometry.x + sx,
2595 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002596 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002597 default:
2598 ;
2599 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002600
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002601 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002602 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002603 case SHELL_SURFACE_POPUP:
2604 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002605 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002606 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2607 break;
Alex Wu4539b082012-03-01 12:57:46 +08002608 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002609 case SHELL_SURFACE_NONE:
2610 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002611 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002612 ws = get_current_workspace(shell);
2613 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002614 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002615 }
2616
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002617 if (surface_type != SHELL_SURFACE_NONE) {
2618 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002619 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2620 surface->output = shsurf->output;
2621 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002622
Juan Zhao7bb92f02011-12-15 11:31:51 -05002623 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002624 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002625 if (shsurf->transient.flags ==
2626 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2627 break;
2628 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002629 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002630 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002631 if (!shell->locked) {
2632 wl_list_for_each(seat, &compositor->seat_list, link)
2633 activate(shell, surface, seat);
2634 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002635 break;
2636 default:
2637 break;
2638 }
2639
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002640 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002641 {
2642 switch (shell->win_animation_type) {
2643 case ANIMATION_FADE:
2644 weston_fade_run(surface, NULL, NULL);
2645 break;
2646 case ANIMATION_ZOOM:
2647 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2648 break;
2649 default:
2650 break;
2651 }
2652 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002653}
2654
2655static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002656configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002657 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002658{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002659 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2660 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002661
Pekka Paalanen77346a62011-11-30 16:26:35 +02002662 shsurf = get_shell_surface(surface);
2663 if (shsurf)
2664 surface_type = shsurf->type;
2665
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002666 surface->geometry.x = x;
2667 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002668 surface->geometry.width = width;
2669 surface->geometry.height = height;
2670 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002671
2672 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002673 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002674 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002675 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002676 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002677 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002678 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002679 surface->geometry.x = surface->output->x;
2680 surface->geometry.y = surface->output->y +
2681 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002682 break;
Alex Wu4539b082012-03-01 12:57:46 +08002683 case SHELL_SURFACE_TOPLEVEL:
2684 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002685 default:
2686 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002687 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002688
Alex Wu4539b082012-03-01 12:57:46 +08002689 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002690 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002691 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002692
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002693 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08002694 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002695 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002696}
2697
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002698static void
2699shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2700{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002701 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002702 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002703 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002704
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002705 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002706 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002707 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002708 type_changed = 1;
2709 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002710
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002711 if (!weston_surface_is_mapped(es)) {
2712 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002713 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002714 es->geometry.width != es->buffer->width ||
2715 es->geometry.height != es->buffer->height) {
2716 GLfloat from_x, from_y;
2717 GLfloat to_x, to_y;
2718
2719 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2720 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2721 configure(shell, es,
2722 es->geometry.x + to_x - from_x,
2723 es->geometry.y + to_y - from_y,
2724 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002725 }
2726}
2727
Tiago Vignattibe143262012-04-16 17:31:41 +03002728static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002729
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002730static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002731desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002732{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002733 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002734 struct desktop_shell *shell =
2735 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002736
2737 shell->child.process.pid = 0;
2738 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002739
2740 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2741 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002742 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002743 shell->child.deathstamp = time;
2744 shell->child.deathcount = 0;
2745 }
2746
2747 shell->child.deathcount++;
2748 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002749 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002750 return;
2751 }
2752
Martin Minarik6d118362012-06-07 18:01:59 +02002753 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002754 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002755}
2756
2757static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002758launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002759{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002760 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002761
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002762 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002763 &shell->child.process,
2764 shell_exe,
2765 desktop_shell_sigchld);
2766
2767 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002768 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002769 return 0;
2770}
2771
2772static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002773bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2774{
Tiago Vignattibe143262012-04-16 17:31:41 +03002775 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002776
2777 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002778 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002779}
2780
Kristian Høgsberg75840622011-09-06 13:48:16 -04002781static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002782unbind_desktop_shell(struct wl_resource *resource)
2783{
Tiago Vignattibe143262012-04-16 17:31:41 +03002784 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002785
2786 if (shell->locked)
2787 resume_desktop(shell);
2788
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002789 shell->child.desktop_shell = NULL;
2790 shell->prepare_event_sent = false;
2791 free(resource);
2792}
2793
2794static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002795bind_desktop_shell(struct wl_client *client,
2796 void *data, uint32_t version, uint32_t id)
2797{
Tiago Vignattibe143262012-04-16 17:31:41 +03002798 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002799 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002800
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002801 resource = wl_client_add_object(client, &desktop_shell_interface,
2802 &desktop_shell_implementation,
2803 id, shell);
2804
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002805 if (client == shell->child.client) {
2806 resource->destroy = unbind_desktop_shell;
2807 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002808 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002809 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002810
2811 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2812 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002813 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002814}
2815
Pekka Paalanen6e168112011-11-24 11:34:05 +02002816static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002817screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2818{
2819 struct desktop_shell *shell = surface->private;
2820
2821 if (!shell->locked)
2822 return;
2823
2824 center_on_output(surface, surface->output);
2825
2826 if (wl_list_empty(&surface->layer_link)) {
2827 wl_list_insert(shell->lock_layer.surface_list.prev,
2828 &surface->layer_link);
2829 weston_surface_assign_output(surface);
2830 shell->compositor->idle_time = shell->screensaver.duration;
2831 weston_compositor_wake(shell->compositor);
2832 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2833 }
2834}
2835
2836static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02002837screensaver_set_surface(struct wl_client *client,
2838 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002839 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02002840 struct wl_resource *output_resource)
2841{
Tiago Vignattibe143262012-04-16 17:31:41 +03002842 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002843 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002844 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002845
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002846 surface->configure = screensaver_configure;
2847 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002848 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002849}
2850
2851static const struct screensaver_interface screensaver_implementation = {
2852 screensaver_set_surface
2853};
2854
2855static void
2856unbind_screensaver(struct wl_resource *resource)
2857{
Tiago Vignattibe143262012-04-16 17:31:41 +03002858 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002859
Pekka Paalanen77346a62011-11-30 16:26:35 +02002860 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002861 free(resource);
2862}
2863
2864static void
2865bind_screensaver(struct wl_client *client,
2866 void *data, uint32_t version, uint32_t id)
2867{
Tiago Vignattibe143262012-04-16 17:31:41 +03002868 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002869 struct wl_resource *resource;
2870
2871 resource = wl_client_add_object(client, &screensaver_interface,
2872 &screensaver_implementation,
2873 id, shell);
2874
Pekka Paalanen77346a62011-11-30 16:26:35 +02002875 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002876 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002877 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002878 return;
2879 }
2880
2881 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2882 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002883 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002884}
2885
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002886static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002887input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2888{
2889 struct weston_mode *mode = surface->output->current;
2890 GLfloat x = (mode->width - surface->buffer->width) / 2;
2891 GLfloat y = mode->height - surface->buffer->height;
2892
2893 /* Don't map the input panel here, wait for
2894 * show_input_panels signal. */
2895
2896 weston_surface_configure(surface,
2897 surface->output->x + x,
2898 surface->output->y + y,
2899 surface->buffer->width,
2900 surface->buffer->height);
2901}
2902
2903static void
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002904destroy_input_panel_surface(struct wl_listener *listener,
2905 void *data)
2906{
2907 struct input_panel_surface *input_panel_surface =
2908 container_of(listener, struct input_panel_surface, listener);
2909
2910 wl_list_remove(&listener->link);
2911 wl_list_remove(&input_panel_surface->link);
2912
2913 free(input_panel_surface);
2914}
2915
2916static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002917input_panel_set_surface(struct wl_client *client,
2918 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002919 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002920 struct wl_resource *output_resource)
2921{
2922 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002923 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002924 struct weston_output *output = output_resource->data;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002925 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002926
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002927 surface->configure = input_panel_configure;
2928 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002929 surface->output = output;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002930
2931 input_panel_surface = malloc(sizeof *input_panel_surface);
2932 if (!input_panel_surface) {
2933 wl_resource_post_no_memory(resource);
2934 return;
2935 }
2936
2937 input_panel_surface->surface = surface;
2938 input_panel_surface->listener.notify = destroy_input_panel_surface;
2939
2940 wl_signal_add(&surface_resource->destroy_signal,
2941 &input_panel_surface->listener);
2942
2943 wl_list_insert(&shell->input_panel.surfaces,
2944 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002945}
2946
2947static const struct input_panel_interface input_panel_implementation = {
2948 input_panel_set_surface
2949};
2950
2951static void
2952unbind_input_panel(struct wl_resource *resource)
2953{
2954 struct desktop_shell *shell = resource->data;
2955
2956 shell->input_panel.binding = NULL;
2957 free(resource);
2958}
2959
2960static void
2961bind_input_panel(struct wl_client *client,
2962 void *data, uint32_t version, uint32_t id)
2963{
2964 struct desktop_shell *shell = data;
2965 struct wl_resource *resource;
2966
2967 resource = wl_client_add_object(client, &input_panel_interface,
2968 &input_panel_implementation,
2969 id, shell);
2970
2971 if (shell->input_panel.binding == NULL) {
2972 resource->destroy = unbind_input_panel;
2973 shell->input_panel.binding = resource;
2974 return;
2975 }
2976
2977 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2978 "interface object already bound");
2979 wl_resource_destroy(resource);
2980}
2981
Kristian Høgsberg07045392012-02-19 18:52:44 -05002982struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002983 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002984 struct weston_surface *current;
2985 struct wl_listener listener;
2986 struct wl_keyboard_grab grab;
2987};
2988
2989static void
2990switcher_next(struct switcher *switcher)
2991{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002992 struct weston_surface *surface;
2993 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002994 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002995 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002996
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002997 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002998 switch (get_shell_surface_type(surface)) {
2999 case SHELL_SURFACE_TOPLEVEL:
3000 case SHELL_SURFACE_FULLSCREEN:
3001 case SHELL_SURFACE_MAXIMIZED:
3002 if (first == NULL)
3003 first = surface;
3004 if (prev == switcher->current)
3005 next = surface;
3006 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003007 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003008 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003009 weston_surface_damage(surface);
3010 break;
3011 default:
3012 break;
3013 }
Alex Wu1659daa2012-04-01 20:13:09 +08003014
3015 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003016 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003017 surface->geometry.dirty = 1;
3018 weston_surface_damage(surface);
3019 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003020 }
3021
3022 if (next == NULL)
3023 next = first;
3024
Alex Wu07b26062012-03-12 16:06:01 +08003025 if (next == NULL)
3026 return;
3027
Kristian Høgsberg07045392012-02-19 18:52:44 -05003028 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003029 wl_signal_add(&next->surface.resource.destroy_signal,
3030 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003031
3032 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003033 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003034
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003035 shsurf = get_shell_surface(switcher->current);
3036 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003037 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003038}
3039
3040static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003041switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003042{
3043 struct switcher *switcher =
3044 container_of(listener, struct switcher, listener);
3045
3046 switcher_next(switcher);
3047}
3048
3049static void
Daniel Stone351eb612012-05-31 15:27:47 -04003050switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003051{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003052 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003053 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003054 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003055
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003056 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003057 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003058 weston_surface_damage(surface);
3059 }
3060
Alex Wu07b26062012-03-12 16:06:01 +08003061 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003062 activate(switcher->shell, switcher->current,
3063 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003064 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003065 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003066 free(switcher);
3067}
3068
3069static void
3070switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003071 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003072{
3073 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003074 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003075
Daniel Stonec9785ea2012-05-30 16:31:52 +01003076 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003077 switcher_next(switcher);
3078}
3079
3080static void
3081switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3082 uint32_t mods_depressed, uint32_t mods_latched,
3083 uint32_t mods_locked, uint32_t group)
3084{
3085 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003086 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003087
Daniel Stone351eb612012-05-31 15:27:47 -04003088 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3089 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003090}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003091
3092static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003093 switcher_key,
3094 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003095};
3096
3097static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003098switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3099 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003100{
Tiago Vignattibe143262012-04-16 17:31:41 +03003101 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003102 struct switcher *switcher;
3103
3104 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003105 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003106 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003107 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003108 wl_list_init(&switcher->listener.link);
3109
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003110 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003111 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003112 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3113 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003114 switcher_next(switcher);
3115}
3116
Pekka Paalanen3c647232011-12-22 13:43:43 +02003117static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003118backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3119 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003120{
3121 struct weston_compositor *compositor = data;
3122 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003123 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003124
3125 /* TODO: we're limiting to simple use cases, where we assume just
3126 * control on the primary display. We'd have to extend later if we
3127 * ever get support for setting backlights on random desktop LCD
3128 * panels though */
3129 output = get_default_output(compositor);
3130 if (!output)
3131 return;
3132
3133 if (!output->set_backlight)
3134 return;
3135
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003136 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3137 backlight_new = output->backlight_current - 25;
3138 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3139 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003140
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003141 if (backlight_new < 5)
3142 backlight_new = 5;
3143 if (backlight_new > 255)
3144 backlight_new = 255;
3145
3146 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003147 output->set_backlight(output, output->backlight_current);
3148}
3149
3150static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003151debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3152 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003153{
Tiago Vignattibe143262012-04-16 17:31:41 +03003154 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003155 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003156 struct weston_surface *surface;
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003157 struct weston_plane plane;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003158
3159 if (shell->debug_repaint_surface) {
3160 weston_surface_destroy(shell->debug_repaint_surface);
3161 shell->debug_repaint_surface = NULL;
3162 } else {
3163 surface = weston_surface_create(compositor);
3164 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3165 weston_surface_configure(surface, 0, 0, 8192, 8192);
3166 wl_list_insert(&compositor->fade_layer.surface_list,
3167 &surface->layer_link);
3168 weston_surface_assign_output(surface);
3169 pixman_region32_init(&surface->input);
3170
3171 /* Here's the dirty little trick that makes the
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003172 * repaint debugging work: we move the surface to a
3173 * different plane and force an update_transform to
3174 * update dependent state and clear the
3175 * geometry.dirty bit. This way the call to
3176 * damage_below() in update_transform() does not
3177 * add damage to the primary plane. */
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003178
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003179 weston_plane_init(&plane, 0, 0);
3180 surface->plane = &plane;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003181 weston_surface_update_transform(surface);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003182 shell->debug_repaint_surface = surface;
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003183 surface->plane = &compositor->primary_plane;
3184 weston_plane_release(&plane);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003185 }
3186}
3187
3188static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003189force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3190 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003191{
3192 struct wl_client *client;
3193 pid_t pid;
3194 uid_t uid;
3195 gid_t gid;
3196
Daniel Stone325fc2d2012-05-30 16:31:58 +01003197 client = seat->keyboard->focus->resource.client;
3198 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003199
Daniel Stone325fc2d2012-05-30 16:31:58 +01003200 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003201}
3202
3203static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003204workspace_up_binding(struct wl_seat *seat, uint32_t time,
3205 uint32_t key, void *data)
3206{
3207 struct desktop_shell *shell = data;
3208 unsigned int new_index = shell->workspaces.current;
3209
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003210 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003211 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003212 if (new_index != 0)
3213 new_index--;
3214
3215 change_workspace(shell, new_index);
3216}
3217
3218static void
3219workspace_down_binding(struct wl_seat *seat, uint32_t time,
3220 uint32_t key, void *data)
3221{
3222 struct desktop_shell *shell = data;
3223 unsigned int new_index = shell->workspaces.current;
3224
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003225 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003226 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003227 if (new_index < shell->workspaces.num - 1)
3228 new_index++;
3229
3230 change_workspace(shell, new_index);
3231}
3232
3233static void
3234workspace_f_binding(struct wl_seat *seat, uint32_t time,
3235 uint32_t key, void *data)
3236{
3237 struct desktop_shell *shell = data;
3238 unsigned int new_index;
3239
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003240 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003241 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003242 new_index = key - KEY_F1;
3243 if (new_index >= shell->workspaces.num)
3244 new_index = shell->workspaces.num - 1;
3245
3246 change_workspace(shell, new_index);
3247}
3248
3249
3250static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003251shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003252{
Tiago Vignattibe143262012-04-16 17:31:41 +03003253 struct desktop_shell *shell =
3254 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003255 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003256
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003257 if (shell->child.client)
3258 wl_client_destroy(shell->child.client);
3259
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003260 wl_list_remove(&shell->lock_listener.link);
3261 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003262 wl_list_remove(&shell->show_input_panel_listener.link);
3263 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003264
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003265 wl_array_for_each(ws, &shell->workspaces.array)
3266 workspace_destroy(*ws);
3267 wl_array_release(&shell->workspaces.array);
3268
Pekka Paalanen3c647232011-12-22 13:43:43 +02003269 free(shell->screensaver.path);
3270 free(shell);
3271}
3272
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003273static void
3274shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3275{
3276 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003277 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003278
3279 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003280 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3281 MODIFIER_CTRL | MODIFIER_ALT,
3282 terminate_binding, ec);
3283 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3284 click_to_activate_binding,
3285 shell);
3286 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3287 MODIFIER_SUPER | MODIFIER_ALT,
3288 surface_opacity_binding, NULL);
3289 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3290 MODIFIER_SUPER, zoom_axis_binding,
3291 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003292
3293 /* configurable bindings */
3294 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003295 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3296 zoom_key_binding, NULL);
3297 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3298 zoom_key_binding, NULL);
3299 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3300 shell);
3301 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3302 resize_binding, shell);
3303 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3304 rotate_binding, NULL);
3305 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3306 shell);
3307 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3308 ec);
3309 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3310 backlight_binding, ec);
3311 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3312 ec);
3313 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3314 backlight_binding, ec);
Kristian Høgsberg73694c82012-06-28 14:13:10 -04003315 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003316 debug_repaint_binding, shell);
3317 weston_compositor_add_key_binding(ec, KEY_K, mod,
3318 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003319 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3320 workspace_up_binding, shell);
3321 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3322 workspace_down_binding, shell);
3323
3324 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3325 if (shell->workspaces.num > 1) {
3326 num_workspace_bindings = shell->workspaces.num;
3327 if (num_workspace_bindings > 6)
3328 num_workspace_bindings = 6;
3329 for (i = 0; i < num_workspace_bindings; i++)
3330 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3331 workspace_f_binding,
3332 shell);
3333 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003334}
3335
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003336int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003337shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003338
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003339WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003340shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003341{
Tiago Vignattibe143262012-04-16 17:31:41 +03003342 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003343 struct workspace **pws;
3344 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003345
3346 shell = malloc(sizeof *shell);
3347 if (shell == NULL)
3348 return -1;
3349
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003350 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003351 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003352
3353 shell->destroy_listener.notify = shell_destroy;
3354 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3355 shell->lock_listener.notify = lock;
3356 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3357 shell->unlock_listener.notify = unlock;
3358 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003359 shell->show_input_panel_listener.notify = show_input_panels;
3360 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3361 shell->hide_input_panel_listener.notify = hide_input_panels;
3362 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003363 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003364 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003365 ec->shell_interface.create_shell_surface = create_shell_surface;
3366 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003367 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003368 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003369 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003370
Pekka Paalanen77346a62011-11-30 16:26:35 +02003371 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003372 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003373
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003374 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3375 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003376 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3377 weston_layer_init(&shell->lock_layer, NULL);
3378
3379 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003380
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003381 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003382
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003383 for (i = 0; i < shell->workspaces.num; i++) {
3384 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3385 if (pws == NULL)
3386 return -1;
3387
3388 *pws = workspace_create();
3389 if (*pws == NULL)
3390 return -1;
3391 }
3392 activate_workspace(shell, 0);
3393
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003394 wl_list_init(&shell->workspaces.animation.link);
3395 shell->workspaces.animation.frame = animate_workspace_change_frame;
3396
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003397 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3398 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003399 return -1;
3400
Kristian Høgsberg75840622011-09-06 13:48:16 -04003401 if (wl_display_add_global(ec->wl_display,
3402 &desktop_shell_interface,
3403 shell, bind_desktop_shell) == NULL)
3404 return -1;
3405
Pekka Paalanen6e168112011-11-24 11:34:05 +02003406 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3407 shell, bind_screensaver) == NULL)
3408 return -1;
3409
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003410 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3411 shell, bind_input_panel) == NULL)
3412 return -1;
3413
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003414 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003415 if (launch_desktop_shell_process(shell) != 0)
3416 return -1;
3417
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003418 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003419 if (ec->seat->seat.pointer)
3420 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3421 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003422
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003423 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003424
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003425 return 0;
3426}