blob: f7369f32f68471c39061d93fc1a56373652cc7b6 [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"
Martin Minarik6d118362012-06-07 18:01:59 +020039#include "log.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050040
Jonas Ådahle3cddce2012-06-13 00:01:22 +020041#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020042#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043
Juan Zhaoe10d2792012-04-25 19:09:52 +080044enum animation_type {
45 ANIMATION_NONE,
46
47 ANIMATION_ZOOM,
48 ANIMATION_FADE
49};
50
Jonas Ådahl04769742012-06-13 00:01:24 +020051struct focus_state {
52 struct weston_seat *seat;
53 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
Tiago Vignattibe143262012-04-16 17:31:41 +030066struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040068
69 struct wl_listener lock_listener;
70 struct wl_listener unlock_listener;
71 struct wl_listener destroy_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020072
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050073 struct weston_layer fullscreen_layer;
74 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050075 struct weston_layer background_layer;
76 struct weston_layer lock_layer;
77
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040078 struct wl_listener pointer_focus_listener;
79 struct weston_surface *busy_surface;
80
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020081 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050082 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020083 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020084 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020085
86 unsigned deathcount;
87 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020088 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020089
90 bool locked;
91 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020092
Pekka Paalanen068ae942011-11-28 14:11:15 +020093 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050094 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010095
96 struct wl_list backgrounds;
97 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020098
Pekka Paalanen77346a62011-11-30 16:26:35 +020099 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200100 struct wl_array array;
101 unsigned int current;
102 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200103
104 struct weston_animation animation;
105 int anim_dir;
106 uint32_t anim_timestamp;
107 double anim_current;
108 struct workspace *anim_from;
109 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200110 } workspaces;
111
112 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200113 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200114 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200115 struct wl_resource *binding;
116 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500117 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200118 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500119
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300120 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800121 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500122 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400123};
124
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500125enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200126 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500127
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200128 SHELL_SURFACE_PANEL,
129 SHELL_SURFACE_BACKGROUND,
130 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200131 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500132
133 SHELL_SURFACE_TOPLEVEL,
134 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500135 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800136 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500137 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200138};
139
Scott Moreauff1db4a2012-04-17 19:06:18 -0600140struct ping_timer {
141 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600142 uint32_t serial;
143};
144
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200145struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200146 struct wl_resource resource;
147
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500148 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200149 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500150 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300151 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200152
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400153 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400154 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500155 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800156 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800157 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600158 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100159
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500160 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200161 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500162 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200163 } rotation;
164
165 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700166 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500167 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200168 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500169 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100170 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400171 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500172 } popup;
173
Alex Wu4539b082012-03-01 12:57:46 +0800174 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300175 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400176 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300177 } transient;
178
179 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800180 enum wl_shell_surface_fullscreen_method type;
181 struct weston_transform transform; /* matrix from x, y */
182 uint32_t framerate;
183 struct weston_surface *black_surface;
184 } fullscreen;
185
Scott Moreauff1db4a2012-04-17 19:06:18 -0600186 struct ping_timer *ping_timer;
187
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200188 struct weston_transform workspace_transform;
189
Jonas Ådahl04769742012-06-13 00:01:24 +0200190 struct focus_state *focus_state;
191
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500192 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500193 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100194 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400195
196 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200197};
198
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300199struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700200 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300201 struct shell_surface *shsurf;
202 struct wl_listener shsurf_destroy_listener;
203};
204
205struct weston_move_grab {
206 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100207 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500208};
209
Pekka Paalanen460099f2012-01-20 16:48:25 +0200210struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300211 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500212 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200213 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400214 GLfloat x;
215 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200216 } center;
217};
218
Alex Wubd3354b2012-04-17 17:20:49 +0800219static struct shell_surface *
220get_shell_surface(struct weston_surface *surface);
221
222static struct desktop_shell *
223shell_surface_get_shell(struct shell_surface *shsurf);
224
225static bool
226shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
227{
228 struct desktop_shell *shell;
229 struct weston_surface *top_fs_es;
230
231 shell = shell_surface_get_shell(shsurf);
232
233 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
234 return false;
235
236 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
237 struct weston_surface,
238 layer_link);
239 return (shsurf == get_shell_surface(top_fs_es));
240}
241
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500242static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400243destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300244{
245 struct shell_grab *grab;
246
247 grab = container_of(listener, struct shell_grab,
248 shsurf_destroy_listener);
249
250 grab->shsurf = NULL;
251}
252
253static void
254shell_grab_init(struct shell_grab *grab,
255 const struct wl_pointer_grab_interface *interface,
256 struct shell_surface *shsurf)
257{
258 grab->grab.interface = interface;
259 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400260 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
261 wl_signal_add(&shsurf->resource.destroy_signal,
262 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300263
264}
265
266static void
267shell_grab_finish(struct shell_grab *grab)
268{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400269 if (grab->shsurf)
270 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300271}
272
273static void
Alex Wu4539b082012-03-01 12:57:46 +0800274center_on_output(struct weston_surface *surface,
275 struct weston_output *output);
276
Daniel Stone496ca172012-05-30 16:31:42 +0100277static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300278get_modifier(char *modifier)
279{
280 if (!modifier)
281 return MODIFIER_SUPER;
282
283 if (!strcmp("ctrl", modifier))
284 return MODIFIER_CTRL;
285 else if (!strcmp("alt", modifier))
286 return MODIFIER_ALT;
287 else if (!strcmp("super", modifier))
288 return MODIFIER_SUPER;
289 else
290 return MODIFIER_SUPER;
291}
292
Juan Zhaoe10d2792012-04-25 19:09:52 +0800293static enum animation_type
294get_animation_type(char *animation)
295{
296 if (!animation)
297 return ANIMATION_NONE;
298
299 if (!strcmp("zoom", animation))
300 return ANIMATION_ZOOM;
301 else if (!strcmp("fade", animation))
302 return ANIMATION_FADE;
303 else
304 return ANIMATION_NONE;
305}
306
Alex Wu4539b082012-03-01 12:57:46 +0800307static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300308shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200309{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200310 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200311 char *path = NULL;
312 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200313 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300314 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800315 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200316
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400317 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300318 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800319 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200320 { "num-workspaces",
321 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200322 };
323
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400324 struct config_key saver_keys[] = {
325 { "path", CONFIG_KEY_STRING, &path },
326 { "duration", CONFIG_KEY_INTEGER, &duration },
327 };
328
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200329 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400330 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200331 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
332 };
333
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200334 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500335 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200336 free(config_file);
337
Pekka Paalanen7296e792011-12-07 16:22:00 +0200338 shell->screensaver.path = path;
339 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300340 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800341 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200342 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
343}
344
345static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200346focus_state_destroy(struct focus_state *state)
347{
348 wl_list_remove(&state->seat_destroy_listener.link);
349 wl_list_remove(&state->surface_destroy_listener.link);
350 free(state);
351}
352
353static void
354focus_state_seat_destroy(struct wl_listener *listener, void *data)
355{
356 struct focus_state *state = container_of(listener,
357 struct focus_state,
358 seat_destroy_listener);
359
360 wl_list_remove(&state->link);
361 focus_state_destroy(state);
362}
363
364static void
365focus_state_surface_destroy(struct wl_listener *listener, void *data)
366{
367 struct focus_state *state = container_of(listener,
368 struct focus_state,
369 seat_destroy_listener);
370
371 wl_list_remove(&state->link);
372 focus_state_destroy(state);
373}
374
375static struct focus_state *
376focus_state_create(struct weston_seat *seat)
377{
378 struct wl_keyboard *keyboard = seat->seat.keyboard;
379 struct focus_state *state;
380 struct wl_surface *surface;
381 struct shell_surface *shsurf;
382
383 state = malloc(sizeof *state);
384 if (state == NULL)
385 return NULL;
386
387 surface = keyboard->focus;
388 shsurf = get_shell_surface((struct weston_surface *)keyboard->focus);
389 shsurf->focus_state = state;
390
391 state->seat = seat;
392 state->keyboard_focus = shsurf->surface;
393 wl_list_init(&state->link);
394
395 state->seat_destroy_listener.notify = focus_state_seat_destroy;
396 state->surface_destroy_listener.notify = focus_state_surface_destroy;
397 wl_signal_add(&seat->seat.destroy_signal,
398 &state->seat_destroy_listener);
399 wl_signal_add(&surface->resource.destroy_signal,
400 &state->surface_destroy_listener);
401
402 return state;
403}
404
405static void
406pop_focus_state(struct desktop_shell *shell, struct workspace *ws)
407{
408 struct focus_state *state, *next;
409
410 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
411 if (state->keyboard_focus)
412 wl_keyboard_set_focus(state->seat->seat.keyboard,
413 &state->keyboard_focus->surface);
414
415 focus_state_destroy(state);
416 }
417 wl_list_init(&ws->focus_list);
418}
419
420static void
421push_focus_state(struct desktop_shell *shell, struct workspace *ws)
422{
423 struct weston_seat *seat;
424 struct focus_state *state;
425 struct wl_keyboard *keyboard;
426
427 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
428 keyboard = seat->seat.keyboard;
429 if (keyboard && keyboard->focus) {
430 state = focus_state_create(seat);
431 if (state == NULL)
432 return;
433
434 wl_list_insert(&ws->focus_list, &state->link);
435
436 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
437 }
438 }
439}
440
441static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200442workspace_destroy(struct workspace *ws)
443{
Jonas Ådahl04769742012-06-13 00:01:24 +0200444 struct focus_state *state, *next;
445
446 wl_list_for_each_safe(state, next, &ws->focus_list, link)
447 focus_state_destroy(state);
448
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200449 free(ws);
450}
451
Jonas Ådahl04769742012-06-13 00:01:24 +0200452static void
453seat_destroyed(struct wl_listener *listener, void *data)
454{
455 struct weston_seat *seat = data;
456 struct focus_state *state, *next;
457 struct workspace *ws = container_of(listener,
458 struct workspace,
459 seat_destroyed_listener);
460
461 wl_list_for_each_safe(state, next, &ws->focus_list, link)
462 if (state->seat == seat)
463 wl_list_remove(&state->link);
464}
465
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200466static struct workspace *
467workspace_create(void)
468{
469 struct workspace *ws = malloc(sizeof *ws);
470 if (ws == NULL)
471 return NULL;
472
473 weston_layer_init(&ws->layer, NULL);
474
Jonas Ådahl04769742012-06-13 00:01:24 +0200475 wl_list_init(&ws->focus_list);
476 wl_list_init(&ws->seat_destroyed_listener.link);
477 ws->seat_destroyed_listener.notify = seat_destroyed;
478
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200479 return ws;
480}
481
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200482static int
483workspace_is_empty(struct workspace *ws)
484{
485 return wl_list_empty(&ws->layer.surface_list);
486}
487
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200488static struct workspace *
489get_workspace(struct desktop_shell *shell, unsigned int index)
490{
491 struct workspace **pws = shell->workspaces.array.data;
492 pws += index;
493 return *pws;
494}
495
496static struct workspace *
497get_current_workspace(struct desktop_shell *shell)
498{
499 return get_workspace(shell, shell->workspaces.current);
500}
501
502static void
503activate_workspace(struct desktop_shell *shell, unsigned int index)
504{
505 struct workspace *ws;
506
507 ws = get_workspace(shell, index);
508 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
509
510 shell->workspaces.current = index;
511}
512
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200513static unsigned int
514get_output_height(struct weston_output *output)
515{
516 return abs(output->region.extents.y1 - output->region.extents.y2);
517}
518
519static void
520surface_translate(struct weston_surface *surface, double d)
521{
522 struct shell_surface *shsurf = get_shell_surface(surface);
523 struct weston_transform *transform;
524
525 transform = &shsurf->workspace_transform;
526 if (wl_list_empty(&transform->link))
527 wl_list_insert(surface->geometry.transformation_list.prev,
528 &shsurf->workspace_transform.link);
529
530 weston_matrix_init(&shsurf->workspace_transform.matrix);
531 weston_matrix_translate(&shsurf->workspace_transform.matrix,
532 0.0, d, 0.0);
533 surface->geometry.dirty = 1;
534}
535
536static void
537workspace_translate_out(struct workspace *ws, double fraction)
538{
539 struct weston_surface *surface;
540 unsigned int height;
541 double d;
542
543 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
544 height = get_output_height(surface->output);
545 d = height * fraction;
546
547 surface_translate(surface, d);
548 }
549}
550
551static void
552workspace_translate_in(struct workspace *ws, double fraction)
553{
554 struct weston_surface *surface;
555 unsigned int height;
556 double d;
557
558 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
559 height = get_output_height(surface->output);
560
561 if (fraction > 0)
562 d = -(height - height * fraction);
563 else
564 d = height + height * fraction;
565
566 surface_translate(surface, d);
567 }
568}
569
570static void
571workspace_damage_all_surfaces(struct workspace *ws)
572{
573 struct weston_surface *surface;
574
575 wl_list_for_each(surface, &ws->layer.surface_list, layer_link)
576 weston_surface_damage(surface);
577}
578
579static void
580reverse_workspace_change_animation(struct desktop_shell *shell,
581 unsigned int index,
582 struct workspace *from,
583 struct workspace *to)
584{
585 shell->workspaces.current = index;
586
587 shell->workspaces.anim_to = to;
588 shell->workspaces.anim_from = from;
589 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
590 shell->workspaces.anim_timestamp = 0;
591
Jonas Ådahl04769742012-06-13 00:01:24 +0200592 push_focus_state(shell, from);
593 pop_focus_state(shell, to);
594
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200595 workspace_damage_all_surfaces(from);
596 workspace_damage_all_surfaces(to);
597}
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{
618 workspace_damage_all_surfaces(from);
619 workspace_damage_all_surfaces(to);
620
621 wl_list_remove(&shell->workspaces.animation.link);
622 workspace_deactivate_transforms(from);
623 workspace_deactivate_transforms(to);
624 shell->workspaces.anim_to = NULL;
625
626 wl_list_remove(&shell->workspaces.anim_from->layer.link);
627}
628
629static void
630animate_workspace_change_frame(struct weston_animation *animation,
631 struct weston_output *output, uint32_t msecs)
632{
633 struct desktop_shell *shell =
634 container_of(animation, struct desktop_shell,
635 workspaces.animation);
636 struct workspace *from = shell->workspaces.anim_from;
637 struct workspace *to = shell->workspaces.anim_to;
638 uint32_t t;
639 double x, y;
640
641 if (workspace_is_empty(from) && workspace_is_empty(to)) {
642 finish_workspace_change_animation(shell, from, to);
643 return;
644 }
645
646 if (shell->workspaces.anim_timestamp == 0) {
647 if (shell->workspaces.anim_current == 0.0)
648 shell->workspaces.anim_timestamp = msecs;
649 else
650 shell->workspaces.anim_timestamp =
651 msecs -
652 /* Invers of movement function 'y' below. */
653 (asin(1.0 - shell->workspaces.anim_current) *
654 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
655 M_2_PI);
656 }
657
658 t = msecs - shell->workspaces.anim_timestamp;
659
660 /*
661 * x = [0, π/2]
662 * y(x) = sin(x)
663 */
664 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
665 y = sin(x);
666
667 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
668 workspace_damage_all_surfaces(from);
669 workspace_damage_all_surfaces(to);
670
671 workspace_translate_out(from, shell->workspaces.anim_dir * y);
672 workspace_translate_in(to, shell->workspaces.anim_dir * y);
673 shell->workspaces.anim_current = y;
674
675 workspace_damage_all_surfaces(from);
676 workspace_damage_all_surfaces(to);
677 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200678 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200679 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200680}
681
682static void
683animate_workspace_change(struct desktop_shell *shell,
684 unsigned int index,
685 struct workspace *from,
686 struct workspace *to)
687{
688 struct weston_output *output;
689
690 int dir;
691
692 if (index > shell->workspaces.current)
693 dir = -1;
694 else
695 dir = 1;
696
697 shell->workspaces.current = index;
698
699 shell->workspaces.anim_dir = dir;
700 shell->workspaces.anim_from = from;
701 shell->workspaces.anim_to = to;
702 shell->workspaces.anim_current = 0.0;
703 shell->workspaces.anim_timestamp = 0;
704
705 output = container_of(shell->compositor->output_list.next,
706 struct weston_output, link);
707 wl_list_insert(&output->animation_list,
708 &shell->workspaces.animation.link);
709
710 wl_list_insert(&from->layer.link, &to->layer.link);
711
712 workspace_translate_in(to, 0);
713
Jonas Ådahl04769742012-06-13 00:01:24 +0200714 push_focus_state(shell, from);
715 pop_focus_state(shell, to);
716
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200717 workspace_damage_all_surfaces(from);
718 workspace_damage_all_surfaces(to);
719}
720
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200721static void
722change_workspace(struct desktop_shell *shell, unsigned int index)
723{
724 struct workspace *from;
725 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200726
727 if (index == shell->workspaces.current)
728 return;
729
730 /* Don't change workspace when there is any fullscreen surfaces. */
731 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
732 return;
733
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200734 from = get_current_workspace(shell);
735 to = get_workspace(shell, index);
736
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200737 if (shell->workspaces.anim_from == to &&
738 shell->workspaces.anim_to == from) {
739 reverse_workspace_change_animation(shell, index, from, to);
740 return;
741 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200742
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200743 if (shell->workspaces.anim_to != NULL)
744 finish_workspace_change_animation(shell,
745 shell->workspaces.anim_from,
746 shell->workspaces.anim_to);
747
748 if (workspace_is_empty(to) && workspace_is_empty(from)) {
749 shell->workspaces.current = index;
750 wl_list_insert(&from->layer.link, &to->layer.link);
751 wl_list_remove(&from->layer.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200752
753 push_focus_state(shell, from);
754 pop_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200755 }
756 else
757 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200758}
759
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200760static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400761noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100762 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500763{
764 grab->focus = NULL;
765}
766
767static void
Scott Moreau447013d2012-02-18 05:05:29 -0700768move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100769 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500770{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500771 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100772 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300773 struct shell_surface *shsurf = move->base.shsurf;
774 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100775 int dx = wl_fixed_to_int(pointer->x + move->dx);
776 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300777
778 if (!shsurf)
779 return;
780
781 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500782
Daniel Stone103db7f2012-05-08 17:17:55 +0100783 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200784 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500785}
786
787static void
Scott Moreau447013d2012-02-18 05:05:29 -0700788move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100789 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500790{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300791 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
792 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100793 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100794 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500795
Daniel Stone4dbadb12012-05-30 16:31:51 +0100796 if (pointer->button_count == 0 &&
797 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300798 shell_grab_finish(shell_grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100799 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500800 free(grab);
801 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500802}
803
Scott Moreau447013d2012-02-18 05:05:29 -0700804static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500805 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500806 move_grab_motion,
807 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500808};
809
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600810static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400811busy_cursor_grab_focus(struct wl_pointer_grab *base,
812 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600813{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400814 struct shell_grab *grab = (struct shell_grab *) base;
815 struct wl_pointer *pointer = base->pointer;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600816
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400817 if (grab->grab.focus != surface) {
818 shell_grab_finish(grab);
819 wl_pointer_end_grab(pointer);
820 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600821 }
822}
823
824static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400825busy_cursor_grab_motion(struct wl_pointer_grab *grab,
826 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600827{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400828}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600829
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400830static void
831busy_cursor_grab_button(struct wl_pointer_grab *grab,
832 uint32_t time, uint32_t button, uint32_t state)
833{
834}
835
836static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
837 busy_cursor_grab_focus,
838 busy_cursor_grab_motion,
839 busy_cursor_grab_button,
840};
841
842static void
843set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
844{
845 struct shell_grab *grab;
846 struct desktop_shell *shell = shsurf->shell;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400847
848 grab = malloc(sizeof *grab);
849 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600850 return;
851
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400852 shell_grab_init(grab, &busy_cursor_grab_interface, shsurf);
853 grab->grab.focus = &shsurf->surface->surface;
854 wl_pointer_start_grab(pointer, &grab->grab);
855 wl_pointer_set_focus(pointer, &shell->busy_surface->surface, 0, 0);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400856}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600857
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400858static void
859end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
860{
861 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
862
863 if (grab->grab.interface == &busy_cursor_grab_interface) {
864 shell_grab_finish(grab);
865 wl_pointer_end_grab(pointer);
866 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600867 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600868}
869
Scott Moreau9521d5e2012-04-19 13:06:17 -0600870static void
871ping_timer_destroy(struct shell_surface *shsurf)
872{
873 if (!shsurf || !shsurf->ping_timer)
874 return;
875
876 if (shsurf->ping_timer->source)
877 wl_event_source_remove(shsurf->ping_timer->source);
878
879 free(shsurf->ping_timer);
880 shsurf->ping_timer = NULL;
881}
882
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400883static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600884ping_timeout_handler(void *data)
885{
886 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400887 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600888
Scott Moreau9521d5e2012-04-19 13:06:17 -0600889 /* Client is not responding */
890 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400891
892 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
893 if (seat->seat.pointer->focus == &shsurf->surface->surface)
894 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600895
896 return 1;
897}
898
899static void
900ping_handler(struct weston_surface *surface, uint32_t serial)
901{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400902 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600903 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400904 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600905
906 if (!shsurf)
907 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400908 if (!shsurf->resource.client)
909 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600910
911 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300912 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600913 if (!shsurf->ping_timer)
914 return;
915
916 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600917 loop = wl_display_get_event_loop(surface->compositor->wl_display);
918 shsurf->ping_timer->source =
919 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
920 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
921
922 wl_shell_surface_send_ping(&shsurf->resource, serial);
923 }
924}
925
926static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400927handle_pointer_focus(struct wl_listener *listener, void *data)
928{
929 struct wl_pointer *pointer = data;
930 struct weston_surface *surface =
931 (struct weston_surface *) pointer->focus;
932 struct weston_compositor *compositor;
933 struct shell_surface *shsurf;
934 uint32_t serial;
935
936 if (!surface)
937 return;
938
939 compositor = surface->compositor;
940 shsurf = get_shell_surface(surface);
941
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +0300942 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400943 set_busy_cursor(shsurf, pointer);
944 } else {
945 serial = wl_display_next_serial(compositor->wl_display);
946 ping_handler(surface, serial);
947 }
948}
949
950static void
Scott Moreauff1db4a2012-04-17 19:06:18 -0600951shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
952 uint32_t serial)
953{
954 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400955 struct desktop_shell *shell = shsurf->shell;
956 struct weston_seat *seat;
957 struct weston_compositor *ec = shsurf->surface->compositor;
958 struct wl_pointer *pointer;
959 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600960
Scott Moreauff1db4a2012-04-17 19:06:18 -0600961 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400962 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600963 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400964 if (was_unresponsive) {
965 /* Received pong from previously unresponsive client */
966 wl_list_for_each(seat, &ec->seat_list, link) {
967 pointer = seat->seat.pointer;
968 if (pointer->focus ==
969 &shell->busy_surface->surface &&
970 pointer->current ==
971 &shsurf->surface->surface)
972 end_busy_cursor(shsurf, pointer);
973 }
974 }
Scott Moreau9521d5e2012-04-19 13:06:17 -0600975 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600976 }
977}
978
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400979static void
980shell_surface_set_title(struct wl_client *client,
981 struct wl_resource *resource, const char *title)
982{
983 struct shell_surface *shsurf = resource->data;
984
985 free(shsurf->title);
986 shsurf->title = strdup(title);
987}
988
989static void
990shell_surface_set_class(struct wl_client *client,
991 struct wl_resource *resource, const char *class)
992{
993 struct shell_surface *shsurf = resource->data;
994
995 free(shsurf->class);
996 shsurf->class = strdup(class);
997}
998
Scott Moreauff1db4a2012-04-17 19:06:18 -0600999static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001000surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001001{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001002 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001003
1004 if (!shsurf)
1005 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001006
1007 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001008 if (!move)
1009 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001010
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001011 shell_grab_init(&move->base, &move_grab_interface, shsurf);
1012
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001013 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +01001014 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001015 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +01001016 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001017
Daniel Stone37816df2012-05-16 18:45:18 +01001018 wl_pointer_start_grab(ws->seat.pointer, &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001019
Daniel Stone37816df2012-05-16 18:45:18 +01001020 wl_pointer_set_focus(ws->seat.pointer, NULL,
1021 wl_fixed_from_int(0),
1022 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001023
1024 return 0;
1025}
1026
1027static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001028shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001029 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001030{
Daniel Stone37816df2012-05-16 18:45:18 +01001031 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001032 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001033
Daniel Stone37816df2012-05-16 18:45:18 +01001034 if (ws->seat.pointer->button_count == 0 ||
1035 ws->seat.pointer->grab_serial != serial ||
1036 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001037 return;
1038
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001039 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001040 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001041}
1042
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001043struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001044 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001045 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001046 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001047};
1048
1049static void
Scott Moreau447013d2012-02-18 05:05:29 -07001050resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001051 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001052{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001053 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001054 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001055 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001056 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +01001057 wl_fixed_t from_x, from_y;
1058 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001059
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001060 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001061 return;
1062
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001063 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001064 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +01001065 &from_x, &from_y);
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001066 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001067 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001068
Daniel Stone103db7f2012-05-08 17:17:55 +01001069 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001070 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001071 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001072 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001073 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001074 }
1075
Daniel Stone103db7f2012-05-08 17:17:55 +01001076 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001077 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001078 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001079 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001080 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001081 }
1082
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001083 shsurf->client->send_configure(shsurf->surface,
1084 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001085}
1086
1087static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001088send_configure(struct weston_surface *surface,
1089 uint32_t edges, int32_t width, int32_t height)
1090{
1091 struct shell_surface *shsurf = get_shell_surface(surface);
1092
1093 wl_shell_surface_send_configure(&shsurf->resource,
1094 edges, width, height);
1095}
1096
1097static const struct weston_shell_client shell_client = {
1098 send_configure
1099};
1100
1101static void
Scott Moreau447013d2012-02-18 05:05:29 -07001102resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001103 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001104{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001105 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001106 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001107 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001108
Daniel Stone4dbadb12012-05-30 16:31:51 +01001109 if (pointer->button_count == 0 &&
1110 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001111 shell_grab_finish(&resize->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001112 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001113 free(grab);
1114 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001115}
1116
Scott Moreau447013d2012-02-18 05:05:29 -07001117static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001118 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001119 resize_grab_motion,
1120 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001121};
1122
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001123static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001124surface_resize(struct shell_surface *shsurf,
1125 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001126{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001127 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001128
Alex Wu4539b082012-03-01 12:57:46 +08001129 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1130 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001131
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001132 if (edges == 0 || edges > 15 ||
1133 (edges & 3) == 3 || (edges & 12) == 12)
1134 return 0;
1135
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001136 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001137 if (!resize)
1138 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001139
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001140 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
1141
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001142 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001143 resize->width = shsurf->surface->geometry.width;
1144 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001145
Daniel Stone37816df2012-05-16 18:45:18 +01001146 wl_pointer_start_grab(ws->seat.pointer, &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001147
Daniel Stone37816df2012-05-16 18:45:18 +01001148 wl_pointer_set_focus(ws->seat.pointer, NULL,
1149 wl_fixed_from_int(0),
1150 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001151
1152 return 0;
1153}
1154
1155static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001156shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001157 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001158 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001159{
Daniel Stone37816df2012-05-16 18:45:18 +01001160 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001161 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001162
Alex Wu4539b082012-03-01 12:57:46 +08001163 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1164 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001165
Daniel Stone37816df2012-05-16 18:45:18 +01001166 if (ws->seat.pointer->button_count == 0 ||
1167 ws->seat.pointer->grab_serial != serial ||
1168 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001169 return;
1170
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001171 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001172 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001173}
1174
Juan Zhao96879df2012-02-07 08:45:41 +08001175static struct weston_output *
1176get_default_output(struct weston_compositor *compositor)
1177{
1178 return container_of(compositor->output_list.next,
1179 struct weston_output, link);
1180}
1181
Alex Wu4539b082012-03-01 12:57:46 +08001182static void
1183shell_unset_fullscreen(struct shell_surface *shsurf)
1184{
1185 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001186 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1187 shell_surface_is_top_fullscreen(shsurf)) {
1188 weston_output_switch_mode(shsurf->fullscreen_output,
1189 shsurf->fullscreen_output->origin);
1190 }
Alex Wu4539b082012-03-01 12:57:46 +08001191 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1192 shsurf->fullscreen.framerate = 0;
1193 wl_list_remove(&shsurf->fullscreen.transform.link);
1194 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001195 if (shsurf->fullscreen.black_surface)
1196 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001197 shsurf->fullscreen.black_surface = NULL;
1198 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001199 weston_surface_set_position(shsurf->surface,
1200 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001201 if (shsurf->saved_rotation_valid) {
1202 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1203 &shsurf->rotation.transform.link);
1204 shsurf->saved_rotation_valid = false;
1205 }
Alex Wu4539b082012-03-01 12:57:46 +08001206}
1207
Pekka Paalanen98262232011-12-01 10:42:22 +02001208static int
1209reset_shell_surface_type(struct shell_surface *surface)
1210{
1211 switch (surface->type) {
1212 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001213 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001214 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001215 case SHELL_SURFACE_MAXIMIZED:
1216 surface->output = get_default_output(surface->surface->compositor);
1217 weston_surface_set_position(surface->surface,
1218 surface->saved_x,
1219 surface->saved_y);
1220 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001221 case SHELL_SURFACE_PANEL:
1222 case SHELL_SURFACE_BACKGROUND:
1223 wl_list_remove(&surface->link);
1224 wl_list_init(&surface->link);
1225 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001226 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +02001227 case SHELL_SURFACE_LOCK:
1228 wl_resource_post_error(&surface->resource,
1229 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +02001230 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +02001231 return -1;
1232 case SHELL_SURFACE_NONE:
1233 case SHELL_SURFACE_TOPLEVEL:
1234 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001235 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001236 break;
1237 }
1238
1239 surface->type = SHELL_SURFACE_NONE;
1240 return 0;
1241}
1242
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001243static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001244set_surface_type(struct shell_surface *shsurf)
1245{
1246 struct weston_surface *surface = shsurf->surface;
1247 struct shell_surface *pshsurf = shsurf->parent;
1248 struct weston_surface *pes;
1249 struct shell_surface *priv;
1250 struct desktop_shell *shell = shsurf->shell;
1251
1252 reset_shell_surface_type(shsurf);
1253
1254 shsurf->type = shsurf->next_type;
1255 shsurf->next_type = SHELL_SURFACE_NONE;
1256
1257 switch (shsurf->type) {
1258 case SHELL_SURFACE_TOPLEVEL:
1259 break;
1260 case SHELL_SURFACE_TRANSIENT:
1261 pes = pshsurf->surface;
1262 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001263 pes->geometry.x + shsurf->transient.x,
1264 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001265 break;
1266
1267 case SHELL_SURFACE_MAXIMIZED:
1268 shsurf->saved_x = surface->geometry.x;
1269 shsurf->saved_y = surface->geometry.y;
1270 shsurf->saved_position_valid = true;
1271 break;
1272
1273 case SHELL_SURFACE_FULLSCREEN:
1274 shsurf->saved_x = surface->geometry.x;
1275 shsurf->saved_y = surface->geometry.y;
1276 shsurf->saved_position_valid = true;
1277
1278 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1279 wl_list_remove(&shsurf->rotation.transform.link);
1280 wl_list_init(&shsurf->rotation.transform.link);
1281 shsurf->surface->geometry.dirty = 1;
1282 shsurf->saved_rotation_valid = true;
1283 }
1284 break;
1285
1286 case SHELL_SURFACE_BACKGROUND:
1287 wl_list_for_each(priv, &shell->backgrounds, link) {
1288 if (priv->output == shsurf->output) {
1289 priv->surface->output = NULL;
1290 wl_list_remove(&priv->surface->layer_link);
1291 wl_list_remove(&priv->link);
1292 break;
1293 }
1294 }
1295
1296 wl_list_insert(&shell->backgrounds, &shsurf->link);
1297
1298 weston_surface_set_position(surface, shsurf->output->x,
1299 shsurf->output->y);
1300 break;
1301
1302 case SHELL_SURFACE_PANEL:
1303 wl_list_for_each(priv, &shell->panels, link) {
1304 if (priv->output == shsurf->output) {
1305 priv->surface->output = NULL;
1306 wl_list_remove(&priv->surface->layer_link);
1307 wl_list_remove(&priv->link);
1308 break;
1309 }
1310 }
1311
1312 wl_list_insert(&shell->panels, &shsurf->link);
1313
1314 weston_surface_set_position(surface, shsurf->output->x,
1315 shsurf->output->y);
1316 break;
1317
1318 default:
1319 break;
1320 }
1321}
1322
1323static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001324set_toplevel(struct shell_surface *shsurf)
1325{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001326 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001327}
1328
1329static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001330shell_surface_set_toplevel(struct wl_client *client,
1331 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001332{
Pekka Paalanen98262232011-12-01 10:42:22 +02001333 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001334
Tiago Vignattibc052c92012-04-19 16:18:18 +03001335 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001336}
1337
1338static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001339set_transient(struct shell_surface *shsurf,
1340 struct shell_surface *pshsurf, int x, int y, uint32_t flags)
1341{
1342 /* assign to parents output */
1343 shsurf->parent = pshsurf;
1344 shsurf->transient.x = x;
1345 shsurf->transient.y = y;
1346 shsurf->transient.flags = flags;
1347 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1348}
1349
1350static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001351shell_surface_set_transient(struct wl_client *client,
1352 struct wl_resource *resource,
1353 struct wl_resource *parent_resource,
1354 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001355{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001356 struct shell_surface *shsurf = resource->data;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001357 struct shell_surface *pshsurf = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001358
Tiago Vignatti491bac12012-05-18 16:37:43 -04001359 set_transient(shsurf, pshsurf, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001360}
1361
Tiago Vignattibe143262012-04-16 17:31:41 +03001362static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001363shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001364{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001365 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001366}
1367
1368static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001369get_output_panel_height(struct desktop_shell *shell,
1370 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001371{
1372 struct shell_surface *priv;
1373 int panel_height = 0;
1374
1375 if (!output)
1376 return 0;
1377
Tiago Vignattibe143262012-04-16 17:31:41 +03001378 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +08001379 if (priv->output == output) {
1380 panel_height = priv->surface->geometry.height;
1381 break;
1382 }
1383 }
1384 return panel_height;
1385}
1386
1387static void
1388shell_surface_set_maximized(struct wl_client *client,
1389 struct wl_resource *resource,
1390 struct wl_resource *output_resource )
1391{
1392 struct shell_surface *shsurf = resource->data;
1393 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001394 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001395 uint32_t edges = 0, panel_height = 0;
1396
1397 /* get the default output, if the client set it as NULL
1398 check whether the ouput is available */
1399 if (output_resource)
1400 shsurf->output = output_resource->data;
1401 else
1402 shsurf->output = get_default_output(es->compositor);
1403
Tiago Vignattibe143262012-04-16 17:31:41 +03001404 shell = shell_surface_get_shell(shsurf);
1405 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001406 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001407
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001408 shsurf->client->send_configure(shsurf->surface, edges,
1409 es->output->current->width,
1410 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001411
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001412 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001413}
1414
Alex Wu21858432012-04-01 20:13:08 +08001415static void
1416black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1417
Alex Wu4539b082012-03-01 12:57:46 +08001418static struct weston_surface *
1419create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001420 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001421 GLfloat x, GLfloat y, int w, int h)
1422{
1423 struct weston_surface *surface = NULL;
1424
1425 surface = weston_surface_create(ec);
1426 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001427 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001428 return NULL;
1429 }
1430
Alex Wu21858432012-04-01 20:13:08 +08001431 surface->configure = black_surface_configure;
1432 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001433 weston_surface_configure(surface, x, y, w, h);
1434 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1435 return surface;
1436}
1437
1438/* Create black surface and append it to the associated fullscreen surface.
1439 * Handle size dismatch and positioning according to the method. */
1440static void
1441shell_configure_fullscreen(struct shell_surface *shsurf)
1442{
1443 struct weston_output *output = shsurf->fullscreen_output;
1444 struct weston_surface *surface = shsurf->surface;
1445 struct weston_matrix *matrix;
1446 float scale;
1447
1448 center_on_output(surface, output);
1449
1450 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001451 shsurf->fullscreen.black_surface =
1452 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001453 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001454 output->x, output->y,
1455 output->current->width,
1456 output->current->height);
1457
1458 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1459 wl_list_insert(&surface->layer_link,
1460 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001461 shsurf->fullscreen.black_surface->output = output;
1462
1463 switch (shsurf->fullscreen.type) {
1464 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1465 break;
1466 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1467 matrix = &shsurf->fullscreen.transform.matrix;
1468 weston_matrix_init(matrix);
1469 scale = (float)output->current->width/(float)surface->geometry.width;
1470 weston_matrix_scale(matrix, scale, scale, 1);
1471 wl_list_remove(&shsurf->fullscreen.transform.link);
1472 wl_list_insert(surface->geometry.transformation_list.prev,
1473 &shsurf->fullscreen.transform.link);
1474 weston_surface_set_position(surface, output->x, output->y);
1475 break;
1476 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001477 if (shell_surface_is_top_fullscreen(shsurf)) {
1478 struct weston_mode mode = {0,
1479 surface->geometry.width,
1480 surface->geometry.height,
1481 shsurf->fullscreen.framerate};
1482
1483 if (weston_output_switch_mode(output, &mode) == 0) {
1484 weston_surface_configure(shsurf->fullscreen.black_surface,
1485 output->x, output->y,
1486 output->current->width,
1487 output->current->height);
1488 weston_surface_set_position(surface, output->x, output->y);
1489 break;
1490 }
1491 }
Alex Wu4539b082012-03-01 12:57:46 +08001492 break;
1493 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1494 break;
1495 default:
1496 break;
1497 }
1498}
1499
1500/* make the fullscreen and black surface at the top */
1501static void
1502shell_stack_fullscreen(struct shell_surface *shsurf)
1503{
Alex Wubd3354b2012-04-17 17:20:49 +08001504 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001505 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001506 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001507
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001508 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001509 wl_list_insert(&shell->fullscreen_layer.surface_list,
1510 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001511 weston_surface_damage(surface);
1512
1513 if (!shsurf->fullscreen.black_surface)
1514 shsurf->fullscreen.black_surface =
1515 create_black_surface(surface->compositor,
1516 surface,
1517 output->x, output->y,
1518 output->current->width,
1519 output->current->height);
1520
1521 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001522 wl_list_insert(&surface->layer_link,
1523 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001524 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001525}
1526
1527static void
1528shell_map_fullscreen(struct shell_surface *shsurf)
1529{
Alex Wu4539b082012-03-01 12:57:46 +08001530 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001531 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001532}
1533
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001534static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001535shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001536 struct wl_resource *resource,
1537 uint32_t method,
1538 uint32_t framerate,
1539 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001540{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001541 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001542 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001543
1544 if (output_resource)
1545 shsurf->output = output_resource->data;
1546 else
1547 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001548
Alex Wu4539b082012-03-01 12:57:46 +08001549 shsurf->fullscreen_output = shsurf->output;
1550 shsurf->fullscreen.type = method;
1551 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001552 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001553
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001554 shsurf->client->send_configure(shsurf->surface, 0,
1555 shsurf->output->current->width,
1556 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001557}
1558
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001559static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001560popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001561 struct wl_surface *surface,
1562 wl_fixed_t x,
1563 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001564{
Daniel Stone37816df2012-05-16 18:45:18 +01001565 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001566 struct shell_surface *priv =
1567 container_of(grab, struct shell_surface, popup.grab);
1568 struct wl_client *client = priv->surface->surface.resource.client;
1569
Pekka Paalanencb108432012-01-19 16:25:40 +02001570 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001571 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001572 grab->focus = surface;
1573 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001574 wl_pointer_set_focus(pointer, NULL,
1575 wl_fixed_from_int(0),
1576 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001577 grab->focus = NULL;
1578 }
1579}
1580
1581static void
Scott Moreau447013d2012-02-18 05:05:29 -07001582popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001583 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001584{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001585 struct wl_resource *resource;
1586
Daniel Stone37816df2012-05-16 18:45:18 +01001587 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001588 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001589 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001590}
1591
1592static void
Scott Moreau447013d2012-02-18 05:05:29 -07001593popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001594 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001595{
1596 struct wl_resource *resource;
1597 struct shell_surface *shsurf =
1598 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001599 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001600 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001601 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001602
Daniel Stone37816df2012-05-16 18:45:18 +01001603 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001604 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001605 display = wl_client_get_display(resource->client);
1606 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001607 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001608 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001609 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001610 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001611 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001612 wl_pointer_end_grab(grab->pointer);
1613 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001614 }
1615
Daniel Stone4dbadb12012-05-30 16:31:51 +01001616 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001617 shsurf->popup.initial_up = 1;
1618}
1619
Scott Moreau447013d2012-02-18 05:05:29 -07001620static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001621 popup_grab_focus,
1622 popup_grab_motion,
1623 popup_grab_button,
1624};
1625
1626static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001627shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001628{
Daniel Stone37816df2012-05-16 18:45:18 +01001629 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001630 struct weston_surface *es = shsurf->surface;
1631 struct weston_surface *parent = shsurf->parent->surface;
1632
1633 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001634 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001635
Pekka Paalanen938269a2012-02-07 14:19:01 +02001636 weston_surface_update_transform(parent);
1637 if (parent->transform.enabled) {
1638 shsurf->popup.parent_transform.matrix =
1639 parent->transform.matrix;
1640 } else {
1641 /* construct x, y translation matrix */
1642 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1643 shsurf->popup.parent_transform.matrix.d[12] =
1644 parent->geometry.x;
1645 shsurf->popup.parent_transform.matrix.d[13] =
1646 parent->geometry.y;
1647 }
1648 wl_list_insert(es->geometry.transformation_list.prev,
1649 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001650 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001651
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001652 shsurf->popup.initial_up = 0;
1653
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001654 /* We don't require the grab to still be active, but if another
1655 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001656 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1657 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001658 } else {
1659 wl_shell_surface_send_popup_done(&shsurf->resource);
1660 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001661}
1662
1663static void
1664shell_surface_set_popup(struct wl_client *client,
1665 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001666 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001667 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001668 struct wl_resource *parent_resource,
1669 int32_t x, int32_t y, uint32_t flags)
1670{
1671 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001672
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001673 shsurf->type = SHELL_SURFACE_POPUP;
1674 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001675 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001676 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001677 shsurf->popup.x = x;
1678 shsurf->popup.y = y;
1679}
1680
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001681static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001682 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001683 shell_surface_move,
1684 shell_surface_resize,
1685 shell_surface_set_toplevel,
1686 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001687 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001688 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001689 shell_surface_set_maximized,
1690 shell_surface_set_title,
1691 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001692};
1693
1694static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001695destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001696{
Daniel Stone37816df2012-05-16 18:45:18 +01001697 if (shsurf->popup.grab.pointer)
1698 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001699
Alex Wubd3354b2012-04-17 17:20:49 +08001700 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1701 shell_surface_is_top_fullscreen(shsurf)) {
1702 weston_output_switch_mode(shsurf->fullscreen_output,
1703 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001704 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001705
Alex Wuaa08e2d2012-03-05 11:01:40 +08001706 if (shsurf->fullscreen.black_surface)
1707 weston_surface_destroy(shsurf->fullscreen.black_surface);
1708
Alex Wubd3354b2012-04-17 17:20:49 +08001709 /* As destroy_resource() use wl_list_for_each_safe(),
1710 * we can always remove the listener.
1711 */
1712 wl_list_remove(&shsurf->surface_destroy_listener.link);
1713 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001714 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001715
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001716 wl_list_remove(&shsurf->link);
1717 free(shsurf);
1718}
1719
1720static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001721shell_destroy_shell_surface(struct wl_resource *resource)
1722{
1723 struct shell_surface *shsurf = resource->data;
1724
1725 destroy_shell_surface(shsurf);
1726}
1727
1728static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001729shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001730{
1731 struct shell_surface *shsurf = container_of(listener,
1732 struct shell_surface,
1733 surface_destroy_listener);
1734
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001735 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001736 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001737 } else {
1738 wl_signal_emit(&shsurf->resource.destroy_signal,
1739 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001740 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001741 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001742}
1743
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001744static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001745get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001746{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001747 struct wl_listener *listener;
1748
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001749 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1750 shell_handle_surface_destroy);
1751 if (listener)
1752 return container_of(listener, struct shell_surface,
1753 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001754
1755 return NULL;
1756}
1757
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001758static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001759shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1760
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001761static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001762create_shell_surface(void *shell, struct weston_surface *surface,
1763 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001764{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001765 struct shell_surface *shsurf;
1766
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001767 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001768 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001769 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001770 }
1771
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001772 shsurf = calloc(1, sizeof *shsurf);
1773 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001774 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001775 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001776 }
1777
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001778 surface->configure = shell_surface_configure;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001779 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001780
Tiago Vignattibc052c92012-04-19 16:18:18 +03001781 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001782 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001783 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001784 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001785 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001786 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1787 shsurf->fullscreen.framerate = 0;
1788 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001789 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001790 wl_list_init(&shsurf->fullscreen.transform.link);
1791
Tiago Vignattibc052c92012-04-19 16:18:18 +03001792 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001793 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1794 wl_signal_add(&surface->surface.resource.destroy_signal,
1795 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001796
1797 /* init link so its safe to always remove it in destroy_shell_surface */
1798 wl_list_init(&shsurf->link);
1799
Pekka Paalanen460099f2012-01-20 16:48:25 +02001800 /* empty when not in use */
1801 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001802 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001803
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001804 wl_list_init(&shsurf->workspace_transform.link);
1805
Pekka Paalanen98262232011-12-01 10:42:22 +02001806 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001807 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001808
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001809 shsurf->client = client;
1810
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001811 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001812}
1813
1814static void
1815shell_get_shell_surface(struct wl_client *client,
1816 struct wl_resource *resource,
1817 uint32_t id,
1818 struct wl_resource *surface_resource)
1819{
1820 struct weston_surface *surface = surface_resource->data;
1821 struct desktop_shell *shell = resource->data;
1822 struct shell_surface *shsurf;
1823
1824 if (get_shell_surface(surface)) {
1825 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001826 WL_DISPLAY_ERROR_INVALID_OBJECT,
1827 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001828 return;
1829 }
1830
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001831 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001832 if (!shsurf) {
1833 wl_resource_post_error(surface_resource,
1834 WL_DISPLAY_ERROR_INVALID_OBJECT,
1835 "surface->configure already set");
1836 return;
1837 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001838
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001839 shsurf->resource.destroy = shell_destroy_shell_surface;
1840 shsurf->resource.object.id = id;
1841 shsurf->resource.object.interface = &wl_shell_surface_interface;
1842 shsurf->resource.object.implementation =
1843 (void (**)(void)) &shell_surface_implementation;
1844 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001845
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001846 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001847}
1848
1849static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001850 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001851};
1852
Kristian Høgsberg07937562011-04-12 17:25:42 -04001853static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001854handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001855{
1856 proc->pid = 0;
1857}
1858
1859static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001860launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001861{
1862 if (shell->screensaver.binding)
1863 return;
1864
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001865 if (!shell->screensaver.path)
1866 return;
1867
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001868 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001869 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001870 return;
1871 }
1872
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001873 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001874 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001875 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001876 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001877}
1878
1879static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001880terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001881{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001882 if (shell->screensaver.process.pid == 0)
1883 return;
1884
1885 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001886}
1887
1888static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001889show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001890{
1891 struct wl_list *list;
1892
1893 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001894 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001895 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001896 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001897
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001898 wl_list_remove(&surface->surface->layer_link);
1899 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001900 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001901 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001902}
1903
1904static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001905hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001906{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001907 wl_list_remove(&surface->surface->layer_link);
1908 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001909 surface->surface->output = NULL;
1910}
1911
1912static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001913desktop_shell_set_background(struct wl_client *client,
1914 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001915 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001916 struct wl_resource *surface_resource)
1917{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001918 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001919
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001920 shsurf->next_type = SHELL_SURFACE_BACKGROUND;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001921 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001922
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001923 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001924 surface_resource,
1925 shsurf->output->current->width,
1926 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001927}
1928
1929static void
1930desktop_shell_set_panel(struct wl_client *client,
1931 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001932 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001933 struct wl_resource *surface_resource)
1934{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001935 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001936
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001937 shsurf->next_type = SHELL_SURFACE_PANEL;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001938 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001939
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001940 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001941 surface_resource,
1942 shsurf->output->current->width,
1943 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001944}
1945
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001946static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001947handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001948{
Tiago Vignattibe143262012-04-16 17:31:41 +03001949 struct desktop_shell *shell =
1950 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001951
Martin Minarik6d118362012-06-07 18:01:59 +02001952 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001953 shell->lock_surface = NULL;
1954}
1955
1956static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001957desktop_shell_set_lock_surface(struct wl_client *client,
1958 struct wl_resource *resource,
1959 struct wl_resource *surface_resource)
1960{
Tiago Vignattibe143262012-04-16 17:31:41 +03001961 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001962 struct shell_surface *surface = surface_resource->data;
1963
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001964 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001965
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001966 if (!shell->locked)
1967 return;
1968
Pekka Paalanen98262232011-12-01 10:42:22 +02001969 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001970
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001971 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1972 wl_signal_add(&surface_resource->destroy_signal,
1973 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001974
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001975 shell->lock_surface->next_type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001976}
1977
1978static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001979resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001980{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001981 struct shell_surface *tmp;
1982
1983 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1984 hide_screensaver(shell, tmp);
1985
1986 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001987
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001988 wl_list_remove(&shell->lock_layer.link);
1989 wl_list_insert(&shell->compositor->cursor_layer.link,
1990 &shell->fullscreen_layer.link);
1991 wl_list_insert(&shell->fullscreen_layer.link,
1992 &shell->panel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001993
Jonas Ådahl04769742012-06-13 00:01:24 +02001994 pop_focus_state(shell, get_current_workspace(shell));
1995
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001996 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001997 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001998 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001999 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002000}
2001
2002static void
2003desktop_shell_unlock(struct wl_client *client,
2004 struct wl_resource *resource)
2005{
Tiago Vignattibe143262012-04-16 17:31:41 +03002006 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002007
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002008 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002009
2010 if (shell->locked)
2011 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002012}
2013
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002014static void
2015desktop_shell_set_busy_surface(struct wl_client *client,
2016 struct wl_resource *resource,
2017 struct wl_resource *surface_resource)
2018{
2019 struct desktop_shell *shell = resource->data;
2020
2021 shell->busy_surface = surface_resource->data;
2022}
2023
Kristian Høgsberg75840622011-09-06 13:48:16 -04002024static const struct desktop_shell_interface desktop_shell_implementation = {
2025 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002026 desktop_shell_set_panel,
2027 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002028 desktop_shell_unlock,
2029 desktop_shell_set_busy_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002030};
2031
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002032static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002033get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002034{
2035 struct shell_surface *shsurf;
2036
2037 shsurf = get_shell_surface(surface);
2038 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002039 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002040 return shsurf->type;
2041}
2042
Kristian Høgsberg75840622011-09-06 13:48:16 -04002043static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002044move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002045{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002046 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002047 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002048 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002049
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002050 if (surface == NULL)
2051 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002052
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002053 shsurf = get_shell_surface(surface);
2054 if (shsurf == NULL)
2055 return;
2056
2057 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002058 case SHELL_SURFACE_PANEL:
2059 case SHELL_SURFACE_BACKGROUND:
2060 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02002061 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002062 return;
2063 default:
2064 break;
2065 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04002066
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002067 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002068}
2069
2070static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002071resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002072{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002073 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002074 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002075 uint32_t edges = 0;
2076 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002077 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002078
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002079 if (surface == NULL)
2080 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002081
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002082 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002083 if (!shsurf)
2084 return;
2085
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002086 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002087 case SHELL_SURFACE_PANEL:
2088 case SHELL_SURFACE_BACKGROUND:
2089 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02002090 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002091 return;
2092 default:
2093 break;
2094 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04002095
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002096 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002097 wl_fixed_to_int(seat->pointer->grab_x),
2098 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002099 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002100
Pekka Paalanen60921e52012-01-25 15:55:43 +02002101 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002102 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002103 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002104 edges |= 0;
2105 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002106 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002107
Pekka Paalanen60921e52012-01-25 15:55:43 +02002108 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002109 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002110 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002111 edges |= 0;
2112 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002113 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002114
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002115 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002116}
2117
2118static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002119surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002120 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002121{
Scott Moreau02709af2012-05-22 01:54:10 -06002122 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002123 struct shell_surface *shsurf;
2124 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002125 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002126
2127 if (surface == NULL)
2128 return;
2129
2130 shsurf = get_shell_surface(surface);
2131 if (!shsurf)
2132 return;
2133
2134 switch (shsurf->type) {
2135 case SHELL_SURFACE_BACKGROUND:
2136 case SHELL_SURFACE_SCREENSAVER:
2137 return;
2138 default:
2139 break;
2140 }
2141
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002142 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002143
Scott Moreau02709af2012-05-22 01:54:10 -06002144 if (surface->alpha > 1.0)
2145 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002146 if (surface->alpha < step)
2147 surface->alpha = step;
2148
2149 surface->geometry.dirty = 1;
2150 weston_surface_damage(surface);
2151}
2152
2153static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002154do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002155 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002156{
Daniel Stone37816df2012-05-16 18:45:18 +01002157 struct weston_seat *ws = (struct weston_seat *) seat;
2158 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002159 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002160 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002161
2162 wl_list_for_each(output, &compositor->output_list, link) {
2163 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002164 wl_fixed_to_double(seat->pointer->x),
2165 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002166 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002167 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002168 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002169 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002170 increment = -output->zoom.increment;
2171 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002172 increment = output->zoom.increment *
2173 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002174 else
2175 increment = 0;
2176
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002177 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002178
Scott Moreaue6603982012-06-11 13:07:51 -06002179 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002180 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002181 else if (output->zoom.level > output->zoom.max_level)
2182 output->zoom.level = output->zoom.max_level;
2183 else
2184 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002185
Scott Moreaue6603982012-06-11 13:07:51 -06002186 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002187
Scott Moreau8dacaab2012-06-17 18:10:58 -06002188 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002189 }
2190 }
2191}
2192
Scott Moreauccbf29d2012-02-22 14:21:41 -07002193static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002194zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002195 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002196{
2197 do_zoom(seat, time, 0, axis, value);
2198}
2199
2200static void
2201zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2202 void *data)
2203{
2204 do_zoom(seat, time, key, 0, 0);
2205}
2206
2207static void
2208terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2209 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002210{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002211 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002212
Daniel Stone325fc2d2012-05-30 16:31:58 +01002213 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002214}
2215
2216static void
Scott Moreau447013d2012-02-18 05:05:29 -07002217rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002218 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002219{
2220 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002221 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002222 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002223 struct shell_surface *shsurf = rotate->base.shsurf;
2224 struct weston_surface *surface;
2225 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2226
2227 if (!shsurf)
2228 return;
2229
2230 surface = shsurf->surface;
2231
2232 cx = 0.5f * surface->geometry.width;
2233 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002234
Daniel Stone37816df2012-05-16 18:45:18 +01002235 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2236 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002237 r = sqrtf(dx * dx + dy * dy);
2238
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002239 wl_list_remove(&shsurf->rotation.transform.link);
2240 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002241
2242 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002243 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002244 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002245
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002246 weston_matrix_init(&rotate->rotation);
2247 rotate->rotation.d[0] = dx / r;
2248 rotate->rotation.d[4] = -dy / r;
2249 rotate->rotation.d[1] = -rotate->rotation.d[4];
2250 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002251
2252 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002253 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002254 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002255 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002256 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002257
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002258 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002259 &shsurf->surface->geometry.transformation_list,
2260 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002261 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002262 wl_list_init(&shsurf->rotation.transform.link);
2263 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002264 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002265 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002266
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002267 /* We need to adjust the position of the surface
2268 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002269 cposx = surface->geometry.x + cx;
2270 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002271 dposx = rotate->center.x - cposx;
2272 dposy = rotate->center.y - cposy;
2273 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002274 weston_surface_set_position(surface,
2275 surface->geometry.x + dposx,
2276 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002277 }
2278
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002279 /* Repaint implies weston_surface_update_transform(), which
2280 * lazily applies the damage due to rotation update.
2281 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002282 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002283}
2284
2285static void
Scott Moreau447013d2012-02-18 05:05:29 -07002286rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002287 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002288{
2289 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002290 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002291 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002292 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002293 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002294
Daniel Stone4dbadb12012-05-30 16:31:51 +01002295 if (pointer->button_count == 0 &&
2296 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002297 if (shsurf)
2298 weston_matrix_multiply(&shsurf->rotation.rotation,
2299 &rotate->rotation);
2300 shell_grab_finish(&rotate->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002301 wl_pointer_end_grab(pointer);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002302 free(rotate);
2303 }
2304}
2305
Scott Moreau447013d2012-02-18 05:05:29 -07002306static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002307 noop_grab_focus,
2308 rotate_grab_motion,
2309 rotate_grab_button,
2310};
2311
2312static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002313rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002314 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002315{
2316 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002317 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002318 struct shell_surface *surface;
2319 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002320 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002321 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002322
2323 if (base_surface == NULL)
2324 return;
2325
2326 surface = get_shell_surface(base_surface);
2327 if (!surface)
2328 return;
2329
2330 switch (surface->type) {
2331 case SHELL_SURFACE_PANEL:
2332 case SHELL_SURFACE_BACKGROUND:
2333 case SHELL_SURFACE_FULLSCREEN:
2334 case SHELL_SURFACE_SCREENSAVER:
2335 return;
2336 default:
2337 break;
2338 }
2339
Pekka Paalanen460099f2012-01-20 16:48:25 +02002340 rotate = malloc(sizeof *rotate);
2341 if (!rotate)
2342 return;
2343
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002344 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02002345
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002346 weston_surface_to_global_float(surface->surface,
2347 surface->surface->geometry.width / 2,
2348 surface->surface->geometry.height / 2,
2349 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002350
Daniel Stone37816df2012-05-16 18:45:18 +01002351 wl_pointer_start_grab(seat->pointer, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002352
Daniel Stone37816df2012-05-16 18:45:18 +01002353 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2354 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002355 r = sqrtf(dx * dx + dy * dy);
2356 if (r > 20.0f) {
2357 struct weston_matrix inverse;
2358
2359 weston_matrix_init(&inverse);
2360 inverse.d[0] = dx / r;
2361 inverse.d[4] = dy / r;
2362 inverse.d[1] = -inverse.d[4];
2363 inverse.d[5] = inverse.d[0];
2364 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002365
2366 weston_matrix_init(&rotate->rotation);
2367 rotate->rotation.d[0] = dx / r;
2368 rotate->rotation.d[4] = -dy / r;
2369 rotate->rotation.d[1] = -rotate->rotation.d[4];
2370 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002371 } else {
2372 weston_matrix_init(&surface->rotation.rotation);
2373 weston_matrix_init(&rotate->rotation);
2374 }
2375
Daniel Stone37816df2012-05-16 18:45:18 +01002376 wl_pointer_set_focus(seat->pointer, NULL,
2377 wl_fixed_from_int(0),
2378 wl_fixed_from_int(0));
Pekka Paalanen460099f2012-01-20 16:48:25 +02002379}
2380
2381static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002382lower_fullscreen_layer(struct desktop_shell *shell)
2383{
2384 struct workspace *ws;
2385 struct weston_surface *surface, *prev;
2386
2387 ws = get_current_workspace(shell);
2388 wl_list_for_each_reverse_safe(surface, prev,
2389 &shell->fullscreen_layer.surface_list,
2390 layer_link)
2391 weston_surface_restack(surface, &ws->layer.surface_list);
2392}
2393
2394static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002395activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002396 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002397{
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002398 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002399
Daniel Stone37816df2012-05-16 18:45:18 +01002400 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002401
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002402 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002403 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002404 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002405 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002406 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002407
Pekka Paalanen77346a62011-11-30 16:26:35 +02002408 case SHELL_SURFACE_SCREENSAVER:
2409 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002410 if (shell->lock_surface)
2411 weston_surface_restack(es,
2412 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002413 break;
Alex Wu4539b082012-03-01 12:57:46 +08002414 case SHELL_SURFACE_FULLSCREEN:
2415 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002416 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002417 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002418 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002419 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002420 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002421 lower_fullscreen_layer(shell);
2422 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002423 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002424 }
2425}
2426
Alex Wu21858432012-04-01 20:13:08 +08002427/* no-op func for checking black surface */
2428static void
2429black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2430{
2431}
2432
2433static bool
2434is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2435{
2436 if (es->configure == black_surface_configure) {
2437 if (fs_surface)
2438 *fs_surface = (struct weston_surface *)es->private;
2439 return true;
2440 }
2441 return false;
2442}
2443
Kristian Høgsberg75840622011-09-06 13:48:16 -04002444static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002445click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002446 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002447{
Daniel Stone37816df2012-05-16 18:45:18 +01002448 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002449 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002450 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002451 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002452
Daniel Stone37816df2012-05-16 18:45:18 +01002453 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002454 if (!focus)
2455 return;
2456
Alex Wu21858432012-04-01 20:13:08 +08002457 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002458 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002459
Daniel Stone325fc2d2012-05-30 16:31:58 +01002460 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002461 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002462}
2463
2464static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002465lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002466{
Tiago Vignattibe143262012-04-16 17:31:41 +03002467 struct desktop_shell *shell =
2468 container_of(listener, struct desktop_shell, lock_listener);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002469 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002470 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002471
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002472 if (shell->locked) {
2473 wl_list_for_each(output, &shell->compositor->output_list, link)
2474 /* TODO: find a way to jump to other DPMS levels */
2475 if (output->set_dpms)
2476 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002477 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002478 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002479
2480 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002481
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002482 /* Hide all surfaces by removing the fullscreen, panel and
2483 * toplevel layers. This way nothing else can show or receive
2484 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002485
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002486 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002487 wl_list_remove(&shell->fullscreen_layer.link);
2488 wl_list_insert(&shell->compositor->cursor_layer.link,
2489 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002490
Pekka Paalanen77346a62011-11-30 16:26:35 +02002491 launch_screensaver(shell);
2492
2493 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
2494 show_screensaver(shell, shsurf);
2495
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002496 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02002497 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002498 weston_compositor_wake(shell->compositor);
2499 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002500 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002501
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002502 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002503 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002504
Jonas Ådahl04769742012-06-13 00:01:24 +02002505 /* stash keyboard foci in current workspace */
2506 push_focus_state(shell, get_current_workspace(shell));
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002507
2508 /* TODO: disable bindings that should not work while locked. */
2509
2510 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002511}
2512
2513static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002514unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002515{
Tiago Vignattibe143262012-04-16 17:31:41 +03002516 struct desktop_shell *shell =
2517 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002518
Pekka Paalanend81c2162011-11-16 13:47:34 +02002519 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002520 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002521 return;
2522 }
2523
2524 /* If desktop-shell client has gone away, unlock immediately. */
2525 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002526 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002527 return;
2528 }
2529
2530 if (shell->prepare_event_sent)
2531 return;
2532
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002533 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002534 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002535}
2536
2537static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002538center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002539{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002540 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002541 GLfloat x = (mode->width - surface->geometry.width) / 2;
2542 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002543
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002544 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002545}
2546
2547static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002548map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002549 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002550{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002551 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002552 struct shell_surface *shsurf = get_shell_surface(surface);
2553 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002554 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002555 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002556 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002557 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002558
Pekka Paalanen60921e52012-01-25 15:55:43 +02002559 surface->geometry.width = width;
2560 surface->geometry.height = height;
2561 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002562
2563 /* initial positioning, see also configure() */
2564 switch (surface_type) {
2565 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002566 weston_surface_set_position(surface, 10 + random() % 400,
2567 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002568 break;
2569 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002570 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002571 break;
Alex Wu4539b082012-03-01 12:57:46 +08002572 case SHELL_SURFACE_FULLSCREEN:
2573 shell_map_fullscreen(shsurf);
2574 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002575 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002576 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002577 panel_height = get_output_panel_height(shell,surface->output);
2578 weston_surface_set_position(surface, surface->output->x,
2579 surface->output->y + panel_height);
2580 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002581 case SHELL_SURFACE_LOCK:
2582 center_on_output(surface, get_default_output(compositor));
2583 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002584 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002585 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002586 case SHELL_SURFACE_NONE:
2587 weston_surface_set_position(surface,
2588 surface->geometry.x + sx,
2589 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002590 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002591 default:
2592 ;
2593 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002594
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002595 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002596 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002597 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002598 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002599 wl_list_insert(&shell->background_layer.surface_list,
2600 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002601 break;
2602 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002603 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002604 wl_list_insert(&shell->panel_layer.surface_list,
2605 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002606 break;
2607 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002608 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002609 wl_list_insert(&shell->lock_layer.surface_list,
2610 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002611 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002612 break;
2613 case SHELL_SURFACE_SCREENSAVER:
2614 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002615 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002616 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002617 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002618 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002619 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002620 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002621 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002622 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002623 case SHELL_SURFACE_POPUP:
2624 case SHELL_SURFACE_TRANSIENT:
2625 parent = shsurf->parent->surface;
2626 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2627 break;
Alex Wu4539b082012-03-01 12:57:46 +08002628 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002629 case SHELL_SURFACE_NONE:
2630 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002631 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002632 ws = get_current_workspace(shell);
2633 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002634 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002635 }
2636
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002637 if (surface_type != SHELL_SURFACE_NONE) {
2638 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002639 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2640 surface->output = shsurf->output;
2641 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002642
Juan Zhao7bb92f02011-12-15 11:31:51 -05002643 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002644 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002645 if (shsurf->transient.flags ==
2646 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2647 break;
2648 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002649 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002650 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002651 if (!shell->locked) {
2652 wl_list_for_each(seat, &compositor->seat_list, link)
2653 activate(shell, surface, seat);
2654 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002655 break;
2656 default:
2657 break;
2658 }
2659
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002660 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002661 {
2662 switch (shell->win_animation_type) {
2663 case ANIMATION_FADE:
2664 weston_fade_run(surface, NULL, NULL);
2665 break;
2666 case ANIMATION_ZOOM:
2667 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2668 break;
2669 default:
2670 break;
2671 }
2672 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002673}
2674
2675static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002676configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002677 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002678{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002679 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2680 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002681
Pekka Paalanen77346a62011-11-30 16:26:35 +02002682 shsurf = get_shell_surface(surface);
2683 if (shsurf)
2684 surface_type = shsurf->type;
2685
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002686 surface->geometry.x = x;
2687 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002688 surface->geometry.width = width;
2689 surface->geometry.height = height;
2690 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002691
2692 switch (surface_type) {
2693 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002694 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002695 break;
Alex Wu4539b082012-03-01 12:57:46 +08002696 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002697 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002698 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002699 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002700 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002701 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002702 surface->geometry.x = surface->output->x;
2703 surface->geometry.y = surface->output->y +
2704 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002705 break;
Alex Wu4539b082012-03-01 12:57:46 +08002706 case SHELL_SURFACE_TOPLEVEL:
2707 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002708 default:
2709 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002710 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002711
Alex Wu4539b082012-03-01 12:57:46 +08002712 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002713 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002714 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002715
2716 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2717 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002718 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2719 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002720 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002721}
2722
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002723static void
2724shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2725{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002726 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002727 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002728 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002729
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002730 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002731 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002732 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002733 type_changed = 1;
2734 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002735
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002736 if (!weston_surface_is_mapped(es)) {
2737 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002738 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002739 es->geometry.width != es->buffer->width ||
2740 es->geometry.height != es->buffer->height) {
2741 GLfloat from_x, from_y;
2742 GLfloat to_x, to_y;
2743
2744 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2745 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2746 configure(shell, es,
2747 es->geometry.x + to_x - from_x,
2748 es->geometry.y + to_y - from_y,
2749 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002750 }
2751}
2752
Tiago Vignattibe143262012-04-16 17:31:41 +03002753static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002754
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002755static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002756desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002757{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002758 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002759 struct desktop_shell *shell =
2760 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002761
2762 shell->child.process.pid = 0;
2763 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002764
2765 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2766 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002767 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002768 shell->child.deathstamp = time;
2769 shell->child.deathcount = 0;
2770 }
2771
2772 shell->child.deathcount++;
2773 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002774 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002775 return;
2776 }
2777
Martin Minarik6d118362012-06-07 18:01:59 +02002778 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002779 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002780}
2781
2782static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002783launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002784{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002785 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002786
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002787 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002788 &shell->child.process,
2789 shell_exe,
2790 desktop_shell_sigchld);
2791
2792 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002793 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002794 return 0;
2795}
2796
2797static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002798bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2799{
Tiago Vignattibe143262012-04-16 17:31:41 +03002800 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002801
2802 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002803 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002804}
2805
Kristian Høgsberg75840622011-09-06 13:48:16 -04002806static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002807unbind_desktop_shell(struct wl_resource *resource)
2808{
Tiago Vignattibe143262012-04-16 17:31:41 +03002809 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002810
2811 if (shell->locked)
2812 resume_desktop(shell);
2813
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002814 shell->child.desktop_shell = NULL;
2815 shell->prepare_event_sent = false;
2816 free(resource);
2817}
2818
2819static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002820bind_desktop_shell(struct wl_client *client,
2821 void *data, uint32_t version, uint32_t id)
2822{
Tiago Vignattibe143262012-04-16 17:31:41 +03002823 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002824 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002825
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002826 resource = wl_client_add_object(client, &desktop_shell_interface,
2827 &desktop_shell_implementation,
2828 id, shell);
2829
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002830 if (client == shell->child.client) {
2831 resource->destroy = unbind_desktop_shell;
2832 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002833 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002834 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002835
2836 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2837 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002838 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002839}
2840
Pekka Paalanen6e168112011-11-24 11:34:05 +02002841static void
2842screensaver_set_surface(struct wl_client *client,
2843 struct wl_resource *resource,
2844 struct wl_resource *shell_surface_resource,
2845 struct wl_resource *output_resource)
2846{
Tiago Vignattibe143262012-04-16 17:31:41 +03002847 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002848 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002849 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002850
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002851 surface->next_type = SHELL_SURFACE_SCREENSAVER;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002852
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002853 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002854 surface->output = output;
2855 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002856}
2857
2858static const struct screensaver_interface screensaver_implementation = {
2859 screensaver_set_surface
2860};
2861
2862static void
2863unbind_screensaver(struct wl_resource *resource)
2864{
Tiago Vignattibe143262012-04-16 17:31:41 +03002865 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002866
Pekka Paalanen77346a62011-11-30 16:26:35 +02002867 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002868 free(resource);
2869}
2870
2871static void
2872bind_screensaver(struct wl_client *client,
2873 void *data, uint32_t version, uint32_t id)
2874{
Tiago Vignattibe143262012-04-16 17:31:41 +03002875 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002876 struct wl_resource *resource;
2877
2878 resource = wl_client_add_object(client, &screensaver_interface,
2879 &screensaver_implementation,
2880 id, shell);
2881
Pekka Paalanen77346a62011-11-30 16:26:35 +02002882 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002883 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002884 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002885 return;
2886 }
2887
2888 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2889 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002890 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002891}
2892
Kristian Høgsberg07045392012-02-19 18:52:44 -05002893struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002894 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002895 struct weston_surface *current;
2896 struct wl_listener listener;
2897 struct wl_keyboard_grab grab;
2898};
2899
2900static void
2901switcher_next(struct switcher *switcher)
2902{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002903 struct weston_surface *surface;
2904 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002905 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002906 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002907
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002908 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002909 switch (get_shell_surface_type(surface)) {
2910 case SHELL_SURFACE_TOPLEVEL:
2911 case SHELL_SURFACE_FULLSCREEN:
2912 case SHELL_SURFACE_MAXIMIZED:
2913 if (first == NULL)
2914 first = surface;
2915 if (prev == switcher->current)
2916 next = surface;
2917 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002918 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002919 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002920 weston_surface_damage(surface);
2921 break;
2922 default:
2923 break;
2924 }
Alex Wu1659daa2012-04-01 20:13:09 +08002925
2926 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002927 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002928 surface->geometry.dirty = 1;
2929 weston_surface_damage(surface);
2930 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002931 }
2932
2933 if (next == NULL)
2934 next = first;
2935
Alex Wu07b26062012-03-12 16:06:01 +08002936 if (next == NULL)
2937 return;
2938
Kristian Høgsberg07045392012-02-19 18:52:44 -05002939 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002940 wl_signal_add(&next->surface.resource.destroy_signal,
2941 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002942
2943 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002944 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002945
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002946 shsurf = get_shell_surface(switcher->current);
2947 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002948 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002949}
2950
2951static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002952switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002953{
2954 struct switcher *switcher =
2955 container_of(listener, struct switcher, listener);
2956
2957 switcher_next(switcher);
2958}
2959
2960static void
Daniel Stone351eb612012-05-31 15:27:47 -04002961switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002962{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002963 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002964 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002965 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002966
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002967 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002968 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002969 weston_surface_damage(surface);
2970 }
2971
Alex Wu07b26062012-03-12 16:06:01 +08002972 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01002973 activate(switcher->shell, switcher->current,
2974 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002975 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01002976 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002977 free(switcher);
2978}
2979
2980static void
2981switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002982 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002983{
2984 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01002985 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04002986
Daniel Stonec9785ea2012-05-30 16:31:52 +01002987 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04002988 switcher_next(switcher);
2989}
2990
2991static void
2992switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
2993 uint32_t mods_depressed, uint32_t mods_latched,
2994 uint32_t mods_locked, uint32_t group)
2995{
2996 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002997 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002998
Daniel Stone351eb612012-05-31 15:27:47 -04002999 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3000 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003001}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003002
3003static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003004 switcher_key,
3005 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003006};
3007
3008static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003009switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3010 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003011{
Tiago Vignattibe143262012-04-16 17:31:41 +03003012 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003013 struct switcher *switcher;
3014
3015 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003016 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003017 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003018 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003019 wl_list_init(&switcher->listener.link);
3020
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003021 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003022 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003023 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3024 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003025 switcher_next(switcher);
3026}
3027
Pekka Paalanen3c647232011-12-22 13:43:43 +02003028static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003029backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3030 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003031{
3032 struct weston_compositor *compositor = data;
3033 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003034 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003035
3036 /* TODO: we're limiting to simple use cases, where we assume just
3037 * control on the primary display. We'd have to extend later if we
3038 * ever get support for setting backlights on random desktop LCD
3039 * panels though */
3040 output = get_default_output(compositor);
3041 if (!output)
3042 return;
3043
3044 if (!output->set_backlight)
3045 return;
3046
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003047 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3048 backlight_new = output->backlight_current - 25;
3049 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3050 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003051
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003052 if (backlight_new < 5)
3053 backlight_new = 5;
3054 if (backlight_new > 255)
3055 backlight_new = 255;
3056
3057 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003058 output->set_backlight(output, output->backlight_current);
3059}
3060
3061static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003062debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3063 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003064{
Tiago Vignattibe143262012-04-16 17:31:41 +03003065 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003066 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003067 struct weston_surface *surface;
3068
3069 if (shell->debug_repaint_surface) {
3070 weston_surface_destroy(shell->debug_repaint_surface);
3071 shell->debug_repaint_surface = NULL;
3072 } else {
3073 surface = weston_surface_create(compositor);
3074 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3075 weston_surface_configure(surface, 0, 0, 8192, 8192);
3076 wl_list_insert(&compositor->fade_layer.surface_list,
3077 &surface->layer_link);
3078 weston_surface_assign_output(surface);
3079 pixman_region32_init(&surface->input);
3080
3081 /* Here's the dirty little trick that makes the
3082 * repaint debugging work: we force an
3083 * update_transform first to update dependent state
3084 * and clear the geometry.dirty bit. Then we clear
3085 * the surface damage so it only gets repainted
3086 * piecewise as we repaint other things. */
3087
3088 weston_surface_update_transform(surface);
3089 pixman_region32_fini(&surface->damage);
3090 pixman_region32_init(&surface->damage);
3091 shell->debug_repaint_surface = surface;
3092 }
3093}
3094
3095static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003096force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3097 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003098{
3099 struct wl_client *client;
3100 pid_t pid;
3101 uid_t uid;
3102 gid_t gid;
3103
Daniel Stone325fc2d2012-05-30 16:31:58 +01003104 client = seat->keyboard->focus->resource.client;
3105 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003106
Daniel Stone325fc2d2012-05-30 16:31:58 +01003107 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003108}
3109
3110static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003111workspace_up_binding(struct wl_seat *seat, uint32_t time,
3112 uint32_t key, void *data)
3113{
3114 struct desktop_shell *shell = data;
3115 unsigned int new_index = shell->workspaces.current;
3116
3117 if (new_index != 0)
3118 new_index--;
3119
3120 change_workspace(shell, new_index);
3121}
3122
3123static void
3124workspace_down_binding(struct wl_seat *seat, uint32_t time,
3125 uint32_t key, void *data)
3126{
3127 struct desktop_shell *shell = data;
3128 unsigned int new_index = shell->workspaces.current;
3129
3130 if (new_index < shell->workspaces.num - 1)
3131 new_index++;
3132
3133 change_workspace(shell, new_index);
3134}
3135
3136static void
3137workspace_f_binding(struct wl_seat *seat, uint32_t time,
3138 uint32_t key, void *data)
3139{
3140 struct desktop_shell *shell = data;
3141 unsigned int new_index;
3142
3143 new_index = key - KEY_F1;
3144 if (new_index >= shell->workspaces.num)
3145 new_index = shell->workspaces.num - 1;
3146
3147 change_workspace(shell, new_index);
3148}
3149
3150
3151static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003152shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003153{
Tiago Vignattibe143262012-04-16 17:31:41 +03003154 struct desktop_shell *shell =
3155 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003156 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003157
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003158 if (shell->child.client)
3159 wl_client_destroy(shell->child.client);
3160
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003161 wl_list_remove(&shell->lock_listener.link);
3162 wl_list_remove(&shell->unlock_listener.link);
3163
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003164 wl_array_for_each(ws, &shell->workspaces.array)
3165 workspace_destroy(*ws);
3166 wl_array_release(&shell->workspaces.array);
3167
Pekka Paalanen3c647232011-12-22 13:43:43 +02003168 free(shell->screensaver.path);
3169 free(shell);
3170}
3171
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003172static void
3173shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3174{
3175 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003176 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003177
3178 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003179 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3180 MODIFIER_CTRL | MODIFIER_ALT,
3181 terminate_binding, ec);
3182 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3183 click_to_activate_binding,
3184 shell);
3185 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3186 MODIFIER_SUPER | MODIFIER_ALT,
3187 surface_opacity_binding, NULL);
3188 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3189 MODIFIER_SUPER, zoom_axis_binding,
3190 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003191
3192 /* configurable bindings */
3193 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003194 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3195 zoom_key_binding, NULL);
3196 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3197 zoom_key_binding, NULL);
3198 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3199 shell);
3200 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3201 resize_binding, shell);
3202 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3203 rotate_binding, NULL);
3204 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3205 shell);
3206 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3207 ec);
3208 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3209 backlight_binding, ec);
3210 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3211 ec);
3212 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3213 backlight_binding, ec);
3214 weston_compositor_add_key_binding(ec, KEY_SPACE, mod,
3215 debug_repaint_binding, shell);
3216 weston_compositor_add_key_binding(ec, KEY_K, mod,
3217 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003218 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3219 workspace_up_binding, shell);
3220 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3221 workspace_down_binding, shell);
3222
3223 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3224 if (shell->workspaces.num > 1) {
3225 num_workspace_bindings = shell->workspaces.num;
3226 if (num_workspace_bindings > 6)
3227 num_workspace_bindings = 6;
3228 for (i = 0; i < num_workspace_bindings; i++)
3229 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3230 workspace_f_binding,
3231 shell);
3232 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003233}
3234
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003235int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003236shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003237
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003238WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003239shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003240{
Tiago Vignattibe143262012-04-16 17:31:41 +03003241 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003242 struct workspace **pws;
3243 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003244
3245 shell = malloc(sizeof *shell);
3246 if (shell == NULL)
3247 return -1;
3248
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003249 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003250 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003251
3252 shell->destroy_listener.notify = shell_destroy;
3253 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3254 shell->lock_listener.notify = lock;
3255 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3256 shell->unlock_listener.notify = unlock;
3257 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003258 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003259 ec->shell_interface.create_shell_surface = create_shell_surface;
3260 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003261 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003262 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003263 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003264
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003265 wl_list_init(&shell->backgrounds);
3266 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003267 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003268
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003269 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3270 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003271 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3272 weston_layer_init(&shell->lock_layer, NULL);
3273
3274 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003275
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003276 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003277
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003278 for (i = 0; i < shell->workspaces.num; i++) {
3279 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3280 if (pws == NULL)
3281 return -1;
3282
3283 *pws = workspace_create();
3284 if (*pws == NULL)
3285 return -1;
3286 }
3287 activate_workspace(shell, 0);
3288
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003289 wl_list_init(&shell->workspaces.animation.link);
3290 shell->workspaces.animation.frame = animate_workspace_change_frame;
3291
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003292 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3293 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003294 return -1;
3295
Kristian Høgsberg75840622011-09-06 13:48:16 -04003296 if (wl_display_add_global(ec->wl_display,
3297 &desktop_shell_interface,
3298 shell, bind_desktop_shell) == NULL)
3299 return -1;
3300
Pekka Paalanen6e168112011-11-24 11:34:05 +02003301 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3302 shell, bind_screensaver) == NULL)
3303 return -1;
3304
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003305 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003306 if (launch_desktop_shell_process(shell) != 0)
3307 return -1;
3308
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003309 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003310 if (ec->seat->seat.pointer)
3311 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3312 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003313
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003314 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003315
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003316 return 0;
3317}