blob: 6ad312b386f6272020131bec8e3caa7dc13ad24d [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040033#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050034
Pekka Paalanen50719bc2011-11-22 14:18:50 +020035#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040037#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020038#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050039
Jonas Ådahle3cddce2012-06-13 00:01:22 +020040#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020041#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020042
Juan Zhaoe10d2792012-04-25 19:09:52 +080043enum animation_type {
44 ANIMATION_NONE,
45
46 ANIMATION_ZOOM,
47 ANIMATION_FADE
48};
49
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
51 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040052 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Jonas Ådahle3cddce2012-06-13 00:01:22 +020059struct workspace {
60 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020061
62 struct wl_list focus_list;
63 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020064};
65
Philipp Brüschweiler88013572012-08-06 13:44:42 +020066struct input_panel_surface {
67 struct wl_list link;
68 struct weston_surface *surface;
69 struct wl_listener listener;
70};
71
Tiago Vignattibe143262012-04-16 17:31:41 +030072struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050073 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040074
75 struct wl_listener lock_listener;
76 struct wl_listener unlock_listener;
77 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020078 struct wl_listener show_input_panel_listener;
79 struct wl_listener hide_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020080
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050081 struct weston_layer fullscreen_layer;
82 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050083 struct weston_layer background_layer;
84 struct weston_layer lock_layer;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +020085 struct weston_layer input_panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050086
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040087 struct wl_listener pointer_focus_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030088 struct weston_surface *grab_surface;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040089
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020090 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050091 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020092 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020093 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020094
95 unsigned deathcount;
96 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020097 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020098
99 bool locked;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +0200100 bool showing_input_panels;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200101 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200102
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400103 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500104 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100105
Pekka Paalanen77346a62011-11-30 16:26:35 +0200106 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200107 struct wl_array array;
108 unsigned int current;
109 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200110
111 struct weston_animation animation;
112 int anim_dir;
113 uint32_t anim_timestamp;
114 double anim_current;
115 struct workspace *anim_from;
116 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200117 } workspaces;
118
119 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200120 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200121 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200122 struct wl_resource *binding;
123 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500124 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200125 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500126
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200127 struct {
128 struct wl_resource *binding;
129 struct wl_list surfaces;
130 } input_panel;
131
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300132 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800133 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500134 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400135};
136
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500137enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200138 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500139 SHELL_SURFACE_TOPLEVEL,
140 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500141 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800142 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500143 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200144};
145
Scott Moreauff1db4a2012-04-17 19:06:18 -0600146struct ping_timer {
147 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600148 uint32_t serial;
149};
150
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200151struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200152 struct wl_resource resource;
153
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500154 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200155 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400156 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300157 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200158
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400159 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400160 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500161 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800162 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800163 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600164 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100165
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500166 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200167 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500168 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200169 } rotation;
170
171 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700172 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500173 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200174 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500175 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100176 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400177 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500178 } popup;
179
Alex Wu4539b082012-03-01 12:57:46 +0800180 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300181 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400182 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300183 } transient;
184
185 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800186 enum wl_shell_surface_fullscreen_method type;
187 struct weston_transform transform; /* matrix from x, y */
188 uint32_t framerate;
189 struct weston_surface *black_surface;
190 } fullscreen;
191
Scott Moreauff1db4a2012-04-17 19:06:18 -0600192 struct ping_timer *ping_timer;
193
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200194 struct weston_transform workspace_transform;
195
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500196 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500197 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100198 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400199
200 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200201};
202
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300203struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700204 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300205 struct shell_surface *shsurf;
206 struct wl_listener shsurf_destroy_listener;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300207 struct wl_pointer *pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300208};
209
210struct weston_move_grab {
211 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100212 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500213};
214
Pekka Paalanen460099f2012-01-20 16:48:25 +0200215struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300216 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500217 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200218 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400219 GLfloat x;
220 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200221 } center;
222};
223
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400224static void
225activate(struct desktop_shell *shell, struct weston_surface *es,
226 struct weston_seat *seat);
227
228static struct workspace *
229get_current_workspace(struct desktop_shell *shell);
230
Alex Wubd3354b2012-04-17 17:20:49 +0800231static struct shell_surface *
232get_shell_surface(struct weston_surface *surface);
233
234static struct desktop_shell *
235shell_surface_get_shell(struct shell_surface *shsurf);
236
237static bool
238shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
239{
240 struct desktop_shell *shell;
241 struct weston_surface *top_fs_es;
242
243 shell = shell_surface_get_shell(shsurf);
244
245 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
246 return false;
247
248 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
249 struct weston_surface,
250 layer_link);
251 return (shsurf == get_shell_surface(top_fs_es));
252}
253
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500254static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400255destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300256{
257 struct shell_grab *grab;
258
259 grab = container_of(listener, struct shell_grab,
260 shsurf_destroy_listener);
261
262 grab->shsurf = NULL;
263}
264
265static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300266shell_grab_start(struct shell_grab *grab,
267 const struct wl_pointer_grab_interface *interface,
268 struct shell_surface *shsurf,
269 struct wl_pointer *pointer,
270 enum desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300271{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300272 struct desktop_shell *shell = shsurf->shell;
273
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300274 grab->grab.interface = interface;
275 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400276 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
277 wl_signal_add(&shsurf->resource.destroy_signal,
278 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300279
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300280 grab->pointer = pointer;
281 grab->grab.focus = &shsurf->surface->surface;
282
283 wl_pointer_start_grab(pointer, &grab->grab);
284 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
285 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
286 wl_fixed_from_int(0), wl_fixed_from_int(0));
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300287}
288
289static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300290shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300291{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400292 if (grab->shsurf)
293 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300294
295 wl_pointer_end_grab(grab->pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300296}
297
298static void
Alex Wu4539b082012-03-01 12:57:46 +0800299center_on_output(struct weston_surface *surface,
300 struct weston_output *output);
301
Daniel Stone496ca172012-05-30 16:31:42 +0100302static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300303get_modifier(char *modifier)
304{
305 if (!modifier)
306 return MODIFIER_SUPER;
307
308 if (!strcmp("ctrl", modifier))
309 return MODIFIER_CTRL;
310 else if (!strcmp("alt", modifier))
311 return MODIFIER_ALT;
312 else if (!strcmp("super", modifier))
313 return MODIFIER_SUPER;
314 else
315 return MODIFIER_SUPER;
316}
317
Juan Zhaoe10d2792012-04-25 19:09:52 +0800318static enum animation_type
319get_animation_type(char *animation)
320{
321 if (!animation)
322 return ANIMATION_NONE;
323
324 if (!strcmp("zoom", animation))
325 return ANIMATION_ZOOM;
326 else if (!strcmp("fade", animation))
327 return ANIMATION_FADE;
328 else
329 return ANIMATION_NONE;
330}
331
Alex Wu4539b082012-03-01 12:57:46 +0800332static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300333shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200334{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200335 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200336 char *path = NULL;
337 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200338 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300339 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800340 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200341
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400342 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300343 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800344 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200345 { "num-workspaces",
346 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200347 };
348
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400349 struct config_key saver_keys[] = {
350 { "path", CONFIG_KEY_STRING, &path },
351 { "duration", CONFIG_KEY_INTEGER, &duration },
352 };
353
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200354 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400355 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200356 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
357 };
358
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200359 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500360 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200361 free(config_file);
362
Pekka Paalanen7296e792011-12-07 16:22:00 +0200363 shell->screensaver.path = path;
364 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300365 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800366 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200367 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
368}
369
370static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200371focus_state_destroy(struct focus_state *state)
372{
373 wl_list_remove(&state->seat_destroy_listener.link);
374 wl_list_remove(&state->surface_destroy_listener.link);
375 free(state);
376}
377
378static void
379focus_state_seat_destroy(struct wl_listener *listener, void *data)
380{
381 struct focus_state *state = container_of(listener,
382 struct focus_state,
383 seat_destroy_listener);
384
385 wl_list_remove(&state->link);
386 focus_state_destroy(state);
387}
388
389static void
390focus_state_surface_destroy(struct wl_listener *listener, void *data)
391{
392 struct focus_state *state = container_of(listener,
393 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400394 surface_destroy_listener);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400395 struct desktop_shell *shell;
396 struct weston_surface *surface, *next;
Jonas Ådahl04769742012-06-13 00:01:24 +0200397
Kristian Høgsberge3778222012-07-31 17:29:30 -0400398 next = NULL;
399 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
400 if (surface == state->keyboard_focus)
401 continue;
402
403 next = surface;
404 break;
405 }
406
407 if (next) {
408 shell = state->seat->compositor->shell_interface.shell;
409 activate(shell, next, state->seat);
410 } else {
411 wl_list_remove(&state->link);
412 focus_state_destroy(state);
413 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200414}
415
416static struct focus_state *
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400417focus_state_create(struct weston_seat *seat, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200418{
Jonas Ådahl04769742012-06-13 00:01:24 +0200419 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200420
421 state = malloc(sizeof *state);
422 if (state == NULL)
423 return NULL;
424
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400425 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200426 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400427 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200428
429 state->seat_destroy_listener.notify = focus_state_seat_destroy;
430 state->surface_destroy_listener.notify = focus_state_surface_destroy;
431 wl_signal_add(&seat->seat.destroy_signal,
432 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400433 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200434
435 return state;
436}
437
438static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400439restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200440{
441 struct focus_state *state, *next;
Jonas Ådahl56899442012-08-29 22:12:59 +0200442 struct wl_surface *surface;
Jonas Ådahl04769742012-06-13 00:01:24 +0200443
444 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Jonas Ådahl56899442012-08-29 22:12:59 +0200445 surface = state->keyboard_focus ?
446 &state->keyboard_focus->surface : NULL;
447
448 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200449 }
450}
451
452static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200453workspace_destroy(struct workspace *ws)
454{
Jonas Ådahl04769742012-06-13 00:01:24 +0200455 struct focus_state *state, *next;
456
457 wl_list_for_each_safe(state, next, &ws->focus_list, link)
458 focus_state_destroy(state);
459
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200460 free(ws);
461}
462
Jonas Ådahl04769742012-06-13 00:01:24 +0200463static void
464seat_destroyed(struct wl_listener *listener, void *data)
465{
466 struct weston_seat *seat = data;
467 struct focus_state *state, *next;
468 struct workspace *ws = container_of(listener,
469 struct workspace,
470 seat_destroyed_listener);
471
472 wl_list_for_each_safe(state, next, &ws->focus_list, link)
473 if (state->seat == seat)
474 wl_list_remove(&state->link);
475}
476
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200477static struct workspace *
478workspace_create(void)
479{
480 struct workspace *ws = malloc(sizeof *ws);
481 if (ws == NULL)
482 return NULL;
483
484 weston_layer_init(&ws->layer, NULL);
485
Jonas Ådahl04769742012-06-13 00:01:24 +0200486 wl_list_init(&ws->focus_list);
487 wl_list_init(&ws->seat_destroyed_listener.link);
488 ws->seat_destroyed_listener.notify = seat_destroyed;
489
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200490 return ws;
491}
492
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200493static int
494workspace_is_empty(struct workspace *ws)
495{
496 return wl_list_empty(&ws->layer.surface_list);
497}
498
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200499static struct workspace *
500get_workspace(struct desktop_shell *shell, unsigned int index)
501{
502 struct workspace **pws = shell->workspaces.array.data;
503 pws += index;
504 return *pws;
505}
506
507static struct workspace *
508get_current_workspace(struct desktop_shell *shell)
509{
510 return get_workspace(shell, shell->workspaces.current);
511}
512
513static void
514activate_workspace(struct desktop_shell *shell, unsigned int index)
515{
516 struct workspace *ws;
517
518 ws = get_workspace(shell, index);
519 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
520
521 shell->workspaces.current = index;
522}
523
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200524static unsigned int
525get_output_height(struct weston_output *output)
526{
527 return abs(output->region.extents.y1 - output->region.extents.y2);
528}
529
530static void
531surface_translate(struct weston_surface *surface, double d)
532{
533 struct shell_surface *shsurf = get_shell_surface(surface);
534 struct weston_transform *transform;
535
536 transform = &shsurf->workspace_transform;
537 if (wl_list_empty(&transform->link))
538 wl_list_insert(surface->geometry.transformation_list.prev,
539 &shsurf->workspace_transform.link);
540
541 weston_matrix_init(&shsurf->workspace_transform.matrix);
542 weston_matrix_translate(&shsurf->workspace_transform.matrix,
543 0.0, d, 0.0);
544 surface->geometry.dirty = 1;
545}
546
547static void
548workspace_translate_out(struct workspace *ws, double fraction)
549{
550 struct weston_surface *surface;
551 unsigned int height;
552 double d;
553
554 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
555 height = get_output_height(surface->output);
556 d = height * fraction;
557
558 surface_translate(surface, d);
559 }
560}
561
562static void
563workspace_translate_in(struct workspace *ws, double fraction)
564{
565 struct weston_surface *surface;
566 unsigned int height;
567 double d;
568
569 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
570 height = get_output_height(surface->output);
571
572 if (fraction > 0)
573 d = -(height - height * fraction);
574 else
575 d = height + height * fraction;
576
577 surface_translate(surface, d);
578 }
579}
580
581static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200582reverse_workspace_change_animation(struct desktop_shell *shell,
583 unsigned int index,
584 struct workspace *from,
585 struct workspace *to)
586{
587 shell->workspaces.current = index;
588
589 shell->workspaces.anim_to = to;
590 shell->workspaces.anim_from = from;
591 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
592 shell->workspaces.anim_timestamp = 0;
593
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400594 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200595
Scott Moreau4272e632012-08-13 09:58:41 -0600596 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200597}
598
599static void
600workspace_deactivate_transforms(struct workspace *ws)
601{
602 struct weston_surface *surface;
603 struct shell_surface *shsurf;
604
605 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
606 shsurf = get_shell_surface(surface);
607 wl_list_remove(&shsurf->workspace_transform.link);
608 wl_list_init(&shsurf->workspace_transform.link);
609 shsurf->surface->geometry.dirty = 1;
610 }
611}
612
613static void
614finish_workspace_change_animation(struct desktop_shell *shell,
615 struct workspace *from,
616 struct workspace *to)
617{
Scott Moreau4272e632012-08-13 09:58:41 -0600618 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200619
620 wl_list_remove(&shell->workspaces.animation.link);
621 workspace_deactivate_transforms(from);
622 workspace_deactivate_transforms(to);
623 shell->workspaces.anim_to = NULL;
624
625 wl_list_remove(&shell->workspaces.anim_from->layer.link);
626}
627
628static void
629animate_workspace_change_frame(struct weston_animation *animation,
630 struct weston_output *output, uint32_t msecs)
631{
632 struct desktop_shell *shell =
633 container_of(animation, struct desktop_shell,
634 workspaces.animation);
635 struct workspace *from = shell->workspaces.anim_from;
636 struct workspace *to = shell->workspaces.anim_to;
637 uint32_t t;
638 double x, y;
639
640 if (workspace_is_empty(from) && workspace_is_empty(to)) {
641 finish_workspace_change_animation(shell, from, to);
642 return;
643 }
644
645 if (shell->workspaces.anim_timestamp == 0) {
646 if (shell->workspaces.anim_current == 0.0)
647 shell->workspaces.anim_timestamp = msecs;
648 else
649 shell->workspaces.anim_timestamp =
650 msecs -
651 /* Invers of movement function 'y' below. */
652 (asin(1.0 - shell->workspaces.anim_current) *
653 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
654 M_2_PI);
655 }
656
657 t = msecs - shell->workspaces.anim_timestamp;
658
659 /*
660 * x = [0, π/2]
661 * y(x) = sin(x)
662 */
663 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
664 y = sin(x);
665
666 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -0600667 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200668
669 workspace_translate_out(from, shell->workspaces.anim_dir * y);
670 workspace_translate_in(to, shell->workspaces.anim_dir * y);
671 shell->workspaces.anim_current = y;
672
Scott Moreau4272e632012-08-13 09:58:41 -0600673 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200674 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200675 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200676 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200677}
678
679static void
680animate_workspace_change(struct desktop_shell *shell,
681 unsigned int index,
682 struct workspace *from,
683 struct workspace *to)
684{
685 struct weston_output *output;
686
687 int dir;
688
689 if (index > shell->workspaces.current)
690 dir = -1;
691 else
692 dir = 1;
693
694 shell->workspaces.current = index;
695
696 shell->workspaces.anim_dir = dir;
697 shell->workspaces.anim_from = from;
698 shell->workspaces.anim_to = to;
699 shell->workspaces.anim_current = 0.0;
700 shell->workspaces.anim_timestamp = 0;
701
702 output = container_of(shell->compositor->output_list.next,
703 struct weston_output, link);
704 wl_list_insert(&output->animation_list,
705 &shell->workspaces.animation.link);
706
707 wl_list_insert(&from->layer.link, &to->layer.link);
708
709 workspace_translate_in(to, 0);
710
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400711 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +0200712
Scott Moreau4272e632012-08-13 09:58:41 -0600713 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200714}
715
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200716static void
717change_workspace(struct desktop_shell *shell, unsigned int index)
718{
719 struct workspace *from;
720 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200721
722 if (index == shell->workspaces.current)
723 return;
724
725 /* Don't change workspace when there is any fullscreen surfaces. */
726 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
727 return;
728
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200729 from = get_current_workspace(shell);
730 to = get_workspace(shell, index);
731
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200732 if (shell->workspaces.anim_from == to &&
733 shell->workspaces.anim_to == from) {
734 reverse_workspace_change_animation(shell, index, from, to);
735 return;
736 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200737
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200738 if (shell->workspaces.anim_to != NULL)
739 finish_workspace_change_animation(shell,
740 shell->workspaces.anim_from,
741 shell->workspaces.anim_to);
742
743 if (workspace_is_empty(to) && workspace_is_empty(from)) {
744 shell->workspaces.current = index;
745 wl_list_insert(&from->layer.link, &to->layer.link);
746 wl_list_remove(&from->layer.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200747
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400748 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200749 }
750 else
751 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200752}
753
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200754static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400755noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100756 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500757{
758 grab->focus = NULL;
759}
760
761static void
Scott Moreau447013d2012-02-18 05:05:29 -0700762move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100763 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500764{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500765 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100766 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300767 struct shell_surface *shsurf = move->base.shsurf;
768 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100769 int dx = wl_fixed_to_int(pointer->x + move->dx);
770 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300771
772 if (!shsurf)
773 return;
774
775 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500776
Daniel Stone103db7f2012-05-08 17:17:55 +0100777 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200778 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -0400779
780 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500781}
782
783static void
Scott Moreau447013d2012-02-18 05:05:29 -0700784move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100785 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500786{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300787 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
788 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100789 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100790 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500791
Daniel Stone4dbadb12012-05-30 16:31:51 +0100792 if (pointer->button_count == 0 &&
793 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300794 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500795 free(grab);
796 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500797}
798
Scott Moreau447013d2012-02-18 05:05:29 -0700799static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500800 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500801 move_grab_motion,
802 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500803};
804
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -0400805static int
806surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
807{
808 struct weston_move_grab *move;
809
810 if (!shsurf)
811 return -1;
812
813 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
814 return 0;
815
816 move = malloc(sizeof *move);
817 if (!move)
818 return -1;
819
820 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
821 ws->seat.pointer->grab_x;
822 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
823 ws->seat.pointer->grab_y;
824
825 shell_grab_start(&move->base, &move_grab_interface, shsurf,
826 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
827
828 return 0;
829}
830
831static void
832shell_surface_move(struct wl_client *client, struct wl_resource *resource,
833 struct wl_resource *seat_resource, uint32_t serial)
834{
835 struct weston_seat *ws = seat_resource->data;
836 struct shell_surface *shsurf = resource->data;
837
838 if (ws->seat.pointer->button_count == 0 ||
839 ws->seat.pointer->grab_serial != serial ||
840 ws->seat.pointer->focus != &shsurf->surface->surface)
841 return;
842
843 if (surface_move(shsurf, ws) < 0)
844 wl_resource_post_no_memory(resource);
845}
846
847struct weston_resize_grab {
848 struct shell_grab base;
849 uint32_t edges;
850 int32_t width, height;
851};
852
853static void
854resize_grab_motion(struct wl_pointer_grab *grab,
855 uint32_t time, wl_fixed_t x, wl_fixed_t y)
856{
857 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
858 struct wl_pointer *pointer = grab->pointer;
859 struct shell_surface *shsurf = resize->base.shsurf;
860 int32_t width, height;
861 wl_fixed_t from_x, from_y;
862 wl_fixed_t to_x, to_y;
863
864 if (!shsurf)
865 return;
866
867 weston_surface_from_global_fixed(shsurf->surface,
868 pointer->grab_x, pointer->grab_y,
869 &from_x, &from_y);
870 weston_surface_from_global_fixed(shsurf->surface,
871 pointer->x, pointer->y, &to_x, &to_y);
872
873 width = resize->width;
874 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
875 width += wl_fixed_to_int(from_x - to_x);
876 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
877 width += wl_fixed_to_int(to_x - from_x);
878 }
879
880 height = resize->height;
881 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
882 height += wl_fixed_to_int(from_y - to_y);
883 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
884 height += wl_fixed_to_int(to_y - from_y);
885 }
886
887 shsurf->client->send_configure(shsurf->surface,
888 resize->edges, width, height);
889}
890
891static void
892send_configure(struct weston_surface *surface,
893 uint32_t edges, int32_t width, int32_t height)
894{
895 struct shell_surface *shsurf = get_shell_surface(surface);
896
897 wl_shell_surface_send_configure(&shsurf->resource,
898 edges, width, height);
899}
900
901static const struct weston_shell_client shell_client = {
902 send_configure
903};
904
905static void
906resize_grab_button(struct wl_pointer_grab *grab,
907 uint32_t time, uint32_t button, uint32_t state_w)
908{
909 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
910 struct wl_pointer *pointer = grab->pointer;
911 enum wl_pointer_button_state state = state_w;
912
913 if (pointer->button_count == 0 &&
914 state == WL_POINTER_BUTTON_STATE_RELEASED) {
915 shell_grab_end(&resize->base);
916 free(grab);
917 }
918}
919
920static const struct wl_pointer_grab_interface resize_grab_interface = {
921 noop_grab_focus,
922 resize_grab_motion,
923 resize_grab_button,
924};
925
926static int
927surface_resize(struct shell_surface *shsurf,
928 struct weston_seat *ws, uint32_t edges)
929{
930 struct weston_resize_grab *resize;
931
932 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
933 return 0;
934
935 if (edges == 0 || edges > 15 ||
936 (edges & 3) == 3 || (edges & 12) == 12)
937 return 0;
938
939 resize = malloc(sizeof *resize);
940 if (!resize)
941 return -1;
942
943 resize->edges = edges;
944 resize->width = shsurf->surface->geometry.width;
945 resize->height = shsurf->surface->geometry.height;
946
947 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
948 ws->seat.pointer, edges);
949
950 return 0;
951}
952
953static void
954shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
955 struct wl_resource *seat_resource, uint32_t serial,
956 uint32_t edges)
957{
958 struct weston_seat *ws = seat_resource->data;
959 struct shell_surface *shsurf = resource->data;
960
961 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
962 return;
963
964 if (ws->seat.pointer->button_count == 0 ||
965 ws->seat.pointer->grab_serial != serial ||
966 ws->seat.pointer->focus != &shsurf->surface->surface)
967 return;
968
969 if (surface_resize(shsurf, ws, edges) < 0)
970 wl_resource_post_no_memory(resource);
971}
972
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600973static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400974busy_cursor_grab_focus(struct wl_pointer_grab *base,
975 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600976{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400977 struct shell_grab *grab = (struct shell_grab *) base;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600978
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400979 if (grab->grab.focus != surface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300980 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400981 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600982 }
983}
984
985static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400986busy_cursor_grab_motion(struct wl_pointer_grab *grab,
987 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600988{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400989}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600990
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400991static void
Kristian Høgsbergbbe98392012-08-01 00:20:21 -0400992busy_cursor_grab_button(struct wl_pointer_grab *base,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400993 uint32_t time, uint32_t button, uint32_t state)
994{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -0400995 struct shell_grab *grab = (struct shell_grab *) base;
996 struct shell_surface *shsurf;
997 struct weston_surface *surface =
998 (struct weston_surface *) grab->grab.pointer->current;
999 struct weston_seat *seat =
1000 (struct weston_seat *) grab->grab.pointer->seat;
1001
1002 shsurf = get_shell_surface(surface);
1003 if (shsurf && button == BTN_LEFT && state) {
1004 activate(shsurf->shell, shsurf->surface, seat);
1005 surface_move(shsurf, seat);
1006 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001007}
1008
1009static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1010 busy_cursor_grab_focus,
1011 busy_cursor_grab_motion,
1012 busy_cursor_grab_button,
1013};
1014
1015static void
1016set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1017{
1018 struct shell_grab *grab;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001019
1020 grab = malloc(sizeof *grab);
1021 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001022 return;
1023
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001024 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1025 DESKTOP_SHELL_CURSOR_BUSY);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001026}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001027
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001028static void
1029end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1030{
1031 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1032
1033 if (grab->grab.interface == &busy_cursor_grab_interface) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001034 shell_grab_end(grab);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001035 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001036 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001037}
1038
Scott Moreau9521d5e2012-04-19 13:06:17 -06001039static void
1040ping_timer_destroy(struct shell_surface *shsurf)
1041{
1042 if (!shsurf || !shsurf->ping_timer)
1043 return;
1044
1045 if (shsurf->ping_timer->source)
1046 wl_event_source_remove(shsurf->ping_timer->source);
1047
1048 free(shsurf->ping_timer);
1049 shsurf->ping_timer = NULL;
1050}
1051
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001052static int
Scott Moreauff1db4a2012-04-17 19:06:18 -06001053ping_timeout_handler(void *data)
1054{
1055 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001056 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001057
Scott Moreau9521d5e2012-04-19 13:06:17 -06001058 /* Client is not responding */
1059 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001060
1061 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1062 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1063 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001064
1065 return 1;
1066}
1067
1068static void
1069ping_handler(struct weston_surface *surface, uint32_t serial)
1070{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001071 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001072 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001073 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001074
1075 if (!shsurf)
1076 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -04001077 if (!shsurf->resource.client)
1078 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001079
Ander Conselvan de Oliveiraeac9a462012-07-16 14:15:48 +03001080 if (shsurf->surface == shsurf->shell->grab_surface)
1081 return;
1082
Scott Moreauff1db4a2012-04-17 19:06:18 -06001083 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +03001084 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001085 if (!shsurf->ping_timer)
1086 return;
1087
1088 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001089 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1090 shsurf->ping_timer->source =
1091 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1092 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1093
1094 wl_shell_surface_send_ping(&shsurf->resource, serial);
1095 }
1096}
1097
1098static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001099handle_pointer_focus(struct wl_listener *listener, void *data)
1100{
1101 struct wl_pointer *pointer = data;
1102 struct weston_surface *surface =
1103 (struct weston_surface *) pointer->focus;
1104 struct weston_compositor *compositor;
1105 struct shell_surface *shsurf;
1106 uint32_t serial;
1107
1108 if (!surface)
1109 return;
1110
1111 compositor = surface->compositor;
1112 shsurf = get_shell_surface(surface);
1113
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +03001114 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001115 set_busy_cursor(shsurf, pointer);
1116 } else {
1117 serial = wl_display_next_serial(compositor->wl_display);
1118 ping_handler(surface, serial);
1119 }
1120}
1121
1122static void
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04001123create_pointer_focus_listener(struct weston_seat *seat)
1124{
1125 struct wl_listener *listener;
1126
1127 if (!seat->seat.pointer)
1128 return;
1129
1130 listener = malloc(sizeof *listener);
1131 listener->notify = handle_pointer_focus;
1132 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1133}
1134
1135static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06001136shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1137 uint32_t serial)
1138{
1139 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001140 struct desktop_shell *shell = shsurf->shell;
1141 struct weston_seat *seat;
1142 struct weston_compositor *ec = shsurf->surface->compositor;
1143 struct wl_pointer *pointer;
1144 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001145
Kristian Høgsberg92374e12012-08-11 22:39:12 -04001146 if (shsurf->ping_timer == NULL)
1147 /* Just ignore unsolicited pong. */
1148 return;
1149
Scott Moreauff1db4a2012-04-17 19:06:18 -06001150 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001151 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001152 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001153 if (was_unresponsive) {
1154 /* Received pong from previously unresponsive client */
1155 wl_list_for_each(seat, &ec->seat_list, link) {
1156 pointer = seat->seat.pointer;
1157 if (pointer->focus ==
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001158 &shell->grab_surface->surface &&
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001159 pointer->current ==
1160 &shsurf->surface->surface)
1161 end_busy_cursor(shsurf, pointer);
1162 }
1163 }
Scott Moreau9521d5e2012-04-19 13:06:17 -06001164 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -06001165 }
1166}
1167
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001168static void
1169shell_surface_set_title(struct wl_client *client,
1170 struct wl_resource *resource, const char *title)
1171{
1172 struct shell_surface *shsurf = resource->data;
1173
1174 free(shsurf->title);
1175 shsurf->title = strdup(title);
1176}
1177
1178static void
1179shell_surface_set_class(struct wl_client *client,
1180 struct wl_resource *resource, const char *class)
1181{
1182 struct shell_surface *shsurf = resource->data;
1183
1184 free(shsurf->class);
1185 shsurf->class = strdup(class);
1186}
1187
Juan Zhao96879df2012-02-07 08:45:41 +08001188static struct weston_output *
1189get_default_output(struct weston_compositor *compositor)
1190{
1191 return container_of(compositor->output_list.next,
1192 struct weston_output, link);
1193}
1194
Alex Wu4539b082012-03-01 12:57:46 +08001195static void
1196shell_unset_fullscreen(struct shell_surface *shsurf)
1197{
1198 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001199 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1200 shell_surface_is_top_fullscreen(shsurf)) {
1201 weston_output_switch_mode(shsurf->fullscreen_output,
1202 shsurf->fullscreen_output->origin);
1203 }
Alex Wu4539b082012-03-01 12:57:46 +08001204 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1205 shsurf->fullscreen.framerate = 0;
1206 wl_list_remove(&shsurf->fullscreen.transform.link);
1207 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001208 if (shsurf->fullscreen.black_surface)
1209 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001210 shsurf->fullscreen.black_surface = NULL;
1211 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001212 weston_surface_set_position(shsurf->surface,
1213 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001214 if (shsurf->saved_rotation_valid) {
1215 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1216 &shsurf->rotation.transform.link);
1217 shsurf->saved_rotation_valid = false;
1218 }
Alex Wu4539b082012-03-01 12:57:46 +08001219}
1220
Pekka Paalanen98262232011-12-01 10:42:22 +02001221static int
1222reset_shell_surface_type(struct shell_surface *surface)
1223{
1224 switch (surface->type) {
1225 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001226 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001227 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001228 case SHELL_SURFACE_MAXIMIZED:
1229 surface->output = get_default_output(surface->surface->compositor);
1230 weston_surface_set_position(surface->surface,
1231 surface->saved_x,
1232 surface->saved_y);
1233 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001234 case SHELL_SURFACE_NONE:
1235 case SHELL_SURFACE_TOPLEVEL:
1236 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001237 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001238 break;
1239 }
1240
1241 surface->type = SHELL_SURFACE_NONE;
1242 return 0;
1243}
1244
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001245static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001246set_surface_type(struct shell_surface *shsurf)
1247{
1248 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001249 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001250
1251 reset_shell_surface_type(shsurf);
1252
1253 shsurf->type = shsurf->next_type;
1254 shsurf->next_type = SHELL_SURFACE_NONE;
1255
1256 switch (shsurf->type) {
1257 case SHELL_SURFACE_TOPLEVEL:
1258 break;
1259 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001260 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001261 pes->geometry.x + shsurf->transient.x,
1262 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001263 break;
1264
1265 case SHELL_SURFACE_MAXIMIZED:
1266 shsurf->saved_x = surface->geometry.x;
1267 shsurf->saved_y = surface->geometry.y;
1268 shsurf->saved_position_valid = true;
1269 break;
1270
1271 case SHELL_SURFACE_FULLSCREEN:
1272 shsurf->saved_x = surface->geometry.x;
1273 shsurf->saved_y = surface->geometry.y;
1274 shsurf->saved_position_valid = true;
1275
1276 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1277 wl_list_remove(&shsurf->rotation.transform.link);
1278 wl_list_init(&shsurf->rotation.transform.link);
1279 shsurf->surface->geometry.dirty = 1;
1280 shsurf->saved_rotation_valid = true;
1281 }
1282 break;
1283
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001284 default:
1285 break;
1286 }
1287}
1288
1289static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001290set_toplevel(struct shell_surface *shsurf)
1291{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001292 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001293}
1294
1295static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001296shell_surface_set_toplevel(struct wl_client *client,
1297 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001298{
Pekka Paalanen98262232011-12-01 10:42:22 +02001299 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001300
Tiago Vignattibc052c92012-04-19 16:18:18 +03001301 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001302}
1303
1304static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001305set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001306 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001307{
1308 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001309 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001310 shsurf->transient.x = x;
1311 shsurf->transient.y = y;
1312 shsurf->transient.flags = flags;
1313 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1314}
1315
1316static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001317shell_surface_set_transient(struct wl_client *client,
1318 struct wl_resource *resource,
1319 struct wl_resource *parent_resource,
1320 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001321{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001322 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001323 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001324
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001325 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001326}
1327
Tiago Vignattibe143262012-04-16 17:31:41 +03001328static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001329shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001330{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001331 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001332}
1333
1334static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001335get_output_panel_height(struct desktop_shell *shell,
1336 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001337{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001338 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001339 int panel_height = 0;
1340
1341 if (!output)
1342 return 0;
1343
Juan Zhao4ab94682012-07-09 22:24:09 -07001344 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001345 if (surface->output == output) {
1346 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001347 break;
1348 }
1349 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001350
Juan Zhao96879df2012-02-07 08:45:41 +08001351 return panel_height;
1352}
1353
1354static void
1355shell_surface_set_maximized(struct wl_client *client,
1356 struct wl_resource *resource,
1357 struct wl_resource *output_resource )
1358{
1359 struct shell_surface *shsurf = resource->data;
1360 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001361 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001362 uint32_t edges = 0, panel_height = 0;
1363
1364 /* get the default output, if the client set it as NULL
1365 check whether the ouput is available */
1366 if (output_resource)
1367 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001368 else if (es->output)
1369 shsurf->output = es->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001370 else
1371 shsurf->output = get_default_output(es->compositor);
1372
Tiago Vignattibe143262012-04-16 17:31:41 +03001373 shell = shell_surface_get_shell(shsurf);
Rob Bradford31b68622012-07-02 19:00:19 +01001374 panel_height = get_output_panel_height(shell, shsurf->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001375 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001376
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001377 shsurf->client->send_configure(shsurf->surface, edges,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001378 shsurf->output->width,
1379 shsurf->output->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001380
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001381 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001382}
1383
Alex Wu21858432012-04-01 20:13:08 +08001384static void
1385black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1386
Alex Wu4539b082012-03-01 12:57:46 +08001387static struct weston_surface *
1388create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001389 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001390 GLfloat x, GLfloat y, int w, int h)
1391{
1392 struct weston_surface *surface = NULL;
1393
1394 surface = weston_surface_create(ec);
1395 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001396 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001397 return NULL;
1398 }
1399
Alex Wu21858432012-04-01 20:13:08 +08001400 surface->configure = black_surface_configure;
1401 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001402 weston_surface_configure(surface, x, y, w, h);
1403 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04001404 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
1405
Alex Wu4539b082012-03-01 12:57:46 +08001406 return surface;
1407}
1408
1409/* Create black surface and append it to the associated fullscreen surface.
1410 * Handle size dismatch and positioning according to the method. */
1411static void
1412shell_configure_fullscreen(struct shell_surface *shsurf)
1413{
1414 struct weston_output *output = shsurf->fullscreen_output;
1415 struct weston_surface *surface = shsurf->surface;
1416 struct weston_matrix *matrix;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001417 float scale, output_aspect, surface_aspect, x, y;
Alex Wu4539b082012-03-01 12:57:46 +08001418
Alex Wu4539b082012-03-01 12:57:46 +08001419 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001420 shsurf->fullscreen.black_surface =
1421 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001422 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001423 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001424 output->width,
1425 output->height);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001426
1427 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1428 wl_list_insert(&surface->layer_link,
1429 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001430 shsurf->fullscreen.black_surface->output = output;
1431
1432 switch (shsurf->fullscreen.type) {
1433 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
Kristian Høgsberga08b5282012-07-20 15:30:36 -04001434 if (surface->buffer)
1435 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08001436 break;
1437 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1438 matrix = &shsurf->fullscreen.transform.matrix;
1439 weston_matrix_init(matrix);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001440
Scott Moreau1bad5db2012-08-18 01:04:05 -06001441 output_aspect = (float) output->width /
1442 (float) output->height;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001443 surface_aspect = (float) surface->geometry.width /
1444 (float) surface->geometry.height;
1445 if (output_aspect < surface_aspect)
Scott Moreau1bad5db2012-08-18 01:04:05 -06001446 scale = (float) output->width /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001447 (float) surface->geometry.width;
1448 else
Scott Moreau1bad5db2012-08-18 01:04:05 -06001449 scale = (float) output->height /
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001450 (float) surface->geometry.height;
1451
Alex Wu4539b082012-03-01 12:57:46 +08001452 weston_matrix_scale(matrix, scale, scale, 1);
1453 wl_list_remove(&shsurf->fullscreen.transform.link);
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001454 wl_list_insert(&surface->geometry.transformation_list,
Alex Wu4539b082012-03-01 12:57:46 +08001455 &shsurf->fullscreen.transform.link);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001456 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1457 y = output->y + (output->height - surface->geometry.height * scale) / 2;
Kristian Høgsberg240dfeb2012-07-12 12:32:31 -04001458 weston_surface_set_position(surface, x, y);
1459
Alex Wu4539b082012-03-01 12:57:46 +08001460 break;
1461 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001462 if (shell_surface_is_top_fullscreen(shsurf)) {
1463 struct weston_mode mode = {0,
1464 surface->geometry.width,
1465 surface->geometry.height,
1466 shsurf->fullscreen.framerate};
1467
1468 if (weston_output_switch_mode(output, &mode) == 0) {
1469 weston_surface_configure(shsurf->fullscreen.black_surface,
1470 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001471 output->width,
1472 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001473 weston_surface_set_position(surface, output->x, output->y);
1474 break;
1475 }
1476 }
Alex Wu4539b082012-03-01 12:57:46 +08001477 break;
1478 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1479 break;
1480 default:
1481 break;
1482 }
1483}
1484
1485/* make the fullscreen and black surface at the top */
1486static void
1487shell_stack_fullscreen(struct shell_surface *shsurf)
1488{
Alex Wubd3354b2012-04-17 17:20:49 +08001489 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001490 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001491 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001492
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001493 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001494 wl_list_insert(&shell->fullscreen_layer.surface_list,
1495 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001496 weston_surface_damage(surface);
1497
1498 if (!shsurf->fullscreen.black_surface)
1499 shsurf->fullscreen.black_surface =
1500 create_black_surface(surface->compositor,
1501 surface,
1502 output->x, output->y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001503 output->width,
1504 output->height);
Alex Wubd3354b2012-04-17 17:20:49 +08001505
1506 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001507 wl_list_insert(&surface->layer_link,
1508 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001509 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001510}
1511
1512static void
1513shell_map_fullscreen(struct shell_surface *shsurf)
1514{
Alex Wu4539b082012-03-01 12:57:46 +08001515 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001516 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001517}
1518
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001519static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001520shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001521 struct wl_resource *resource,
1522 uint32_t method,
1523 uint32_t framerate,
1524 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001525{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001526 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001527 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001528
1529 if (output_resource)
1530 shsurf->output = output_resource->data;
Kristian Høgsberg94de6802012-07-18 09:54:04 -04001531 else if (es->output)
1532 shsurf->output = es->output;
Alex Wu4539b082012-03-01 12:57:46 +08001533 else
1534 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001535
Alex Wu4539b082012-03-01 12:57:46 +08001536 shsurf->fullscreen_output = shsurf->output;
1537 shsurf->fullscreen.type = method;
1538 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001539 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001540
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001541 shsurf->client->send_configure(shsurf->surface, 0,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001542 shsurf->output->width,
1543 shsurf->output->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001544}
1545
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001546static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001547popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001548 struct wl_surface *surface,
1549 wl_fixed_t x,
1550 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001551{
Daniel Stone37816df2012-05-16 18:45:18 +01001552 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001553 struct shell_surface *priv =
1554 container_of(grab, struct shell_surface, popup.grab);
1555 struct wl_client *client = priv->surface->surface.resource.client;
1556
Pekka Paalanencb108432012-01-19 16:25:40 +02001557 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001558 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001559 grab->focus = surface;
1560 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001561 wl_pointer_set_focus(pointer, NULL,
1562 wl_fixed_from_int(0),
1563 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001564 grab->focus = NULL;
1565 }
1566}
1567
1568static void
Scott Moreau447013d2012-02-18 05:05:29 -07001569popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001570 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001571{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001572 struct wl_resource *resource;
1573
Daniel Stone37816df2012-05-16 18:45:18 +01001574 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001575 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001576 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001577}
1578
1579static void
Scott Moreau447013d2012-02-18 05:05:29 -07001580popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001581 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001582{
1583 struct wl_resource *resource;
1584 struct shell_surface *shsurf =
1585 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001586 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001587 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001588 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001589
Daniel Stone37816df2012-05-16 18:45:18 +01001590 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001591 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001592 display = wl_client_get_display(resource->client);
1593 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001594 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001595 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001596 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001597 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001598 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001599 wl_pointer_end_grab(grab->pointer);
1600 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001601 }
1602
Daniel Stone4dbadb12012-05-30 16:31:51 +01001603 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001604 shsurf->popup.initial_up = 1;
1605}
1606
Scott Moreau447013d2012-02-18 05:05:29 -07001607static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001608 popup_grab_focus,
1609 popup_grab_motion,
1610 popup_grab_button,
1611};
1612
1613static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001614shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001615{
Daniel Stone37816df2012-05-16 18:45:18 +01001616 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001617 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001618 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001619
1620 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001621 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001622
Pekka Paalanen938269a2012-02-07 14:19:01 +02001623 weston_surface_update_transform(parent);
1624 if (parent->transform.enabled) {
1625 shsurf->popup.parent_transform.matrix =
1626 parent->transform.matrix;
1627 } else {
1628 /* construct x, y translation matrix */
1629 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1630 shsurf->popup.parent_transform.matrix.d[12] =
1631 parent->geometry.x;
1632 shsurf->popup.parent_transform.matrix.d[13] =
1633 parent->geometry.y;
1634 }
1635 wl_list_insert(es->geometry.transformation_list.prev,
1636 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001637 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001638
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001639 shsurf->popup.initial_up = 0;
1640
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001641 /* We don't require the grab to still be active, but if another
1642 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001643 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1644 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001645 } else {
1646 wl_shell_surface_send_popup_done(&shsurf->resource);
1647 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001648}
1649
1650static void
1651shell_surface_set_popup(struct wl_client *client,
1652 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001653 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001654 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001655 struct wl_resource *parent_resource,
1656 int32_t x, int32_t y, uint32_t flags)
1657{
1658 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001659
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001660 shsurf->type = SHELL_SURFACE_POPUP;
1661 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001662 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001663 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001664 shsurf->popup.x = x;
1665 shsurf->popup.y = y;
1666}
1667
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001668static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001669 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001670 shell_surface_move,
1671 shell_surface_resize,
1672 shell_surface_set_toplevel,
1673 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001674 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001675 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001676 shell_surface_set_maximized,
1677 shell_surface_set_title,
1678 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001679};
1680
1681static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001682destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001683{
Daniel Stone37816df2012-05-16 18:45:18 +01001684 if (shsurf->popup.grab.pointer)
1685 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001686
Alex Wubd3354b2012-04-17 17:20:49 +08001687 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1688 shell_surface_is_top_fullscreen(shsurf)) {
1689 weston_output_switch_mode(shsurf->fullscreen_output,
1690 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001691 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001692
Alex Wuaa08e2d2012-03-05 11:01:40 +08001693 if (shsurf->fullscreen.black_surface)
1694 weston_surface_destroy(shsurf->fullscreen.black_surface);
1695
Alex Wubd3354b2012-04-17 17:20:49 +08001696 /* As destroy_resource() use wl_list_for_each_safe(),
1697 * we can always remove the listener.
1698 */
1699 wl_list_remove(&shsurf->surface_destroy_listener.link);
1700 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001701 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001702
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001703 wl_list_remove(&shsurf->link);
1704 free(shsurf);
1705}
1706
1707static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001708shell_destroy_shell_surface(struct wl_resource *resource)
1709{
1710 struct shell_surface *shsurf = resource->data;
1711
1712 destroy_shell_surface(shsurf);
1713}
1714
1715static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001716shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001717{
1718 struct shell_surface *shsurf = container_of(listener,
1719 struct shell_surface,
1720 surface_destroy_listener);
1721
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001722 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001723 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001724 } else {
1725 wl_signal_emit(&shsurf->resource.destroy_signal,
1726 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001727 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001728 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001729}
1730
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001731static void
1732shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1733
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001734static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001735get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001736{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001737 if (surface->configure == shell_surface_configure)
1738 return surface->private;
1739 else
1740 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001741}
1742
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001743static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001744create_shell_surface(void *shell, struct weston_surface *surface,
1745 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001746{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001747 struct shell_surface *shsurf;
1748
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001749 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001750 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001751 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001752 }
1753
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001754 shsurf = calloc(1, sizeof *shsurf);
1755 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001756 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001757 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001758 }
1759
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001760 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001761 surface->private = shsurf;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001762
Tiago Vignattibc052c92012-04-19 16:18:18 +03001763 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001764 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001765 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001766 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001767 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001768 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1769 shsurf->fullscreen.framerate = 0;
1770 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001771 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001772 wl_list_init(&shsurf->fullscreen.transform.link);
1773
Tiago Vignattibc052c92012-04-19 16:18:18 +03001774 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001775 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1776 wl_signal_add(&surface->surface.resource.destroy_signal,
1777 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001778
1779 /* init link so its safe to always remove it in destroy_shell_surface */
1780 wl_list_init(&shsurf->link);
1781
Pekka Paalanen460099f2012-01-20 16:48:25 +02001782 /* empty when not in use */
1783 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001784 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001785
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001786 wl_list_init(&shsurf->workspace_transform.link);
1787
Pekka Paalanen98262232011-12-01 10:42:22 +02001788 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001789 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001790
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001791 shsurf->client = client;
1792
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001793 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001794}
1795
1796static void
1797shell_get_shell_surface(struct wl_client *client,
1798 struct wl_resource *resource,
1799 uint32_t id,
1800 struct wl_resource *surface_resource)
1801{
1802 struct weston_surface *surface = surface_resource->data;
1803 struct desktop_shell *shell = resource->data;
1804 struct shell_surface *shsurf;
1805
1806 if (get_shell_surface(surface)) {
1807 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001808 WL_DISPLAY_ERROR_INVALID_OBJECT,
1809 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001810 return;
1811 }
1812
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001813 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001814 if (!shsurf) {
1815 wl_resource_post_error(surface_resource,
1816 WL_DISPLAY_ERROR_INVALID_OBJECT,
1817 "surface->configure already set");
1818 return;
1819 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001820
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001821 shsurf->resource.destroy = shell_destroy_shell_surface;
1822 shsurf->resource.object.id = id;
1823 shsurf->resource.object.interface = &wl_shell_surface_interface;
1824 shsurf->resource.object.implementation =
1825 (void (**)(void)) &shell_surface_implementation;
1826 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001827
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001828 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001829}
1830
1831static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001832 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001833};
1834
Kristian Høgsberg07937562011-04-12 17:25:42 -04001835static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001836handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001837{
1838 proc->pid = 0;
1839}
1840
1841static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001842launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001843{
1844 if (shell->screensaver.binding)
1845 return;
1846
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001847 if (!shell->screensaver.path)
1848 return;
1849
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001850 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001851 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001852 return;
1853 }
1854
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001855 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001856 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001857 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001858 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001859}
1860
1861static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001862terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001863{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001864 if (shell->screensaver.process.pid == 0)
1865 return;
1866
1867 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001868}
1869
1870static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001871configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001872{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001873 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001874
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001875 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1876 if (s->output == es->output && s != es) {
1877 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001878 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001879 }
1880 }
1881
1882 weston_surface_configure(es, es->output->x, es->output->y,
1883 es->buffer->width, es->buffer->height);
1884
1885 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001886 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001887 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001888 }
1889}
1890
1891static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001892background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1893{
1894 struct desktop_shell *shell = es->private;
1895
1896 configure_static_surface(es, &shell->background_layer);
1897}
1898
1899static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001900desktop_shell_set_background(struct wl_client *client,
1901 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001902 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001903 struct wl_resource *surface_resource)
1904{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001905 struct desktop_shell *shell = resource->data;
1906 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001907
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001908 if (surface->configure) {
1909 wl_resource_post_error(surface_resource,
1910 WL_DISPLAY_ERROR_INVALID_OBJECT,
1911 "surface role already assigned");
1912 return;
1913 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001914
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001915 surface->configure = background_configure;
1916 surface->private = shell;
1917 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001918 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001919 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001920 surface->output->width,
1921 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001922}
1923
1924static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001925panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1926{
1927 struct desktop_shell *shell = es->private;
1928
1929 configure_static_surface(es, &shell->panel_layer);
1930}
1931
1932static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001933desktop_shell_set_panel(struct wl_client *client,
1934 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001935 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001936 struct wl_resource *surface_resource)
1937{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001938 struct desktop_shell *shell = resource->data;
1939 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001940
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001941 if (surface->configure) {
1942 wl_resource_post_error(surface_resource,
1943 WL_DISPLAY_ERROR_INVALID_OBJECT,
1944 "surface role already assigned");
1945 return;
1946 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001947
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001948 surface->configure = panel_configure;
1949 surface->private = shell;
1950 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001951 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001952 surface_resource,
Scott Moreau1bad5db2012-08-18 01:04:05 -06001953 surface->output->width,
1954 surface->output->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001955}
1956
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001957static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001958lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1959{
1960 struct desktop_shell *shell = surface->private;
1961
1962 center_on_output(surface, get_default_output(shell->compositor));
1963
1964 if (!weston_surface_is_mapped(surface)) {
1965 wl_list_insert(&shell->lock_layer.surface_list,
1966 &surface->layer_link);
1967 weston_surface_assign_output(surface);
1968 weston_compositor_wake(shell->compositor);
1969 }
1970}
1971
1972static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001973handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001974{
Tiago Vignattibe143262012-04-16 17:31:41 +03001975 struct desktop_shell *shell =
1976 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001977
Martin Minarik6d118362012-06-07 18:01:59 +02001978 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001979 shell->lock_surface = NULL;
1980}
1981
1982static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001983desktop_shell_set_lock_surface(struct wl_client *client,
1984 struct wl_resource *resource,
1985 struct wl_resource *surface_resource)
1986{
Tiago Vignattibe143262012-04-16 17:31:41 +03001987 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001988 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001989
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001990 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001991
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001992 if (!shell->locked)
1993 return;
1994
Pekka Paalanen98262232011-12-01 10:42:22 +02001995 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001996
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001997 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1998 wl_signal_add(&surface_resource->destroy_signal,
1999 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002000
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002001 surface->configure = lock_surface_configure;
2002 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002003}
2004
2005static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002006resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002007{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002008 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002009 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002010
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002011 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
2012 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002013
2014 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002015
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002016 wl_list_remove(&shell->lock_layer.link);
2017 wl_list_insert(&shell->compositor->cursor_layer.link,
2018 &shell->fullscreen_layer.link);
2019 wl_list_insert(&shell->fullscreen_layer.link,
2020 &shell->panel_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002021 if (shell->showing_input_panels) {
2022 wl_list_insert(&shell->panel_layer.link,
2023 &shell->input_panel_layer.link);
2024 wl_list_insert(&shell->input_panel_layer.link,
2025 &ws->layer.link);
2026 } else {
2027 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2028 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002029
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002030 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02002031
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002032 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002033 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002034 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002035 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002036}
2037
2038static void
2039desktop_shell_unlock(struct wl_client *client,
2040 struct wl_resource *resource)
2041{
Tiago Vignattibe143262012-04-16 17:31:41 +03002042 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002043
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002044 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002045
2046 if (shell->locked)
2047 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002048}
2049
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002050static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002051desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002052 struct wl_resource *resource,
2053 struct wl_resource *surface_resource)
2054{
2055 struct desktop_shell *shell = resource->data;
2056
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002057 shell->grab_surface = surface_resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002058}
2059
Kristian Høgsberg75840622011-09-06 13:48:16 -04002060static const struct desktop_shell_interface desktop_shell_implementation = {
2061 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002062 desktop_shell_set_panel,
2063 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002064 desktop_shell_unlock,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002065 desktop_shell_set_grab_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002066};
2067
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002068static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002069get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002070{
2071 struct shell_surface *shsurf;
2072
2073 shsurf = get_shell_surface(surface);
2074 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002075 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002076 return shsurf->type;
2077}
2078
Kristian Høgsberg75840622011-09-06 13:48:16 -04002079static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002080move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002081{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002082 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002083 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002084 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002085
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002086 if (surface == NULL)
2087 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002088
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002089 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002090 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002091 return;
2092
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002093 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002094}
2095
2096static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002097resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002098{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002099 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002100 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002101 uint32_t edges = 0;
2102 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002103 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002104
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002105 if (surface == NULL)
2106 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002107
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002108 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002109 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002110 return;
2111
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002112 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002113 wl_fixed_to_int(seat->pointer->grab_x),
2114 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002115 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002116
Pekka Paalanen60921e52012-01-25 15:55:43 +02002117 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002118 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002119 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002120 edges |= 0;
2121 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002122 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002123
Pekka Paalanen60921e52012-01-25 15:55:43 +02002124 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002125 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002126 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002127 edges |= 0;
2128 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002129 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002130
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002131 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002132}
2133
2134static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002135surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002136 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002137{
Scott Moreau02709af2012-05-22 01:54:10 -06002138 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002139 struct shell_surface *shsurf;
2140 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002141 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002142
2143 if (surface == NULL)
2144 return;
2145
2146 shsurf = get_shell_surface(surface);
2147 if (!shsurf)
2148 return;
2149
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002150 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002151
Scott Moreau02709af2012-05-22 01:54:10 -06002152 if (surface->alpha > 1.0)
2153 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002154 if (surface->alpha < step)
2155 surface->alpha = step;
2156
2157 surface->geometry.dirty = 1;
2158 weston_surface_damage(surface);
2159}
2160
2161static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002162do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002163 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002164{
Daniel Stone37816df2012-05-16 18:45:18 +01002165 struct weston_seat *ws = (struct weston_seat *) seat;
2166 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002167 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002168 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002169
2170 wl_list_for_each(output, &compositor->output_list, link) {
2171 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002172 wl_fixed_to_double(seat->pointer->x),
2173 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002174 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002175 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002176 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002177 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002178 increment = -output->zoom.increment;
2179 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002180 increment = output->zoom.increment *
2181 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002182 else
2183 increment = 0;
2184
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002185 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002186
Scott Moreaue6603982012-06-11 13:07:51 -06002187 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002188 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002189 else if (output->zoom.level > output->zoom.max_level)
2190 output->zoom.level = output->zoom.max_level;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002191 else {
Scott Moreaue6603982012-06-11 13:07:51 -06002192 output->zoom.active = 1;
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002193 output->disable_planes++;
2194 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07002195
Scott Moreaue6603982012-06-11 13:07:51 -06002196 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002197
Scott Moreau8dacaab2012-06-17 18:10:58 -06002198 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002199 }
2200 }
2201}
2202
Scott Moreauccbf29d2012-02-22 14:21:41 -07002203static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002204zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002205 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002206{
2207 do_zoom(seat, time, 0, axis, value);
2208}
2209
2210static void
2211zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2212 void *data)
2213{
2214 do_zoom(seat, time, key, 0, 0);
2215}
2216
2217static void
2218terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2219 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002220{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002221 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002222
Daniel Stone325fc2d2012-05-30 16:31:58 +01002223 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002224}
2225
2226static void
Scott Moreau447013d2012-02-18 05:05:29 -07002227rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002228 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002229{
2230 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002231 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002232 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002233 struct shell_surface *shsurf = rotate->base.shsurf;
2234 struct weston_surface *surface;
2235 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2236
2237 if (!shsurf)
2238 return;
2239
2240 surface = shsurf->surface;
2241
2242 cx = 0.5f * surface->geometry.width;
2243 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002244
Daniel Stone37816df2012-05-16 18:45:18 +01002245 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2246 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002247 r = sqrtf(dx * dx + dy * dy);
2248
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002249 wl_list_remove(&shsurf->rotation.transform.link);
2250 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002251
2252 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002253 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002254 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002255
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002256 weston_matrix_init(&rotate->rotation);
2257 rotate->rotation.d[0] = dx / r;
2258 rotate->rotation.d[4] = -dy / r;
2259 rotate->rotation.d[1] = -rotate->rotation.d[4];
2260 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002261
2262 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002263 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002264 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002265 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002266 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002267
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002268 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002269 &shsurf->surface->geometry.transformation_list,
2270 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002271 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002272 wl_list_init(&shsurf->rotation.transform.link);
2273 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002274 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002275 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002276
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002277 /* We need to adjust the position of the surface
2278 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002279 cposx = surface->geometry.x + cx;
2280 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002281 dposx = rotate->center.x - cposx;
2282 dposy = rotate->center.y - cposy;
2283 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002284 weston_surface_set_position(surface,
2285 surface->geometry.x + dposx,
2286 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002287 }
2288
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002289 /* Repaint implies weston_surface_update_transform(), which
2290 * lazily applies the damage due to rotation update.
2291 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002292 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002293}
2294
2295static void
Scott Moreau447013d2012-02-18 05:05:29 -07002296rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002297 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002298{
2299 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002300 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002301 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002302 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002303 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002304
Daniel Stone4dbadb12012-05-30 16:31:51 +01002305 if (pointer->button_count == 0 &&
2306 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002307 if (shsurf)
2308 weston_matrix_multiply(&shsurf->rotation.rotation,
2309 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002310 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002311 free(rotate);
2312 }
2313}
2314
Scott Moreau447013d2012-02-18 05:05:29 -07002315static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002316 noop_grab_focus,
2317 rotate_grab_motion,
2318 rotate_grab_button,
2319};
2320
2321static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002322rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002323 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002324{
2325 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002326 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002327 struct shell_surface *surface;
2328 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002329 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002330 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002331
2332 if (base_surface == NULL)
2333 return;
2334
2335 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002336 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002337 return;
2338
Pekka Paalanen460099f2012-01-20 16:48:25 +02002339 rotate = malloc(sizeof *rotate);
2340 if (!rotate)
2341 return;
2342
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002343 weston_surface_to_global_float(surface->surface,
2344 surface->surface->geometry.width / 2,
2345 surface->surface->geometry.height / 2,
2346 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002347
Daniel Stone37816df2012-05-16 18:45:18 +01002348 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2349 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002350 r = sqrtf(dx * dx + dy * dy);
2351 if (r > 20.0f) {
2352 struct weston_matrix inverse;
2353
2354 weston_matrix_init(&inverse);
2355 inverse.d[0] = dx / r;
2356 inverse.d[4] = dy / r;
2357 inverse.d[1] = -inverse.d[4];
2358 inverse.d[5] = inverse.d[0];
2359 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002360
2361 weston_matrix_init(&rotate->rotation);
2362 rotate->rotation.d[0] = dx / r;
2363 rotate->rotation.d[4] = -dy / r;
2364 rotate->rotation.d[1] = -rotate->rotation.d[4];
2365 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002366 } else {
2367 weston_matrix_init(&surface->rotation.rotation);
2368 weston_matrix_init(&rotate->rotation);
2369 }
2370
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03002371 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2372 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002373}
2374
2375static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002376lower_fullscreen_layer(struct desktop_shell *shell)
2377{
2378 struct workspace *ws;
2379 struct weston_surface *surface, *prev;
2380
2381 ws = get_current_workspace(shell);
2382 wl_list_for_each_reverse_safe(surface, prev,
2383 &shell->fullscreen_layer.surface_list,
2384 layer_link)
2385 weston_surface_restack(surface, &ws->layer.surface_list);
2386}
2387
2388static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002389activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002390 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002391{
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002392 struct workspace *ws = get_current_workspace(shell);
2393 struct focus_state *state;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002394
Daniel Stone37816df2012-05-16 18:45:18 +01002395 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002396
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04002397 wl_list_for_each(state, &ws->focus_list, link)
2398 if (state->seat == seat)
2399 break;
2400
2401 if (&state->link == &ws->focus_list) {
2402 state = focus_state_create(seat, ws);
2403 if (state == NULL)
2404 return;
2405 }
2406
2407 state->keyboard_focus = es;
2408 wl_list_remove(&state->surface_destroy_listener.link);
2409 wl_signal_add(&es->surface.resource.destroy_signal,
2410 &state->surface_destroy_listener);
2411
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002412 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002413 case SHELL_SURFACE_FULLSCREEN:
2414 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002415 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002416 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002417 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002418 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002419 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002420 lower_fullscreen_layer(shell);
2421 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002422 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002423 }
2424}
2425
Alex Wu21858432012-04-01 20:13:08 +08002426/* no-op func for checking black surface */
2427static void
2428black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2429{
2430}
2431
2432static bool
2433is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2434{
2435 if (es->configure == black_surface_configure) {
2436 if (fs_surface)
2437 *fs_surface = (struct weston_surface *)es->private;
2438 return true;
2439 }
2440 return false;
2441}
2442
Kristian Høgsberg75840622011-09-06 13:48:16 -04002443static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002444click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002445 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002446{
Daniel Stone37816df2012-05-16 18:45:18 +01002447 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002448 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002449 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002450 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002451
Daniel Stone37816df2012-05-16 18:45:18 +01002452 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002453 if (!focus)
2454 return;
2455
Alex Wu21858432012-04-01 20:13:08 +08002456 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002457 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002458
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002459 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2460 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002461
Daniel Stone325fc2d2012-05-30 16:31:58 +01002462 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002463 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002464}
2465
2466static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002467lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002468{
Tiago Vignattibe143262012-04-16 17:31:41 +03002469 struct desktop_shell *shell =
2470 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002471 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002472 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002473
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002474 if (shell->locked) {
2475 wl_list_for_each(output, &shell->compositor->output_list, link)
2476 /* TODO: find a way to jump to other DPMS levels */
2477 if (output->set_dpms)
2478 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002479 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002480 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002481
2482 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002483
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002484 /* Hide all surfaces by removing the fullscreen, panel and
2485 * toplevel layers. This way nothing else can show or receive
2486 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002487
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002488 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002489 wl_list_remove(&shell->fullscreen_layer.link);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002490 if (shell->showing_input_panels)
2491 wl_list_remove(&shell->input_panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002492 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002493 wl_list_insert(&shell->compositor->cursor_layer.link,
2494 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002495
Pekka Paalanen77346a62011-11-30 16:26:35 +02002496 launch_screensaver(shell);
2497
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002498 /* TODO: disable bindings that should not work while locked. */
2499
2500 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002501}
2502
2503static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002504unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002505{
Tiago Vignattibe143262012-04-16 17:31:41 +03002506 struct desktop_shell *shell =
2507 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002508
Pekka Paalanend81c2162011-11-16 13:47:34 +02002509 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002510 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002511 return;
2512 }
2513
2514 /* If desktop-shell client has gone away, unlock immediately. */
2515 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002516 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002517 return;
2518 }
2519
2520 if (shell->prepare_event_sent)
2521 return;
2522
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002523 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002524 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002525}
2526
2527static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002528show_input_panels(struct wl_listener *listener, void *data)
2529{
2530 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002531 container_of(listener, struct desktop_shell,
2532 show_input_panel_listener);
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002533 struct input_panel_surface *surface, *next;
2534 struct weston_surface *ws;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002535
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002536 shell->showing_input_panels = true;
2537
2538 wl_list_insert(&shell->panel_layer.link,
2539 &shell->input_panel_layer.link);
2540
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002541 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002542 &shell->input_panel.surfaces, link) {
2543 ws = surface->surface;
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002544 wl_list_insert(&shell->input_panel_layer.surface_list,
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002545 &ws->layer_link);
2546 weston_surface_assign_output(ws);
2547 weston_surface_damage(ws);
2548 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002549 }
2550}
2551
2552static void
2553hide_input_panels(struct wl_listener *listener, void *data)
2554{
2555 struct desktop_shell *shell =
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002556 container_of(listener, struct desktop_shell,
2557 hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002558 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002559
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002560 shell->showing_input_panels = false;
2561
2562 wl_list_remove(&shell->input_panel_layer.link);
2563
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002564 wl_list_for_each_safe(surface, next,
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02002565 &shell->input_panel_layer.surface_list, layer_link)
2566 weston_surface_unmap(surface);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002567}
2568
2569static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002570center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002571{
Scott Moreau1bad5db2012-08-18 01:04:05 -06002572 GLfloat x = (output->width - surface->buffer->width) / 2;
2573 GLfloat y = (output->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002574
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002575 weston_surface_configure(surface, output->x + x, output->y + y,
2576 surface->buffer->width,
2577 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002578}
2579
2580static void
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002581weston_surface_set_initial_position (struct weston_surface *surface,
2582 struct desktop_shell *shell)
2583{
2584 struct weston_compositor *compositor = shell->compositor;
2585 int ix = 0, iy = 0;
2586 int range_x, range_y;
2587 int dx, dy, x, y, panel_height;
2588 struct weston_output *output, *target_output = NULL;
2589 struct weston_seat *seat;
2590
2591 /* As a heuristic place the new window on the same output as the
2592 * pointer. Falling back to the output containing 0, 0.
2593 *
2594 * TODO: Do something clever for touch too?
2595 */
2596 wl_list_for_each(seat, &compositor->seat_list, link) {
2597 if (seat->has_pointer) {
2598 ix = wl_fixed_to_int(seat->pointer.x);
2599 iy = wl_fixed_to_int(seat->pointer.y);
2600 break;
2601 }
2602 }
2603
2604 wl_list_for_each(output, &compositor->output_list, link) {
2605 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2606 target_output = output;
2607 break;
2608 }
2609 }
2610
2611 if (!target_output) {
2612 weston_surface_set_position(surface, 10 + random() % 400,
2613 10 + random() % 400);
2614 return;
2615 }
2616
2617 /* Valid range within output where the surface will still be onscreen.
2618 * If this is negative it means that the surface is bigger than
2619 * output.
2620 */
2621 panel_height = get_output_panel_height(shell, target_output);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002622 range_x = target_output->width - surface->geometry.width;
2623 range_y = (target_output->height - panel_height) -
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002624 surface->geometry.height;
2625
Rob Bradford4cb88c72012-08-13 15:18:44 +01002626 if (range_x > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002627 dx = random() % range_x;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002628 else
Rob Bradford4cb88c72012-08-13 15:18:44 +01002629 dx = 0;
2630
2631 if (range_y > 0)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002632 dy = panel_height + random() % range_y;
Rob Bradford4cb88c72012-08-13 15:18:44 +01002633 else
2634 dy = panel_height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002635
2636 x = target_output->x + dx;
2637 y = target_output->y + dy;
2638
2639 weston_surface_set_position (surface, x, y);
2640}
2641
2642static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002643map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002644 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002645{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002646 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002647 struct shell_surface *shsurf = get_shell_surface(surface);
2648 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002649 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002650 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002651 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002652 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002653
Pekka Paalanen60921e52012-01-25 15:55:43 +02002654 surface->geometry.width = width;
2655 surface->geometry.height = height;
2656 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002657
2658 /* initial positioning, see also configure() */
2659 switch (surface_type) {
2660 case SHELL_SURFACE_TOPLEVEL:
Rob Bradfordac63e5b2012-08-13 14:07:52 +01002661 weston_surface_set_initial_position(surface, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002662 break;
Alex Wu4539b082012-03-01 12:57:46 +08002663 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberge4d3a2b2012-07-09 21:43:22 -04002664 center_on_output(surface, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002665 shell_map_fullscreen(shsurf);
2666 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002667 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002668 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002669 panel_height = get_output_panel_height(shell,surface->output);
Rob Bradford31b68622012-07-02 19:00:19 +01002670 weston_surface_set_position(surface, shsurf->output->x,
2671 shsurf->output->y + panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08002672 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002673 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002674 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002675 case SHELL_SURFACE_NONE:
2676 weston_surface_set_position(surface,
2677 surface->geometry.x + sx,
2678 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002679 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002680 default:
2681 ;
2682 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002683
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002684 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002685 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002686 case SHELL_SURFACE_POPUP:
2687 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002688 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002689 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2690 break;
Alex Wu4539b082012-03-01 12:57:46 +08002691 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002692 case SHELL_SURFACE_NONE:
2693 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002694 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002695 ws = get_current_workspace(shell);
2696 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002697 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002698 }
2699
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002700 if (surface_type != SHELL_SURFACE_NONE) {
2701 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002702 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2703 surface->output = shsurf->output;
2704 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002705
Juan Zhao7bb92f02011-12-15 11:31:51 -05002706 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002707 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002708 if (shsurf->transient.flags ==
2709 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2710 break;
2711 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002712 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002713 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002714 if (!shell->locked) {
2715 wl_list_for_each(seat, &compositor->seat_list, link)
2716 activate(shell, surface, seat);
2717 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002718 break;
2719 default:
2720 break;
2721 }
2722
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002723 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002724 {
2725 switch (shell->win_animation_type) {
2726 case ANIMATION_FADE:
2727 weston_fade_run(surface, NULL, NULL);
2728 break;
2729 case ANIMATION_ZOOM:
2730 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2731 break;
2732 default:
2733 break;
2734 }
2735 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002736}
2737
2738static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002739configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002740 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002741{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002742 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2743 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002744
Pekka Paalanen77346a62011-11-30 16:26:35 +02002745 shsurf = get_shell_surface(surface);
2746 if (shsurf)
2747 surface_type = shsurf->type;
2748
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002749 surface->geometry.x = x;
2750 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002751 surface->geometry.width = width;
2752 surface->geometry.height = height;
2753 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002754
2755 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002756 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002757 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002758 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002759 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002760 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002761 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002762 surface->geometry.x = surface->output->x;
2763 surface->geometry.y = surface->output->y +
2764 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002765 break;
Alex Wu4539b082012-03-01 12:57:46 +08002766 case SHELL_SURFACE_TOPLEVEL:
2767 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002768 default:
2769 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002770 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002771
Alex Wu4539b082012-03-01 12:57:46 +08002772 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002773 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002774 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002775
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002776 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08002777 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002778 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002779}
2780
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002781static void
2782shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2783{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002784 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002785 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002786 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002787
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002788 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002789 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002790 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002791 type_changed = 1;
2792 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002793
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002794 if (!weston_surface_is_mapped(es)) {
2795 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002796 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002797 es->geometry.width != es->buffer->width ||
2798 es->geometry.height != es->buffer->height) {
2799 GLfloat from_x, from_y;
2800 GLfloat to_x, to_y;
2801
2802 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2803 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2804 configure(shell, es,
2805 es->geometry.x + to_x - from_x,
2806 es->geometry.y + to_y - from_y,
2807 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002808 }
2809}
2810
Tiago Vignattibe143262012-04-16 17:31:41 +03002811static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002812
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002813static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002814desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002815{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002816 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002817 struct desktop_shell *shell =
2818 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002819
2820 shell->child.process.pid = 0;
2821 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002822
2823 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2824 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002825 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002826 shell->child.deathstamp = time;
2827 shell->child.deathcount = 0;
2828 }
2829
2830 shell->child.deathcount++;
2831 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002832 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002833 return;
2834 }
2835
Martin Minarik6d118362012-06-07 18:01:59 +02002836 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002837 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002838}
2839
2840static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002841launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002842{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002843 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002844
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002845 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002846 &shell->child.process,
2847 shell_exe,
2848 desktop_shell_sigchld);
2849
2850 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002851 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002852 return 0;
2853}
2854
2855static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002856bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2857{
Tiago Vignattibe143262012-04-16 17:31:41 +03002858 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002859
2860 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002861 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002862}
2863
Kristian Høgsberg75840622011-09-06 13:48:16 -04002864static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002865unbind_desktop_shell(struct wl_resource *resource)
2866{
Tiago Vignattibe143262012-04-16 17:31:41 +03002867 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002868
2869 if (shell->locked)
2870 resume_desktop(shell);
2871
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002872 shell->child.desktop_shell = NULL;
2873 shell->prepare_event_sent = false;
2874 free(resource);
2875}
2876
2877static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002878bind_desktop_shell(struct wl_client *client,
2879 void *data, uint32_t version, uint32_t id)
2880{
Tiago Vignattibe143262012-04-16 17:31:41 +03002881 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002882 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002883
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002884 resource = wl_client_add_object(client, &desktop_shell_interface,
2885 &desktop_shell_implementation,
2886 id, shell);
2887
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002888 if (client == shell->child.client) {
2889 resource->destroy = unbind_desktop_shell;
2890 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002891 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002892 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002893
2894 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2895 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002896 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002897}
2898
Pekka Paalanen6e168112011-11-24 11:34:05 +02002899static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002900screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2901{
2902 struct desktop_shell *shell = surface->private;
2903
2904 if (!shell->locked)
2905 return;
2906
2907 center_on_output(surface, surface->output);
2908
2909 if (wl_list_empty(&surface->layer_link)) {
2910 wl_list_insert(shell->lock_layer.surface_list.prev,
2911 &surface->layer_link);
2912 weston_surface_assign_output(surface);
2913 shell->compositor->idle_time = shell->screensaver.duration;
2914 weston_compositor_wake(shell->compositor);
2915 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2916 }
2917}
2918
2919static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02002920screensaver_set_surface(struct wl_client *client,
2921 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002922 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02002923 struct wl_resource *output_resource)
2924{
Tiago Vignattibe143262012-04-16 17:31:41 +03002925 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002926 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002927 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002928
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002929 surface->configure = screensaver_configure;
2930 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002931 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002932}
2933
2934static const struct screensaver_interface screensaver_implementation = {
2935 screensaver_set_surface
2936};
2937
2938static void
2939unbind_screensaver(struct wl_resource *resource)
2940{
Tiago Vignattibe143262012-04-16 17:31:41 +03002941 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002942
Pekka Paalanen77346a62011-11-30 16:26:35 +02002943 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002944 free(resource);
2945}
2946
2947static void
2948bind_screensaver(struct wl_client *client,
2949 void *data, uint32_t version, uint32_t id)
2950{
Tiago Vignattibe143262012-04-16 17:31:41 +03002951 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002952 struct wl_resource *resource;
2953
2954 resource = wl_client_add_object(client, &screensaver_interface,
2955 &screensaver_implementation,
2956 id, shell);
2957
Pekka Paalanen77346a62011-11-30 16:26:35 +02002958 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002959 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002960 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002961 return;
2962 }
2963
2964 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2965 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002966 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002967}
2968
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002969static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002970input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2971{
2972 struct weston_mode *mode = surface->output->current;
2973 GLfloat x = (mode->width - surface->buffer->width) / 2;
2974 GLfloat y = mode->height - surface->buffer->height;
2975
2976 /* Don't map the input panel here, wait for
2977 * show_input_panels signal. */
2978
2979 weston_surface_configure(surface,
2980 surface->output->x + x,
2981 surface->output->y + y,
2982 surface->buffer->width,
2983 surface->buffer->height);
2984}
2985
2986static void
Philipp Brüschweiler88013572012-08-06 13:44:42 +02002987destroy_input_panel_surface(struct wl_listener *listener,
2988 void *data)
2989{
2990 struct input_panel_surface *input_panel_surface =
2991 container_of(listener, struct input_panel_surface, listener);
2992
2993 wl_list_remove(&listener->link);
2994 wl_list_remove(&input_panel_surface->link);
2995
2996 free(input_panel_surface);
2997}
2998
2999static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003000input_panel_set_surface(struct wl_client *client,
3001 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003002 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003003 struct wl_resource *output_resource)
3004{
3005 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003006 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003007 struct weston_output *output = output_resource->data;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003008 struct input_panel_surface *input_panel_surface;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003009
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003010 surface->configure = input_panel_configure;
3011 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003012 surface->output = output;
Philipp Brüschweiler88013572012-08-06 13:44:42 +02003013
3014 input_panel_surface = malloc(sizeof *input_panel_surface);
3015 if (!input_panel_surface) {
3016 wl_resource_post_no_memory(resource);
3017 return;
3018 }
3019
3020 input_panel_surface->surface = surface;
3021 input_panel_surface->listener.notify = destroy_input_panel_surface;
3022
3023 wl_signal_add(&surface_resource->destroy_signal,
3024 &input_panel_surface->listener);
3025
3026 wl_list_insert(&shell->input_panel.surfaces,
3027 &input_panel_surface->link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003028}
3029
3030static const struct input_panel_interface input_panel_implementation = {
3031 input_panel_set_surface
3032};
3033
3034static void
3035unbind_input_panel(struct wl_resource *resource)
3036{
3037 struct desktop_shell *shell = resource->data;
3038
3039 shell->input_panel.binding = NULL;
3040 free(resource);
3041}
3042
3043static void
3044bind_input_panel(struct wl_client *client,
3045 void *data, uint32_t version, uint32_t id)
3046{
3047 struct desktop_shell *shell = data;
3048 struct wl_resource *resource;
3049
3050 resource = wl_client_add_object(client, &input_panel_interface,
3051 &input_panel_implementation,
3052 id, shell);
3053
3054 if (shell->input_panel.binding == NULL) {
3055 resource->destroy = unbind_input_panel;
3056 shell->input_panel.binding = resource;
3057 return;
3058 }
3059
3060 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3061 "interface object already bound");
3062 wl_resource_destroy(resource);
3063}
3064
Kristian Høgsberg07045392012-02-19 18:52:44 -05003065struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003066 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003067 struct weston_surface *current;
3068 struct wl_listener listener;
3069 struct wl_keyboard_grab grab;
3070};
3071
3072static void
3073switcher_next(struct switcher *switcher)
3074{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003075 struct weston_surface *surface;
3076 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003077 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003078 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003079
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003080 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003081 switch (get_shell_surface_type(surface)) {
3082 case SHELL_SURFACE_TOPLEVEL:
3083 case SHELL_SURFACE_FULLSCREEN:
3084 case SHELL_SURFACE_MAXIMIZED:
3085 if (first == NULL)
3086 first = surface;
3087 if (prev == switcher->current)
3088 next = surface;
3089 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003090 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003091 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003092 weston_surface_damage(surface);
3093 break;
3094 default:
3095 break;
3096 }
Alex Wu1659daa2012-04-01 20:13:09 +08003097
3098 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003099 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003100 surface->geometry.dirty = 1;
3101 weston_surface_damage(surface);
3102 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003103 }
3104
3105 if (next == NULL)
3106 next = first;
3107
Alex Wu07b26062012-03-12 16:06:01 +08003108 if (next == NULL)
3109 return;
3110
Kristian Høgsberg07045392012-02-19 18:52:44 -05003111 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003112 wl_signal_add(&next->surface.resource.destroy_signal,
3113 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003114
3115 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003116 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003117
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003118 shsurf = get_shell_surface(switcher->current);
3119 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003120 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003121}
3122
3123static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003124switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003125{
3126 struct switcher *switcher =
3127 container_of(listener, struct switcher, listener);
3128
3129 switcher_next(switcher);
3130}
3131
3132static void
Daniel Stone351eb612012-05-31 15:27:47 -04003133switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003134{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003135 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003136 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003137 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003138
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003139 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003140 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003141 weston_surface_damage(surface);
3142 }
3143
Alex Wu07b26062012-03-12 16:06:01 +08003144 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003145 activate(switcher->shell, switcher->current,
3146 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003147 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003148 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003149 free(switcher);
3150}
3151
3152static void
3153switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003154 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003155{
3156 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003157 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003158
Daniel Stonec9785ea2012-05-30 16:31:52 +01003159 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003160 switcher_next(switcher);
3161}
3162
3163static void
3164switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3165 uint32_t mods_depressed, uint32_t mods_latched,
3166 uint32_t mods_locked, uint32_t group)
3167{
3168 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003169 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003170
Daniel Stone351eb612012-05-31 15:27:47 -04003171 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3172 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003173}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003174
3175static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003176 switcher_key,
3177 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003178};
3179
3180static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003181switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3182 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003183{
Tiago Vignattibe143262012-04-16 17:31:41 +03003184 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003185 struct switcher *switcher;
3186
3187 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003188 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003189 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003190 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003191 wl_list_init(&switcher->listener.link);
3192
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003193 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003194 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003195 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3196 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003197 switcher_next(switcher);
3198}
3199
Pekka Paalanen3c647232011-12-22 13:43:43 +02003200static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003201backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3202 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003203{
3204 struct weston_compositor *compositor = data;
3205 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003206 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003207
3208 /* TODO: we're limiting to simple use cases, where we assume just
3209 * control on the primary display. We'd have to extend later if we
3210 * ever get support for setting backlights on random desktop LCD
3211 * panels though */
3212 output = get_default_output(compositor);
3213 if (!output)
3214 return;
3215
3216 if (!output->set_backlight)
3217 return;
3218
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003219 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3220 backlight_new = output->backlight_current - 25;
3221 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3222 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003223
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003224 if (backlight_new < 5)
3225 backlight_new = 5;
3226 if (backlight_new > 255)
3227 backlight_new = 255;
3228
3229 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003230 output->set_backlight(output, output->backlight_current);
3231}
3232
3233static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003234debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3235 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003236{
Tiago Vignattibe143262012-04-16 17:31:41 +03003237 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003238 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003239 struct weston_surface *surface;
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003240 struct weston_plane plane;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003241
3242 if (shell->debug_repaint_surface) {
3243 weston_surface_destroy(shell->debug_repaint_surface);
3244 shell->debug_repaint_surface = NULL;
3245 } else {
3246 surface = weston_surface_create(compositor);
3247 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3248 weston_surface_configure(surface, 0, 0, 8192, 8192);
3249 wl_list_insert(&compositor->fade_layer.surface_list,
3250 &surface->layer_link);
3251 weston_surface_assign_output(surface);
3252 pixman_region32_init(&surface->input);
3253
3254 /* Here's the dirty little trick that makes the
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003255 * repaint debugging work: we move the surface to a
3256 * different plane and force an update_transform to
3257 * update dependent state and clear the
3258 * geometry.dirty bit. This way the call to
3259 * damage_below() in update_transform() does not
3260 * add damage to the primary plane. */
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003261
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003262 weston_plane_init(&plane, 0, 0);
3263 surface->plane = &plane;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003264 weston_surface_update_transform(surface);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003265 shell->debug_repaint_surface = surface;
Ander Conselvan de Oliveira383b6712012-08-09 16:44:59 +03003266 surface->plane = &compositor->primary_plane;
3267 weston_plane_release(&plane);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003268 }
3269}
3270
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003271
3272static void
3273fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3274 void *data)
3275{
3276 struct desktop_shell *shell = data;
3277 struct weston_compositor *compositor = shell->compositor;
3278 compositor->fan_debug = !compositor->fan_debug;
3279 weston_compositor_damage_all(compositor);
3280}
3281
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003282static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003283force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3284 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003285{
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003286 struct wl_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003287 struct wl_client *client;
3288 pid_t pid;
3289 uid_t uid;
3290 gid_t gid;
3291
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02003292 focus_surface = seat->keyboard->focus;
3293 if (!focus_surface)
3294 return;
3295
3296 client = focus_surface->resource.client;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003297 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003298
Daniel Stone325fc2d2012-05-30 16:31:58 +01003299 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003300}
3301
3302static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003303workspace_up_binding(struct wl_seat *seat, uint32_t time,
3304 uint32_t key, void *data)
3305{
3306 struct desktop_shell *shell = data;
3307 unsigned int new_index = shell->workspaces.current;
3308
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003309 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003310 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003311 if (new_index != 0)
3312 new_index--;
3313
3314 change_workspace(shell, new_index);
3315}
3316
3317static void
3318workspace_down_binding(struct wl_seat *seat, uint32_t time,
3319 uint32_t key, void *data)
3320{
3321 struct desktop_shell *shell = data;
3322 unsigned int new_index = shell->workspaces.current;
3323
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003324 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003325 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003326 if (new_index < shell->workspaces.num - 1)
3327 new_index++;
3328
3329 change_workspace(shell, new_index);
3330}
3331
3332static void
3333workspace_f_binding(struct wl_seat *seat, uint32_t time,
3334 uint32_t key, void *data)
3335{
3336 struct desktop_shell *shell = data;
3337 unsigned int new_index;
3338
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003339 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003340 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003341 new_index = key - KEY_F1;
3342 if (new_index >= shell->workspaces.num)
3343 new_index = shell->workspaces.num - 1;
3344
3345 change_workspace(shell, new_index);
3346}
3347
3348
3349static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003350shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003351{
Tiago Vignattibe143262012-04-16 17:31:41 +03003352 struct desktop_shell *shell =
3353 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003354 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003355
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003356 if (shell->child.client)
3357 wl_client_destroy(shell->child.client);
3358
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003359 wl_list_remove(&shell->lock_listener.link);
3360 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003361 wl_list_remove(&shell->show_input_panel_listener.link);
3362 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003363
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003364 wl_array_for_each(ws, &shell->workspaces.array)
3365 workspace_destroy(*ws);
3366 wl_array_release(&shell->workspaces.array);
3367
Pekka Paalanen3c647232011-12-22 13:43:43 +02003368 free(shell->screensaver.path);
3369 free(shell);
3370}
3371
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003372static void
3373shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3374{
3375 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003376 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003377
3378 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003379 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3380 MODIFIER_CTRL | MODIFIER_ALT,
3381 terminate_binding, ec);
3382 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3383 click_to_activate_binding,
3384 shell);
3385 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3386 MODIFIER_SUPER | MODIFIER_ALT,
3387 surface_opacity_binding, NULL);
3388 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3389 MODIFIER_SUPER, zoom_axis_binding,
3390 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003391
3392 /* configurable bindings */
3393 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003394 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3395 zoom_key_binding, NULL);
3396 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3397 zoom_key_binding, NULL);
3398 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3399 shell);
3400 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3401 resize_binding, shell);
3402 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3403 rotate_binding, NULL);
3404 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3405 shell);
3406 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3407 ec);
3408 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3409 backlight_binding, ec);
3410 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3411 ec);
3412 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3413 backlight_binding, ec);
Kristian Høgsberg73694c82012-06-28 14:13:10 -04003414 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003415 debug_repaint_binding, shell);
Pekka Paalanen07c91f82012-08-30 16:47:21 -05003416 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_ALT,
3417 fan_debug_repaint_binding, shell);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003418 weston_compositor_add_key_binding(ec, KEY_K, mod,
3419 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003420 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3421 workspace_up_binding, shell);
3422 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3423 workspace_down_binding, shell);
3424
3425 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3426 if (shell->workspaces.num > 1) {
3427 num_workspace_bindings = shell->workspaces.num;
3428 if (num_workspace_bindings > 6)
3429 num_workspace_bindings = 6;
3430 for (i = 0; i < num_workspace_bindings; i++)
3431 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3432 workspace_f_binding,
3433 shell);
3434 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003435}
3436
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003437int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003438shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003439
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003440WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003441shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003442{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003443 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03003444 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003445 struct workspace **pws;
3446 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003447
3448 shell = malloc(sizeof *shell);
3449 if (shell == NULL)
3450 return -1;
3451
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003452 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003453 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003454
3455 shell->destroy_listener.notify = shell_destroy;
3456 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3457 shell->lock_listener.notify = lock;
3458 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3459 shell->unlock_listener.notify = unlock;
3460 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003461 shell->show_input_panel_listener.notify = show_input_panels;
3462 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3463 shell->hide_input_panel_listener.notify = hide_input_panels;
3464 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003465 ec->ping_handler = ping_handler;
Kristian Høgsberg82a1d112012-07-19 14:02:00 -04003466 ec->shell_interface.shell = shell;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003467 ec->shell_interface.create_shell_surface = create_shell_surface;
3468 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003469 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003470 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003471 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003472
Pekka Paalanen77346a62011-11-30 16:26:35 +02003473 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003474 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003475
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003476 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3477 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003478 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3479 weston_layer_init(&shell->lock_layer, NULL);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003480 weston_layer_init(&shell->input_panel_layer, NULL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003481
3482 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003483
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003484 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003485
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003486 for (i = 0; i < shell->workspaces.num; i++) {
3487 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3488 if (pws == NULL)
3489 return -1;
3490
3491 *pws = workspace_create();
3492 if (*pws == NULL)
3493 return -1;
3494 }
3495 activate_workspace(shell, 0);
3496
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003497 wl_list_init(&shell->workspaces.animation.link);
3498 shell->workspaces.animation.frame = animate_workspace_change_frame;
3499
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003500 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3501 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003502 return -1;
3503
Kristian Høgsberg75840622011-09-06 13:48:16 -04003504 if (wl_display_add_global(ec->wl_display,
3505 &desktop_shell_interface,
3506 shell, bind_desktop_shell) == NULL)
3507 return -1;
3508
Pekka Paalanen6e168112011-11-24 11:34:05 +02003509 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3510 shell, bind_screensaver) == NULL)
3511 return -1;
3512
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003513 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3514 shell, bind_input_panel) == NULL)
3515 return -1;
3516
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003517 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003518 if (launch_desktop_shell_process(shell) != 0)
3519 return -1;
3520
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04003521 wl_list_for_each(seat, &ec->seat_list, link)
3522 create_pointer_focus_listener(seat);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003523
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003524 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003525
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003526 return 0;
3527}