blob: 7a85ff1c9806473c137dbf276eca5bcb4645185e [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;
847 struct weston_seat *seat = (struct weston_seat *) pointer->seat;
848 struct weston_surface *sprite;
849
850 grab = malloc(sizeof *grab);
851 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600852 return;
853
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400854 shell_grab_init(grab, &busy_cursor_grab_interface, shsurf);
855 grab->grab.focus = &shsurf->surface->surface;
856 wl_pointer_start_grab(pointer, &grab->grab);
857 wl_pointer_set_focus(pointer, &shell->busy_surface->surface, 0, 0);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600858
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400859 sprite = (struct weston_surface *) seat->sprite;
860 shell->busy_surface->output = sprite->output;
861}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600862
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400863static void
864end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
865{
866 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
867
868 if (grab->grab.interface == &busy_cursor_grab_interface) {
869 shell_grab_finish(grab);
870 wl_pointer_end_grab(pointer);
871 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600872 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600873}
874
Scott Moreau9521d5e2012-04-19 13:06:17 -0600875static void
876ping_timer_destroy(struct shell_surface *shsurf)
877{
878 if (!shsurf || !shsurf->ping_timer)
879 return;
880
881 if (shsurf->ping_timer->source)
882 wl_event_source_remove(shsurf->ping_timer->source);
883
884 free(shsurf->ping_timer);
885 shsurf->ping_timer = NULL;
886}
887
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400888static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600889ping_timeout_handler(void *data)
890{
891 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400892 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600893
Scott Moreau9521d5e2012-04-19 13:06:17 -0600894 /* Client is not responding */
895 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400896
897 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
898 if (seat->seat.pointer->focus == &shsurf->surface->surface)
899 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600900
901 return 1;
902}
903
904static void
905ping_handler(struct weston_surface *surface, uint32_t serial)
906{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400907 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600908 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400909 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600910
911 if (!shsurf)
912 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400913 if (!shsurf->resource.client)
914 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600915
916 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300917 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600918 if (!shsurf->ping_timer)
919 return;
920
921 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600922 loop = wl_display_get_event_loop(surface->compositor->wl_display);
923 shsurf->ping_timer->source =
924 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
925 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
926
927 wl_shell_surface_send_ping(&shsurf->resource, serial);
928 }
929}
930
931static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400932handle_pointer_focus(struct wl_listener *listener, void *data)
933{
934 struct wl_pointer *pointer = data;
935 struct weston_surface *surface =
936 (struct weston_surface *) pointer->focus;
937 struct weston_compositor *compositor;
938 struct shell_surface *shsurf;
939 uint32_t serial;
940
941 if (!surface)
942 return;
943
944 compositor = surface->compositor;
945 shsurf = get_shell_surface(surface);
946
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +0300947 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400948 set_busy_cursor(shsurf, pointer);
949 } else {
950 serial = wl_display_next_serial(compositor->wl_display);
951 ping_handler(surface, serial);
952 }
953}
954
955static void
Scott Moreauff1db4a2012-04-17 19:06:18 -0600956shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
957 uint32_t serial)
958{
959 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400960 struct desktop_shell *shell = shsurf->shell;
961 struct weston_seat *seat;
962 struct weston_compositor *ec = shsurf->surface->compositor;
963 struct wl_pointer *pointer;
964 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600965
Scott Moreauff1db4a2012-04-17 19:06:18 -0600966 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400967 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600968 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400969 if (was_unresponsive) {
970 /* Received pong from previously unresponsive client */
971 wl_list_for_each(seat, &ec->seat_list, link) {
972 pointer = seat->seat.pointer;
973 if (pointer->focus ==
974 &shell->busy_surface->surface &&
975 pointer->current ==
976 &shsurf->surface->surface)
977 end_busy_cursor(shsurf, pointer);
978 }
979 }
Scott Moreau9521d5e2012-04-19 13:06:17 -0600980 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600981 }
982}
983
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400984static void
985shell_surface_set_title(struct wl_client *client,
986 struct wl_resource *resource, const char *title)
987{
988 struct shell_surface *shsurf = resource->data;
989
990 free(shsurf->title);
991 shsurf->title = strdup(title);
992}
993
994static void
995shell_surface_set_class(struct wl_client *client,
996 struct wl_resource *resource, const char *class)
997{
998 struct shell_surface *shsurf = resource->data;
999
1000 free(shsurf->class);
1001 shsurf->class = strdup(class);
1002}
1003
Scott Moreauff1db4a2012-04-17 19:06:18 -06001004static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001005surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001006{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001007 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001008
1009 if (!shsurf)
1010 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001011
1012 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001013 if (!move)
1014 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001015
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001016 shell_grab_init(&move->base, &move_grab_interface, shsurf);
1017
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001018 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +01001019 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001020 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +01001021 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001022
Daniel Stone37816df2012-05-16 18:45:18 +01001023 wl_pointer_start_grab(ws->seat.pointer, &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001024
Daniel Stone37816df2012-05-16 18:45:18 +01001025 wl_pointer_set_focus(ws->seat.pointer, NULL,
1026 wl_fixed_from_int(0),
1027 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001028
1029 return 0;
1030}
1031
1032static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001033shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001034 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001035{
Daniel Stone37816df2012-05-16 18:45:18 +01001036 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001037 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001038
Daniel Stone37816df2012-05-16 18:45:18 +01001039 if (ws->seat.pointer->button_count == 0 ||
1040 ws->seat.pointer->grab_serial != serial ||
1041 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001042 return;
1043
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001044 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001045 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001046}
1047
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001048struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001049 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001050 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001051 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001052};
1053
1054static void
Scott Moreau447013d2012-02-18 05:05:29 -07001055resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001056 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001057{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001058 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001059 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001060 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001061 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +01001062 wl_fixed_t from_x, from_y;
1063 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001064
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001065 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001066 return;
1067
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001068 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001069 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +01001070 &from_x, &from_y);
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001071 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001072 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001073
Daniel Stone103db7f2012-05-08 17:17:55 +01001074 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001075 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001076 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001077 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001078 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001079 }
1080
Daniel Stone103db7f2012-05-08 17:17:55 +01001081 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001082 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001083 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001084 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001085 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001086 }
1087
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001088 shsurf->client->send_configure(shsurf->surface,
1089 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001090}
1091
1092static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001093send_configure(struct weston_surface *surface,
1094 uint32_t edges, int32_t width, int32_t height)
1095{
1096 struct shell_surface *shsurf = get_shell_surface(surface);
1097
1098 wl_shell_surface_send_configure(&shsurf->resource,
1099 edges, width, height);
1100}
1101
1102static const struct weston_shell_client shell_client = {
1103 send_configure
1104};
1105
1106static void
Scott Moreau447013d2012-02-18 05:05:29 -07001107resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001108 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001109{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001110 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001111 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001112 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001113
Daniel Stone4dbadb12012-05-30 16:31:51 +01001114 if (pointer->button_count == 0 &&
1115 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001116 shell_grab_finish(&resize->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001117 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001118 free(grab);
1119 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001120}
1121
Scott Moreau447013d2012-02-18 05:05:29 -07001122static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001123 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001124 resize_grab_motion,
1125 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001126};
1127
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001128static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001129surface_resize(struct shell_surface *shsurf,
1130 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001131{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001132 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001133
Alex Wu4539b082012-03-01 12:57:46 +08001134 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1135 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001136
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001137 if (edges == 0 || edges > 15 ||
1138 (edges & 3) == 3 || (edges & 12) == 12)
1139 return 0;
1140
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001141 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001142 if (!resize)
1143 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001144
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001145 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
1146
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001147 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001148 resize->width = shsurf->surface->geometry.width;
1149 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001150
Daniel Stone37816df2012-05-16 18:45:18 +01001151 wl_pointer_start_grab(ws->seat.pointer, &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001152
Daniel Stone37816df2012-05-16 18:45:18 +01001153 wl_pointer_set_focus(ws->seat.pointer, NULL,
1154 wl_fixed_from_int(0),
1155 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001156
1157 return 0;
1158}
1159
1160static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001161shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001162 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001163 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001164{
Daniel Stone37816df2012-05-16 18:45:18 +01001165 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001166 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001167
Alex Wu4539b082012-03-01 12:57:46 +08001168 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1169 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001170
Daniel Stone37816df2012-05-16 18:45:18 +01001171 if (ws->seat.pointer->button_count == 0 ||
1172 ws->seat.pointer->grab_serial != serial ||
1173 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001174 return;
1175
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001176 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001177 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001178}
1179
Juan Zhao96879df2012-02-07 08:45:41 +08001180static struct weston_output *
1181get_default_output(struct weston_compositor *compositor)
1182{
1183 return container_of(compositor->output_list.next,
1184 struct weston_output, link);
1185}
1186
Alex Wu4539b082012-03-01 12:57:46 +08001187static void
1188shell_unset_fullscreen(struct shell_surface *shsurf)
1189{
1190 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001191 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1192 shell_surface_is_top_fullscreen(shsurf)) {
1193 weston_output_switch_mode(shsurf->fullscreen_output,
1194 shsurf->fullscreen_output->origin);
1195 }
Alex Wu4539b082012-03-01 12:57:46 +08001196 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1197 shsurf->fullscreen.framerate = 0;
1198 wl_list_remove(&shsurf->fullscreen.transform.link);
1199 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001200 if (shsurf->fullscreen.black_surface)
1201 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001202 shsurf->fullscreen.black_surface = NULL;
1203 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001204 weston_surface_set_position(shsurf->surface,
1205 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001206 if (shsurf->saved_rotation_valid) {
1207 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1208 &shsurf->rotation.transform.link);
1209 shsurf->saved_rotation_valid = false;
1210 }
Alex Wu4539b082012-03-01 12:57:46 +08001211}
1212
Pekka Paalanen98262232011-12-01 10:42:22 +02001213static int
1214reset_shell_surface_type(struct shell_surface *surface)
1215{
1216 switch (surface->type) {
1217 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001218 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001219 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001220 case SHELL_SURFACE_MAXIMIZED:
1221 surface->output = get_default_output(surface->surface->compositor);
1222 weston_surface_set_position(surface->surface,
1223 surface->saved_x,
1224 surface->saved_y);
1225 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001226 case SHELL_SURFACE_PANEL:
1227 case SHELL_SURFACE_BACKGROUND:
1228 wl_list_remove(&surface->link);
1229 wl_list_init(&surface->link);
1230 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001231 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +02001232 case SHELL_SURFACE_LOCK:
1233 wl_resource_post_error(&surface->resource,
1234 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +02001235 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +02001236 return -1;
1237 case SHELL_SURFACE_NONE:
1238 case SHELL_SURFACE_TOPLEVEL:
1239 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001240 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001241 break;
1242 }
1243
1244 surface->type = SHELL_SURFACE_NONE;
1245 return 0;
1246}
1247
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001248static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001249set_surface_type(struct shell_surface *shsurf)
1250{
1251 struct weston_surface *surface = shsurf->surface;
1252 struct shell_surface *pshsurf = shsurf->parent;
1253 struct weston_surface *pes;
1254 struct shell_surface *priv;
1255 struct desktop_shell *shell = shsurf->shell;
1256
1257 reset_shell_surface_type(shsurf);
1258
1259 shsurf->type = shsurf->next_type;
1260 shsurf->next_type = SHELL_SURFACE_NONE;
1261
1262 switch (shsurf->type) {
1263 case SHELL_SURFACE_TOPLEVEL:
1264 break;
1265 case SHELL_SURFACE_TRANSIENT:
1266 pes = pshsurf->surface;
1267 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001268 pes->geometry.x + shsurf->transient.x,
1269 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001270 break;
1271
1272 case SHELL_SURFACE_MAXIMIZED:
1273 shsurf->saved_x = surface->geometry.x;
1274 shsurf->saved_y = surface->geometry.y;
1275 shsurf->saved_position_valid = true;
1276 break;
1277
1278 case SHELL_SURFACE_FULLSCREEN:
1279 shsurf->saved_x = surface->geometry.x;
1280 shsurf->saved_y = surface->geometry.y;
1281 shsurf->saved_position_valid = true;
1282
1283 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1284 wl_list_remove(&shsurf->rotation.transform.link);
1285 wl_list_init(&shsurf->rotation.transform.link);
1286 shsurf->surface->geometry.dirty = 1;
1287 shsurf->saved_rotation_valid = true;
1288 }
1289 break;
1290
1291 case SHELL_SURFACE_BACKGROUND:
1292 wl_list_for_each(priv, &shell->backgrounds, link) {
1293 if (priv->output == shsurf->output) {
1294 priv->surface->output = NULL;
1295 wl_list_remove(&priv->surface->layer_link);
1296 wl_list_remove(&priv->link);
1297 break;
1298 }
1299 }
1300
1301 wl_list_insert(&shell->backgrounds, &shsurf->link);
1302
1303 weston_surface_set_position(surface, shsurf->output->x,
1304 shsurf->output->y);
1305 break;
1306
1307 case SHELL_SURFACE_PANEL:
1308 wl_list_for_each(priv, &shell->panels, link) {
1309 if (priv->output == shsurf->output) {
1310 priv->surface->output = NULL;
1311 wl_list_remove(&priv->surface->layer_link);
1312 wl_list_remove(&priv->link);
1313 break;
1314 }
1315 }
1316
1317 wl_list_insert(&shell->panels, &shsurf->link);
1318
1319 weston_surface_set_position(surface, shsurf->output->x,
1320 shsurf->output->y);
1321 break;
1322
1323 default:
1324 break;
1325 }
1326}
1327
1328static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001329set_toplevel(struct shell_surface *shsurf)
1330{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001331 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001332}
1333
1334static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001335shell_surface_set_toplevel(struct wl_client *client,
1336 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001337{
Pekka Paalanen98262232011-12-01 10:42:22 +02001338 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001339
Tiago Vignattibc052c92012-04-19 16:18:18 +03001340 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001341}
1342
1343static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001344set_transient(struct shell_surface *shsurf,
1345 struct shell_surface *pshsurf, int x, int y, uint32_t flags)
1346{
1347 /* assign to parents output */
1348 shsurf->parent = pshsurf;
1349 shsurf->transient.x = x;
1350 shsurf->transient.y = y;
1351 shsurf->transient.flags = flags;
1352 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1353}
1354
1355static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001356shell_surface_set_transient(struct wl_client *client,
1357 struct wl_resource *resource,
1358 struct wl_resource *parent_resource,
1359 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001360{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001361 struct shell_surface *shsurf = resource->data;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001362 struct shell_surface *pshsurf = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001363
Tiago Vignatti491bac12012-05-18 16:37:43 -04001364 set_transient(shsurf, pshsurf, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001365}
1366
Tiago Vignattibe143262012-04-16 17:31:41 +03001367static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001368shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001369{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001370 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001371}
1372
1373static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001374get_output_panel_height(struct desktop_shell *shell,
1375 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001376{
1377 struct shell_surface *priv;
1378 int panel_height = 0;
1379
1380 if (!output)
1381 return 0;
1382
Tiago Vignattibe143262012-04-16 17:31:41 +03001383 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +08001384 if (priv->output == output) {
1385 panel_height = priv->surface->geometry.height;
1386 break;
1387 }
1388 }
1389 return panel_height;
1390}
1391
1392static void
1393shell_surface_set_maximized(struct wl_client *client,
1394 struct wl_resource *resource,
1395 struct wl_resource *output_resource )
1396{
1397 struct shell_surface *shsurf = resource->data;
1398 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001399 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001400 uint32_t edges = 0, panel_height = 0;
1401
1402 /* get the default output, if the client set it as NULL
1403 check whether the ouput is available */
1404 if (output_resource)
1405 shsurf->output = output_resource->data;
1406 else
1407 shsurf->output = get_default_output(es->compositor);
1408
Tiago Vignattibe143262012-04-16 17:31:41 +03001409 shell = shell_surface_get_shell(shsurf);
1410 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001411 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001412
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001413 shsurf->client->send_configure(shsurf->surface, edges,
1414 es->output->current->width,
1415 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001416
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001417 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001418}
1419
Alex Wu21858432012-04-01 20:13:08 +08001420static void
1421black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1422
Alex Wu4539b082012-03-01 12:57:46 +08001423static struct weston_surface *
1424create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001425 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001426 GLfloat x, GLfloat y, int w, int h)
1427{
1428 struct weston_surface *surface = NULL;
1429
1430 surface = weston_surface_create(ec);
1431 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001432 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001433 return NULL;
1434 }
1435
Alex Wu21858432012-04-01 20:13:08 +08001436 surface->configure = black_surface_configure;
1437 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001438 weston_surface_configure(surface, x, y, w, h);
1439 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1440 return surface;
1441}
1442
1443/* Create black surface and append it to the associated fullscreen surface.
1444 * Handle size dismatch and positioning according to the method. */
1445static void
1446shell_configure_fullscreen(struct shell_surface *shsurf)
1447{
1448 struct weston_output *output = shsurf->fullscreen_output;
1449 struct weston_surface *surface = shsurf->surface;
1450 struct weston_matrix *matrix;
1451 float scale;
1452
1453 center_on_output(surface, output);
1454
1455 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001456 shsurf->fullscreen.black_surface =
1457 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001458 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001459 output->x, output->y,
1460 output->current->width,
1461 output->current->height);
1462
1463 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1464 wl_list_insert(&surface->layer_link,
1465 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001466 shsurf->fullscreen.black_surface->output = output;
1467
1468 switch (shsurf->fullscreen.type) {
1469 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1470 break;
1471 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1472 matrix = &shsurf->fullscreen.transform.matrix;
1473 weston_matrix_init(matrix);
1474 scale = (float)output->current->width/(float)surface->geometry.width;
1475 weston_matrix_scale(matrix, scale, scale, 1);
1476 wl_list_remove(&shsurf->fullscreen.transform.link);
1477 wl_list_insert(surface->geometry.transformation_list.prev,
1478 &shsurf->fullscreen.transform.link);
1479 weston_surface_set_position(surface, output->x, output->y);
1480 break;
1481 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001482 if (shell_surface_is_top_fullscreen(shsurf)) {
1483 struct weston_mode mode = {0,
1484 surface->geometry.width,
1485 surface->geometry.height,
1486 shsurf->fullscreen.framerate};
1487
1488 if (weston_output_switch_mode(output, &mode) == 0) {
1489 weston_surface_configure(shsurf->fullscreen.black_surface,
1490 output->x, output->y,
1491 output->current->width,
1492 output->current->height);
1493 weston_surface_set_position(surface, output->x, output->y);
1494 break;
1495 }
1496 }
Alex Wu4539b082012-03-01 12:57:46 +08001497 break;
1498 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1499 break;
1500 default:
1501 break;
1502 }
1503}
1504
1505/* make the fullscreen and black surface at the top */
1506static void
1507shell_stack_fullscreen(struct shell_surface *shsurf)
1508{
Alex Wubd3354b2012-04-17 17:20:49 +08001509 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001510 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001511 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001512
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001513 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001514 wl_list_insert(&shell->fullscreen_layer.surface_list,
1515 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001516 weston_surface_damage(surface);
1517
1518 if (!shsurf->fullscreen.black_surface)
1519 shsurf->fullscreen.black_surface =
1520 create_black_surface(surface->compositor,
1521 surface,
1522 output->x, output->y,
1523 output->current->width,
1524 output->current->height);
1525
1526 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001527 wl_list_insert(&surface->layer_link,
1528 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001529 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001530}
1531
1532static void
1533shell_map_fullscreen(struct shell_surface *shsurf)
1534{
Alex Wu4539b082012-03-01 12:57:46 +08001535 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001536 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001537}
1538
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001539static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001540shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001541 struct wl_resource *resource,
1542 uint32_t method,
1543 uint32_t framerate,
1544 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001545{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001546 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001547 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001548
1549 if (output_resource)
1550 shsurf->output = output_resource->data;
1551 else
1552 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001553
Alex Wu4539b082012-03-01 12:57:46 +08001554 shsurf->fullscreen_output = shsurf->output;
1555 shsurf->fullscreen.type = method;
1556 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001557 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001558
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001559 shsurf->client->send_configure(shsurf->surface, 0,
1560 shsurf->output->current->width,
1561 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001562}
1563
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001564static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001565popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001566 struct wl_surface *surface,
1567 wl_fixed_t x,
1568 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001569{
Daniel Stone37816df2012-05-16 18:45:18 +01001570 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001571 struct shell_surface *priv =
1572 container_of(grab, struct shell_surface, popup.grab);
1573 struct wl_client *client = priv->surface->surface.resource.client;
1574
Pekka Paalanencb108432012-01-19 16:25:40 +02001575 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001576 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001577 grab->focus = surface;
1578 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001579 wl_pointer_set_focus(pointer, NULL,
1580 wl_fixed_from_int(0),
1581 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001582 grab->focus = NULL;
1583 }
1584}
1585
1586static void
Scott Moreau447013d2012-02-18 05:05:29 -07001587popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001588 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001589{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001590 struct wl_resource *resource;
1591
Daniel Stone37816df2012-05-16 18:45:18 +01001592 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001593 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001594 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001595}
1596
1597static void
Scott Moreau447013d2012-02-18 05:05:29 -07001598popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001599 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001600{
1601 struct wl_resource *resource;
1602 struct shell_surface *shsurf =
1603 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001604 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001605 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001606 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001607
Daniel Stone37816df2012-05-16 18:45:18 +01001608 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001609 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001610 display = wl_client_get_display(resource->client);
1611 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001612 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001613 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001614 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001615 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001616 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001617 wl_pointer_end_grab(grab->pointer);
1618 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001619 }
1620
Daniel Stone4dbadb12012-05-30 16:31:51 +01001621 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001622 shsurf->popup.initial_up = 1;
1623}
1624
Scott Moreau447013d2012-02-18 05:05:29 -07001625static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001626 popup_grab_focus,
1627 popup_grab_motion,
1628 popup_grab_button,
1629};
1630
1631static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001632shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001633{
Daniel Stone37816df2012-05-16 18:45:18 +01001634 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001635 struct weston_surface *es = shsurf->surface;
1636 struct weston_surface *parent = shsurf->parent->surface;
1637
1638 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001639 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001640
Pekka Paalanen938269a2012-02-07 14:19:01 +02001641 weston_surface_update_transform(parent);
1642 if (parent->transform.enabled) {
1643 shsurf->popup.parent_transform.matrix =
1644 parent->transform.matrix;
1645 } else {
1646 /* construct x, y translation matrix */
1647 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1648 shsurf->popup.parent_transform.matrix.d[12] =
1649 parent->geometry.x;
1650 shsurf->popup.parent_transform.matrix.d[13] =
1651 parent->geometry.y;
1652 }
1653 wl_list_insert(es->geometry.transformation_list.prev,
1654 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001655 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001656
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001657 shsurf->popup.initial_up = 0;
1658
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001659 /* We don't require the grab to still be active, but if another
1660 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001661 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1662 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001663 } else {
1664 wl_shell_surface_send_popup_done(&shsurf->resource);
1665 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001666}
1667
1668static void
1669shell_surface_set_popup(struct wl_client *client,
1670 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001671 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001672 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001673 struct wl_resource *parent_resource,
1674 int32_t x, int32_t y, uint32_t flags)
1675{
1676 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001677
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001678 shsurf->type = SHELL_SURFACE_POPUP;
1679 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001680 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001681 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001682 shsurf->popup.x = x;
1683 shsurf->popup.y = y;
1684}
1685
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001686static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001687 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001688 shell_surface_move,
1689 shell_surface_resize,
1690 shell_surface_set_toplevel,
1691 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001692 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001693 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001694 shell_surface_set_maximized,
1695 shell_surface_set_title,
1696 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001697};
1698
1699static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001700destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001701{
Daniel Stone37816df2012-05-16 18:45:18 +01001702 if (shsurf->popup.grab.pointer)
1703 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001704
Alex Wubd3354b2012-04-17 17:20:49 +08001705 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1706 shell_surface_is_top_fullscreen(shsurf)) {
1707 weston_output_switch_mode(shsurf->fullscreen_output,
1708 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001709 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001710
Alex Wuaa08e2d2012-03-05 11:01:40 +08001711 if (shsurf->fullscreen.black_surface)
1712 weston_surface_destroy(shsurf->fullscreen.black_surface);
1713
Alex Wubd3354b2012-04-17 17:20:49 +08001714 /* As destroy_resource() use wl_list_for_each_safe(),
1715 * we can always remove the listener.
1716 */
1717 wl_list_remove(&shsurf->surface_destroy_listener.link);
1718 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001719 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001720
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001721 wl_list_remove(&shsurf->link);
1722 free(shsurf);
1723}
1724
1725static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001726shell_destroy_shell_surface(struct wl_resource *resource)
1727{
1728 struct shell_surface *shsurf = resource->data;
1729
1730 destroy_shell_surface(shsurf);
1731}
1732
1733static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001734shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001735{
1736 struct shell_surface *shsurf = container_of(listener,
1737 struct shell_surface,
1738 surface_destroy_listener);
1739
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001740 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001741 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001742 } else {
1743 wl_signal_emit(&shsurf->resource.destroy_signal,
1744 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001745 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001746 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001747}
1748
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001749static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001750get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001751{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001752 struct wl_listener *listener;
1753
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001754 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1755 shell_handle_surface_destroy);
1756 if (listener)
1757 return container_of(listener, struct shell_surface,
1758 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001759
1760 return NULL;
1761}
1762
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001763static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001764shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1765
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001766static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001767create_shell_surface(void *shell, struct weston_surface *surface,
1768 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001769{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001770 struct shell_surface *shsurf;
1771
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001772 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001773 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001774 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001775 }
1776
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001777 shsurf = calloc(1, sizeof *shsurf);
1778 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001779 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001780 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001781 }
1782
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001783 surface->configure = shell_surface_configure;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001784 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001785
Tiago Vignattibc052c92012-04-19 16:18:18 +03001786 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001787 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001788 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001789 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001790 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001791 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1792 shsurf->fullscreen.framerate = 0;
1793 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001794 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001795 wl_list_init(&shsurf->fullscreen.transform.link);
1796
Tiago Vignattibc052c92012-04-19 16:18:18 +03001797 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001798 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1799 wl_signal_add(&surface->surface.resource.destroy_signal,
1800 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001801
1802 /* init link so its safe to always remove it in destroy_shell_surface */
1803 wl_list_init(&shsurf->link);
1804
Pekka Paalanen460099f2012-01-20 16:48:25 +02001805 /* empty when not in use */
1806 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001807 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001808
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001809 wl_list_init(&shsurf->workspace_transform.link);
1810
Pekka Paalanen98262232011-12-01 10:42:22 +02001811 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001812 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001813
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001814 shsurf->client = client;
1815
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001816 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001817}
1818
1819static void
1820shell_get_shell_surface(struct wl_client *client,
1821 struct wl_resource *resource,
1822 uint32_t id,
1823 struct wl_resource *surface_resource)
1824{
1825 struct weston_surface *surface = surface_resource->data;
1826 struct desktop_shell *shell = resource->data;
1827 struct shell_surface *shsurf;
1828
1829 if (get_shell_surface(surface)) {
1830 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001831 WL_DISPLAY_ERROR_INVALID_OBJECT,
1832 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001833 return;
1834 }
1835
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001836 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001837 if (!shsurf) {
1838 wl_resource_post_error(surface_resource,
1839 WL_DISPLAY_ERROR_INVALID_OBJECT,
1840 "surface->configure already set");
1841 return;
1842 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001843
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001844 shsurf->resource.destroy = shell_destroy_shell_surface;
1845 shsurf->resource.object.id = id;
1846 shsurf->resource.object.interface = &wl_shell_surface_interface;
1847 shsurf->resource.object.implementation =
1848 (void (**)(void)) &shell_surface_implementation;
1849 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001850
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001851 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001852}
1853
1854static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001855 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001856};
1857
Kristian Høgsberg07937562011-04-12 17:25:42 -04001858static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001859handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001860{
1861 proc->pid = 0;
1862}
1863
1864static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001865launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001866{
1867 if (shell->screensaver.binding)
1868 return;
1869
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001870 if (!shell->screensaver.path)
1871 return;
1872
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001873 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001874 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001875 return;
1876 }
1877
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001878 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001879 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001880 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001881 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001882}
1883
1884static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001885terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001886{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001887 if (shell->screensaver.process.pid == 0)
1888 return;
1889
1890 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001891}
1892
1893static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001894show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001895{
1896 struct wl_list *list;
1897
1898 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001899 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001900 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001901 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001902
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001903 wl_list_remove(&surface->surface->layer_link);
1904 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001905 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001906 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001907}
1908
1909static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001910hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001911{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001912 wl_list_remove(&surface->surface->layer_link);
1913 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001914 surface->surface->output = NULL;
1915}
1916
1917static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001918desktop_shell_set_background(struct wl_client *client,
1919 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001920 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001921 struct wl_resource *surface_resource)
1922{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001923 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001924
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001925 shsurf->next_type = SHELL_SURFACE_BACKGROUND;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001926 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001927
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001928 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001929 surface_resource,
1930 shsurf->output->current->width,
1931 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001932}
1933
1934static void
1935desktop_shell_set_panel(struct wl_client *client,
1936 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001937 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001938 struct wl_resource *surface_resource)
1939{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001940 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001941
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001942 shsurf->next_type = SHELL_SURFACE_PANEL;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001943 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001944
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001945 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001946 surface_resource,
1947 shsurf->output->current->width,
1948 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001949}
1950
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001951static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001952handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001953{
Tiago Vignattibe143262012-04-16 17:31:41 +03001954 struct desktop_shell *shell =
1955 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001956
Martin Minarik6d118362012-06-07 18:01:59 +02001957 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001958 shell->lock_surface = NULL;
1959}
1960
1961static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001962desktop_shell_set_lock_surface(struct wl_client *client,
1963 struct wl_resource *resource,
1964 struct wl_resource *surface_resource)
1965{
Tiago Vignattibe143262012-04-16 17:31:41 +03001966 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001967 struct shell_surface *surface = surface_resource->data;
1968
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001969 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001970
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001971 if (!shell->locked)
1972 return;
1973
Pekka Paalanen98262232011-12-01 10:42:22 +02001974 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001975
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001976 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1977 wl_signal_add(&surface_resource->destroy_signal,
1978 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001979
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001980 shell->lock_surface->next_type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001981}
1982
1983static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001984resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001985{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001986 struct shell_surface *tmp;
1987
1988 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1989 hide_screensaver(shell, tmp);
1990
1991 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001992
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001993 wl_list_remove(&shell->lock_layer.link);
1994 wl_list_insert(&shell->compositor->cursor_layer.link,
1995 &shell->fullscreen_layer.link);
1996 wl_list_insert(&shell->fullscreen_layer.link,
1997 &shell->panel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001998
Jonas Ådahl04769742012-06-13 00:01:24 +02001999 pop_focus_state(shell, get_current_workspace(shell));
2000
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002001 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002002 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002003 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002004 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002005}
2006
2007static void
2008desktop_shell_unlock(struct wl_client *client,
2009 struct wl_resource *resource)
2010{
Tiago Vignattibe143262012-04-16 17:31:41 +03002011 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002012
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002013 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002014
2015 if (shell->locked)
2016 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002017}
2018
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002019static void
2020desktop_shell_set_busy_surface(struct wl_client *client,
2021 struct wl_resource *resource,
2022 struct wl_resource *surface_resource)
2023{
2024 struct desktop_shell *shell = resource->data;
2025
2026 shell->busy_surface = surface_resource->data;
2027}
2028
Kristian Høgsberg75840622011-09-06 13:48:16 -04002029static const struct desktop_shell_interface desktop_shell_implementation = {
2030 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002031 desktop_shell_set_panel,
2032 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002033 desktop_shell_unlock,
2034 desktop_shell_set_busy_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002035};
2036
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002037static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002038get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002039{
2040 struct shell_surface *shsurf;
2041
2042 shsurf = get_shell_surface(surface);
2043 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002044 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002045 return shsurf->type;
2046}
2047
Kristian Høgsberg75840622011-09-06 13:48:16 -04002048static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002049move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002050{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002051 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002052 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002053 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002054
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002055 if (surface == NULL)
2056 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002057
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002058 shsurf = get_shell_surface(surface);
2059 if (shsurf == NULL)
2060 return;
2061
2062 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002063 case SHELL_SURFACE_PANEL:
2064 case SHELL_SURFACE_BACKGROUND:
2065 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02002066 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002067 return;
2068 default:
2069 break;
2070 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04002071
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002072 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002073}
2074
2075static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002076resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002077{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002078 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002079 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002080 uint32_t edges = 0;
2081 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002082 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002083
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002084 if (surface == NULL)
2085 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002086
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002087 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002088 if (!shsurf)
2089 return;
2090
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002091 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002092 case SHELL_SURFACE_PANEL:
2093 case SHELL_SURFACE_BACKGROUND:
2094 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02002095 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002096 return;
2097 default:
2098 break;
2099 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04002100
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002101 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002102 wl_fixed_to_int(seat->pointer->grab_x),
2103 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002104 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002105
Pekka Paalanen60921e52012-01-25 15:55:43 +02002106 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002107 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002108 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002109 edges |= 0;
2110 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002111 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002112
Pekka Paalanen60921e52012-01-25 15:55:43 +02002113 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002114 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002115 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002116 edges |= 0;
2117 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002118 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002119
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002120 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002121}
2122
2123static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002124surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002125 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002126{
Scott Moreau02709af2012-05-22 01:54:10 -06002127 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002128 struct shell_surface *shsurf;
2129 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002130 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002131
2132 if (surface == NULL)
2133 return;
2134
2135 shsurf = get_shell_surface(surface);
2136 if (!shsurf)
2137 return;
2138
2139 switch (shsurf->type) {
2140 case SHELL_SURFACE_BACKGROUND:
2141 case SHELL_SURFACE_SCREENSAVER:
2142 return;
2143 default:
2144 break;
2145 }
2146
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002147 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002148
Scott Moreau02709af2012-05-22 01:54:10 -06002149 if (surface->alpha > 1.0)
2150 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002151 if (surface->alpha < step)
2152 surface->alpha = step;
2153
2154 surface->geometry.dirty = 1;
2155 weston_surface_damage(surface);
2156}
2157
2158static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002159do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002160 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002161{
Daniel Stone37816df2012-05-16 18:45:18 +01002162 struct weston_seat *ws = (struct weston_seat *) seat;
2163 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002164 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002165 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002166
2167 wl_list_for_each(output, &compositor->output_list, link) {
2168 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002169 wl_fixed_to_double(seat->pointer->x),
2170 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002171 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002172 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002173 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002174 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002175 increment = -output->zoom.increment;
2176 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002177 increment = output->zoom.increment *
2178 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002179 else
2180 increment = 0;
2181
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002182 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002183
Scott Moreaue6603982012-06-11 13:07:51 -06002184 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002185 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002186 else if (output->zoom.level > output->zoom.max_level)
2187 output->zoom.level = output->zoom.max_level;
2188 else
2189 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002190
Scott Moreaue6603982012-06-11 13:07:51 -06002191 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002192
Daniel Stone37816df2012-05-16 18:45:18 +01002193 weston_output_update_zoom(output,
2194 seat->pointer->x,
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002195 seat->pointer->y,
Scott Moreaue6603982012-06-11 13:07:51 -06002196 ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002197 }
2198 }
2199}
2200
Scott Moreauccbf29d2012-02-22 14:21:41 -07002201static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002202zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002203 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002204{
2205 do_zoom(seat, time, 0, axis, value);
2206}
2207
2208static void
2209zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2210 void *data)
2211{
2212 do_zoom(seat, time, key, 0, 0);
2213}
2214
2215static void
2216terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2217 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002218{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002219 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002220
Daniel Stone325fc2d2012-05-30 16:31:58 +01002221 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002222}
2223
2224static void
Scott Moreau447013d2012-02-18 05:05:29 -07002225rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002226 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002227{
2228 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002229 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002230 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002231 struct shell_surface *shsurf = rotate->base.shsurf;
2232 struct weston_surface *surface;
2233 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2234
2235 if (!shsurf)
2236 return;
2237
2238 surface = shsurf->surface;
2239
2240 cx = 0.5f * surface->geometry.width;
2241 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002242
Daniel Stone37816df2012-05-16 18:45:18 +01002243 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2244 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002245 r = sqrtf(dx * dx + dy * dy);
2246
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002247 wl_list_remove(&shsurf->rotation.transform.link);
2248 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002249
2250 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002251 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002252 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002253
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002254 weston_matrix_init(&rotate->rotation);
2255 rotate->rotation.d[0] = dx / r;
2256 rotate->rotation.d[4] = -dy / r;
2257 rotate->rotation.d[1] = -rotate->rotation.d[4];
2258 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002259
2260 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002261 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002262 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002263 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002264 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002265
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002266 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002267 &shsurf->surface->geometry.transformation_list,
2268 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002269 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002270 wl_list_init(&shsurf->rotation.transform.link);
2271 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002272 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002273 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002274
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002275 /* We need to adjust the position of the surface
2276 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002277 cposx = surface->geometry.x + cx;
2278 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002279 dposx = rotate->center.x - cposx;
2280 dposy = rotate->center.y - cposy;
2281 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002282 weston_surface_set_position(surface,
2283 surface->geometry.x + dposx,
2284 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002285 }
2286
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002287 /* Repaint implies weston_surface_update_transform(), which
2288 * lazily applies the damage due to rotation update.
2289 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002290 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002291}
2292
2293static void
Scott Moreau447013d2012-02-18 05:05:29 -07002294rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002295 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002296{
2297 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002298 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002299 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002300 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002301 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002302
Daniel Stone4dbadb12012-05-30 16:31:51 +01002303 if (pointer->button_count == 0 &&
2304 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002305 if (shsurf)
2306 weston_matrix_multiply(&shsurf->rotation.rotation,
2307 &rotate->rotation);
2308 shell_grab_finish(&rotate->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002309 wl_pointer_end_grab(pointer);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002310 free(rotate);
2311 }
2312}
2313
Scott Moreau447013d2012-02-18 05:05:29 -07002314static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002315 noop_grab_focus,
2316 rotate_grab_motion,
2317 rotate_grab_button,
2318};
2319
2320static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002321rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002322 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002323{
2324 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002325 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002326 struct shell_surface *surface;
2327 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002328 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002329 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002330
2331 if (base_surface == NULL)
2332 return;
2333
2334 surface = get_shell_surface(base_surface);
2335 if (!surface)
2336 return;
2337
2338 switch (surface->type) {
2339 case SHELL_SURFACE_PANEL:
2340 case SHELL_SURFACE_BACKGROUND:
2341 case SHELL_SURFACE_FULLSCREEN:
2342 case SHELL_SURFACE_SCREENSAVER:
2343 return;
2344 default:
2345 break;
2346 }
2347
Pekka Paalanen460099f2012-01-20 16:48:25 +02002348 rotate = malloc(sizeof *rotate);
2349 if (!rotate)
2350 return;
2351
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002352 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02002353
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002354 weston_surface_to_global_float(surface->surface,
2355 surface->surface->geometry.width / 2,
2356 surface->surface->geometry.height / 2,
2357 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002358
Daniel Stone37816df2012-05-16 18:45:18 +01002359 wl_pointer_start_grab(seat->pointer, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002360
Daniel Stone37816df2012-05-16 18:45:18 +01002361 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2362 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002363 r = sqrtf(dx * dx + dy * dy);
2364 if (r > 20.0f) {
2365 struct weston_matrix inverse;
2366
2367 weston_matrix_init(&inverse);
2368 inverse.d[0] = dx / r;
2369 inverse.d[4] = dy / r;
2370 inverse.d[1] = -inverse.d[4];
2371 inverse.d[5] = inverse.d[0];
2372 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002373
2374 weston_matrix_init(&rotate->rotation);
2375 rotate->rotation.d[0] = dx / r;
2376 rotate->rotation.d[4] = -dy / r;
2377 rotate->rotation.d[1] = -rotate->rotation.d[4];
2378 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002379 } else {
2380 weston_matrix_init(&surface->rotation.rotation);
2381 weston_matrix_init(&rotate->rotation);
2382 }
2383
Daniel Stone37816df2012-05-16 18:45:18 +01002384 wl_pointer_set_focus(seat->pointer, NULL,
2385 wl_fixed_from_int(0),
2386 wl_fixed_from_int(0));
Pekka Paalanen460099f2012-01-20 16:48:25 +02002387}
2388
2389static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002390activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002391 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002392{
Alex Wu21858432012-04-01 20:13:08 +08002393 struct weston_surface *surf, *prev;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002394 struct workspace *ws;
2395 struct weston_layer *ws_layer;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002396
Daniel Stone37816df2012-05-16 18:45:18 +01002397 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002398
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002399 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002400 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002401 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002402 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002403 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002404
Pekka Paalanen77346a62011-11-30 16:26:35 +02002405 case SHELL_SURFACE_SCREENSAVER:
2406 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002407 if (shell->lock_surface)
2408 weston_surface_restack(es,
2409 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002410 break;
Alex Wu4539b082012-03-01 12:57:46 +08002411 case SHELL_SURFACE_FULLSCREEN:
2412 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002413 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002414 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002415 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002416 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002417 ws = get_current_workspace(shell);
2418 ws_layer = &ws->layer;
2419
Alex Wubd3354b2012-04-17 17:20:49 +08002420 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08002421 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
2422 wl_list_for_each_reverse_safe(surf,
2423 prev,
2424 &shell->fullscreen_layer.surface_list,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002425 layer_link) {
Alex Wu21858432012-04-01 20:13:08 +08002426 weston_surface_restack(surf,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002427 &ws_layer->surface_list);
2428 }
Alex Wu21858432012-04-01 20:13:08 +08002429 }
2430
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002431 weston_surface_restack(es, &ws_layer->surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002432 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002433 }
2434}
2435
Alex Wu21858432012-04-01 20:13:08 +08002436/* no-op func for checking black surface */
2437static void
2438black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2439{
2440}
2441
2442static bool
2443is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2444{
2445 if (es->configure == black_surface_configure) {
2446 if (fs_surface)
2447 *fs_surface = (struct weston_surface *)es->private;
2448 return true;
2449 }
2450 return false;
2451}
2452
Kristian Høgsberg75840622011-09-06 13:48:16 -04002453static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002454click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002455 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002456{
Daniel Stone37816df2012-05-16 18:45:18 +01002457 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002458 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002459 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002460 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002461
Daniel Stone37816df2012-05-16 18:45:18 +01002462 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002463 if (!focus)
2464 return;
2465
Alex Wu21858432012-04-01 20:13:08 +08002466 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002467 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002468
Daniel Stone325fc2d2012-05-30 16:31:58 +01002469 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002470 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002471}
2472
2473static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002474lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002475{
Tiago Vignattibe143262012-04-16 17:31:41 +03002476 struct desktop_shell *shell =
2477 container_of(listener, struct desktop_shell, lock_listener);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002478 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002479 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002480
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002481 if (shell->locked) {
2482 wl_list_for_each(output, &shell->compositor->output_list, link)
2483 /* TODO: find a way to jump to other DPMS levels */
2484 if (output->set_dpms)
2485 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002486 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002487 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002488
2489 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002490
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002491 /* Hide all surfaces by removing the fullscreen, panel and
2492 * toplevel layers. This way nothing else can show or receive
2493 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002494
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002495 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002496 wl_list_remove(&shell->fullscreen_layer.link);
2497 wl_list_insert(&shell->compositor->cursor_layer.link,
2498 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002499
Pekka Paalanen77346a62011-11-30 16:26:35 +02002500 launch_screensaver(shell);
2501
2502 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
2503 show_screensaver(shell, shsurf);
2504
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002505 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02002506 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002507 weston_compositor_wake(shell->compositor);
2508 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002509 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002510
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002511 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002512 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002513
Jonas Ådahl04769742012-06-13 00:01:24 +02002514 /* stash keyboard foci in current workspace */
2515 push_focus_state(shell, get_current_workspace(shell));
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002516
2517 /* TODO: disable bindings that should not work while locked. */
2518
2519 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002520}
2521
2522static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002523unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002524{
Tiago Vignattibe143262012-04-16 17:31:41 +03002525 struct desktop_shell *shell =
2526 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002527
Pekka Paalanend81c2162011-11-16 13:47:34 +02002528 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002529 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002530 return;
2531 }
2532
2533 /* If desktop-shell client has gone away, unlock immediately. */
2534 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002535 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002536 return;
2537 }
2538
2539 if (shell->prepare_event_sent)
2540 return;
2541
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002542 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002543 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002544}
2545
2546static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002547center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002548{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002549 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002550 GLfloat x = (mode->width - surface->geometry.width) / 2;
2551 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002552
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002553 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002554}
2555
2556static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002557map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002558 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002559{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002560 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002561 struct shell_surface *shsurf = get_shell_surface(surface);
2562 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002563 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002564 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002565 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002566 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002567
Pekka Paalanen60921e52012-01-25 15:55:43 +02002568 surface->geometry.width = width;
2569 surface->geometry.height = height;
2570 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002571
2572 /* initial positioning, see also configure() */
2573 switch (surface_type) {
2574 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002575 weston_surface_set_position(surface, 10 + random() % 400,
2576 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002577 break;
2578 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002579 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002580 break;
Alex Wu4539b082012-03-01 12:57:46 +08002581 case SHELL_SURFACE_FULLSCREEN:
2582 shell_map_fullscreen(shsurf);
2583 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002584 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002585 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002586 panel_height = get_output_panel_height(shell,surface->output);
2587 weston_surface_set_position(surface, surface->output->x,
2588 surface->output->y + panel_height);
2589 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002590 case SHELL_SURFACE_LOCK:
2591 center_on_output(surface, get_default_output(compositor));
2592 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002593 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002594 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002595 case SHELL_SURFACE_NONE:
2596 weston_surface_set_position(surface,
2597 surface->geometry.x + sx,
2598 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002599 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002600 default:
2601 ;
2602 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002603
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002604 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002605 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002606 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002607 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002608 wl_list_insert(&shell->background_layer.surface_list,
2609 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002610 break;
2611 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002612 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002613 wl_list_insert(&shell->panel_layer.surface_list,
2614 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002615 break;
2616 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002617 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002618 wl_list_insert(&shell->lock_layer.surface_list,
2619 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002620 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002621 break;
2622 case SHELL_SURFACE_SCREENSAVER:
2623 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002624 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002625 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002626 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002627 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002628 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002629 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002630 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002631 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002632 case SHELL_SURFACE_POPUP:
2633 case SHELL_SURFACE_TRANSIENT:
2634 parent = shsurf->parent->surface;
2635 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2636 break;
Alex Wu4539b082012-03-01 12:57:46 +08002637 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002638 case SHELL_SURFACE_NONE:
2639 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002640 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002641 ws = get_current_workspace(shell);
2642 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002643 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002644 }
2645
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002646 if (surface_type != SHELL_SURFACE_NONE) {
2647 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002648 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2649 surface->output = shsurf->output;
2650 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002651
Juan Zhao7bb92f02011-12-15 11:31:51 -05002652 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002653 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002654 if (shsurf->transient.flags ==
2655 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2656 break;
2657 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002658 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002659 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002660 if (!shell->locked) {
2661 wl_list_for_each(seat, &compositor->seat_list, link)
2662 activate(shell, surface, seat);
2663 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002664 break;
2665 default:
2666 break;
2667 }
2668
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002669 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002670 {
2671 switch (shell->win_animation_type) {
2672 case ANIMATION_FADE:
2673 weston_fade_run(surface, NULL, NULL);
2674 break;
2675 case ANIMATION_ZOOM:
2676 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2677 break;
2678 default:
2679 break;
2680 }
2681 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002682}
2683
2684static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002685configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002686 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002687{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002688 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2689 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002690
Pekka Paalanen77346a62011-11-30 16:26:35 +02002691 shsurf = get_shell_surface(surface);
2692 if (shsurf)
2693 surface_type = shsurf->type;
2694
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002695 surface->geometry.x = x;
2696 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002697 surface->geometry.width = width;
2698 surface->geometry.height = height;
2699 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002700
2701 switch (surface_type) {
2702 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002703 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002704 break;
Alex Wu4539b082012-03-01 12:57:46 +08002705 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002706 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002707 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002708 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002709 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002710 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002711 surface->geometry.x = surface->output->x;
2712 surface->geometry.y = surface->output->y +
2713 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002714 break;
Alex Wu4539b082012-03-01 12:57:46 +08002715 case SHELL_SURFACE_TOPLEVEL:
2716 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002717 default:
2718 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002719 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002720
Alex Wu4539b082012-03-01 12:57:46 +08002721 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002722 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002723 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002724
2725 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2726 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002727 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2728 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002729 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002730}
2731
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002732static void
2733shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2734{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002735 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002736 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002737 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002738
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002739 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002740 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002741 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002742 type_changed = 1;
2743 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002744
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002745 if (!weston_surface_is_mapped(es)) {
2746 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002747 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002748 es->geometry.width != es->buffer->width ||
2749 es->geometry.height != es->buffer->height) {
2750 GLfloat from_x, from_y;
2751 GLfloat to_x, to_y;
2752
2753 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2754 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2755 configure(shell, es,
2756 es->geometry.x + to_x - from_x,
2757 es->geometry.y + to_y - from_y,
2758 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002759 }
2760}
2761
Tiago Vignattibe143262012-04-16 17:31:41 +03002762static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002763
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002764static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002765desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002766{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002767 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002768 struct desktop_shell *shell =
2769 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002770
2771 shell->child.process.pid = 0;
2772 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002773
2774 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2775 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002776 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002777 shell->child.deathstamp = time;
2778 shell->child.deathcount = 0;
2779 }
2780
2781 shell->child.deathcount++;
2782 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002783 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002784 return;
2785 }
2786
Martin Minarik6d118362012-06-07 18:01:59 +02002787 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002788 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002789}
2790
2791static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002792launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002793{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002794 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002795
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002796 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002797 &shell->child.process,
2798 shell_exe,
2799 desktop_shell_sigchld);
2800
2801 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002802 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002803 return 0;
2804}
2805
2806static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002807bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2808{
Tiago Vignattibe143262012-04-16 17:31:41 +03002809 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002810
2811 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002812 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002813}
2814
Kristian Høgsberg75840622011-09-06 13:48:16 -04002815static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002816unbind_desktop_shell(struct wl_resource *resource)
2817{
Tiago Vignattibe143262012-04-16 17:31:41 +03002818 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002819
2820 if (shell->locked)
2821 resume_desktop(shell);
2822
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002823 shell->child.desktop_shell = NULL;
2824 shell->prepare_event_sent = false;
2825 free(resource);
2826}
2827
2828static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002829bind_desktop_shell(struct wl_client *client,
2830 void *data, uint32_t version, uint32_t id)
2831{
Tiago Vignattibe143262012-04-16 17:31:41 +03002832 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002833 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002834
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002835 resource = wl_client_add_object(client, &desktop_shell_interface,
2836 &desktop_shell_implementation,
2837 id, shell);
2838
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002839 if (client == shell->child.client) {
2840 resource->destroy = unbind_desktop_shell;
2841 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002842 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002843 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002844
2845 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2846 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002847 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002848}
2849
Pekka Paalanen6e168112011-11-24 11:34:05 +02002850static void
2851screensaver_set_surface(struct wl_client *client,
2852 struct wl_resource *resource,
2853 struct wl_resource *shell_surface_resource,
2854 struct wl_resource *output_resource)
2855{
Tiago Vignattibe143262012-04-16 17:31:41 +03002856 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002857 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002858 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002859
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002860 surface->next_type = SHELL_SURFACE_SCREENSAVER;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002861
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002862 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002863 surface->output = output;
2864 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002865}
2866
2867static const struct screensaver_interface screensaver_implementation = {
2868 screensaver_set_surface
2869};
2870
2871static void
2872unbind_screensaver(struct wl_resource *resource)
2873{
Tiago Vignattibe143262012-04-16 17:31:41 +03002874 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002875
Pekka Paalanen77346a62011-11-30 16:26:35 +02002876 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002877 free(resource);
2878}
2879
2880static void
2881bind_screensaver(struct wl_client *client,
2882 void *data, uint32_t version, uint32_t id)
2883{
Tiago Vignattibe143262012-04-16 17:31:41 +03002884 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002885 struct wl_resource *resource;
2886
2887 resource = wl_client_add_object(client, &screensaver_interface,
2888 &screensaver_implementation,
2889 id, shell);
2890
Pekka Paalanen77346a62011-11-30 16:26:35 +02002891 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002892 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002893 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002894 return;
2895 }
2896
2897 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2898 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002899 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002900}
2901
Kristian Høgsberg07045392012-02-19 18:52:44 -05002902struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002903 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002904 struct weston_surface *current;
2905 struct wl_listener listener;
2906 struct wl_keyboard_grab grab;
2907};
2908
2909static void
2910switcher_next(struct switcher *switcher)
2911{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002912 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002913 struct weston_surface *surface;
2914 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002915 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002916
2917 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002918 switch (get_shell_surface_type(surface)) {
2919 case SHELL_SURFACE_TOPLEVEL:
2920 case SHELL_SURFACE_FULLSCREEN:
2921 case SHELL_SURFACE_MAXIMIZED:
2922 if (first == NULL)
2923 first = surface;
2924 if (prev == switcher->current)
2925 next = surface;
2926 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002927 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002928 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002929 weston_surface_damage(surface);
2930 break;
2931 default:
2932 break;
2933 }
Alex Wu1659daa2012-04-01 20:13:09 +08002934
2935 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002936 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002937 surface->geometry.dirty = 1;
2938 weston_surface_damage(surface);
2939 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002940 }
2941
2942 if (next == NULL)
2943 next = first;
2944
Alex Wu07b26062012-03-12 16:06:01 +08002945 if (next == NULL)
2946 return;
2947
Kristian Høgsberg07045392012-02-19 18:52:44 -05002948 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002949 wl_signal_add(&next->surface.resource.destroy_signal,
2950 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002951
2952 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002953 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002954
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002955 shsurf = get_shell_surface(switcher->current);
2956 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002957 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002958}
2959
2960static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002961switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002962{
2963 struct switcher *switcher =
2964 container_of(listener, struct switcher, listener);
2965
2966 switcher_next(switcher);
2967}
2968
2969static void
Daniel Stone351eb612012-05-31 15:27:47 -04002970switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002971{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002972 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002973 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002974 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002975
2976 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002977 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002978 weston_surface_damage(surface);
2979 }
2980
Alex Wu07b26062012-03-12 16:06:01 +08002981 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01002982 activate(switcher->shell, switcher->current,
2983 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002984 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01002985 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002986 free(switcher);
2987}
2988
2989static void
2990switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002991 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002992{
2993 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01002994 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04002995
Daniel Stonec9785ea2012-05-30 16:31:52 +01002996 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04002997 switcher_next(switcher);
2998}
2999
3000static void
3001switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3002 uint32_t mods_depressed, uint32_t mods_latched,
3003 uint32_t mods_locked, uint32_t group)
3004{
3005 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003006 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003007
Daniel Stone351eb612012-05-31 15:27:47 -04003008 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3009 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003010}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003011
3012static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003013 switcher_key,
3014 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003015};
3016
3017static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003018switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3019 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003020{
Tiago Vignattibe143262012-04-16 17:31:41 +03003021 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003022 struct switcher *switcher;
3023
3024 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003025 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003026 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003027 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003028 wl_list_init(&switcher->listener.link);
3029
3030 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003031 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3032 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003033 switcher_next(switcher);
3034}
3035
Pekka Paalanen3c647232011-12-22 13:43:43 +02003036static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003037backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3038 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003039{
3040 struct weston_compositor *compositor = data;
3041 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003042 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003043
3044 /* TODO: we're limiting to simple use cases, where we assume just
3045 * control on the primary display. We'd have to extend later if we
3046 * ever get support for setting backlights on random desktop LCD
3047 * panels though */
3048 output = get_default_output(compositor);
3049 if (!output)
3050 return;
3051
3052 if (!output->set_backlight)
3053 return;
3054
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003055 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3056 backlight_new = output->backlight_current - 25;
3057 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3058 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003059
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003060 if (backlight_new < 5)
3061 backlight_new = 5;
3062 if (backlight_new > 255)
3063 backlight_new = 255;
3064
3065 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003066 output->set_backlight(output, output->backlight_current);
3067}
3068
3069static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003070debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3071 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003072{
Tiago Vignattibe143262012-04-16 17:31:41 +03003073 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003074 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003075 struct weston_surface *surface;
3076
3077 if (shell->debug_repaint_surface) {
3078 weston_surface_destroy(shell->debug_repaint_surface);
3079 shell->debug_repaint_surface = NULL;
3080 } else {
3081 surface = weston_surface_create(compositor);
3082 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3083 weston_surface_configure(surface, 0, 0, 8192, 8192);
3084 wl_list_insert(&compositor->fade_layer.surface_list,
3085 &surface->layer_link);
3086 weston_surface_assign_output(surface);
3087 pixman_region32_init(&surface->input);
3088
3089 /* Here's the dirty little trick that makes the
3090 * repaint debugging work: we force an
3091 * update_transform first to update dependent state
3092 * and clear the geometry.dirty bit. Then we clear
3093 * the surface damage so it only gets repainted
3094 * piecewise as we repaint other things. */
3095
3096 weston_surface_update_transform(surface);
3097 pixman_region32_fini(&surface->damage);
3098 pixman_region32_init(&surface->damage);
3099 shell->debug_repaint_surface = surface;
3100 }
3101}
3102
3103static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003104force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3105 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003106{
3107 struct wl_client *client;
3108 pid_t pid;
3109 uid_t uid;
3110 gid_t gid;
3111
Daniel Stone325fc2d2012-05-30 16:31:58 +01003112 client = seat->keyboard->focus->resource.client;
3113 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003114
Daniel Stone325fc2d2012-05-30 16:31:58 +01003115 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003116}
3117
3118static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003119workspace_up_binding(struct wl_seat *seat, uint32_t time,
3120 uint32_t key, void *data)
3121{
3122 struct desktop_shell *shell = data;
3123 unsigned int new_index = shell->workspaces.current;
3124
3125 if (new_index != 0)
3126 new_index--;
3127
3128 change_workspace(shell, new_index);
3129}
3130
3131static void
3132workspace_down_binding(struct wl_seat *seat, uint32_t time,
3133 uint32_t key, void *data)
3134{
3135 struct desktop_shell *shell = data;
3136 unsigned int new_index = shell->workspaces.current;
3137
3138 if (new_index < shell->workspaces.num - 1)
3139 new_index++;
3140
3141 change_workspace(shell, new_index);
3142}
3143
3144static void
3145workspace_f_binding(struct wl_seat *seat, uint32_t time,
3146 uint32_t key, void *data)
3147{
3148 struct desktop_shell *shell = data;
3149 unsigned int new_index;
3150
3151 new_index = key - KEY_F1;
3152 if (new_index >= shell->workspaces.num)
3153 new_index = shell->workspaces.num - 1;
3154
3155 change_workspace(shell, new_index);
3156}
3157
3158
3159static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003160shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003161{
Tiago Vignattibe143262012-04-16 17:31:41 +03003162 struct desktop_shell *shell =
3163 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003164 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003165
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003166 if (shell->child.client)
3167 wl_client_destroy(shell->child.client);
3168
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003169 wl_list_remove(&shell->lock_listener.link);
3170 wl_list_remove(&shell->unlock_listener.link);
3171
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003172 wl_array_for_each(ws, &shell->workspaces.array)
3173 workspace_destroy(*ws);
3174 wl_array_release(&shell->workspaces.array);
3175
Pekka Paalanen3c647232011-12-22 13:43:43 +02003176 free(shell->screensaver.path);
3177 free(shell);
3178}
3179
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003180static void
3181shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3182{
3183 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003184 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003185
3186 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003187 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3188 MODIFIER_CTRL | MODIFIER_ALT,
3189 terminate_binding, ec);
3190 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3191 click_to_activate_binding,
3192 shell);
3193 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3194 MODIFIER_SUPER | MODIFIER_ALT,
3195 surface_opacity_binding, NULL);
3196 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3197 MODIFIER_SUPER, zoom_axis_binding,
3198 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003199
3200 /* configurable bindings */
3201 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003202 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3203 zoom_key_binding, NULL);
3204 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3205 zoom_key_binding, NULL);
3206 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3207 shell);
3208 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3209 resize_binding, shell);
3210 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3211 rotate_binding, NULL);
3212 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3213 shell);
3214 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3215 ec);
3216 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3217 backlight_binding, ec);
3218 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3219 ec);
3220 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3221 backlight_binding, ec);
3222 weston_compositor_add_key_binding(ec, KEY_SPACE, mod,
3223 debug_repaint_binding, shell);
3224 weston_compositor_add_key_binding(ec, KEY_K, mod,
3225 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003226 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3227 workspace_up_binding, shell);
3228 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3229 workspace_down_binding, shell);
3230
3231 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3232 if (shell->workspaces.num > 1) {
3233 num_workspace_bindings = shell->workspaces.num;
3234 if (num_workspace_bindings > 6)
3235 num_workspace_bindings = 6;
3236 for (i = 0; i < num_workspace_bindings; i++)
3237 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3238 workspace_f_binding,
3239 shell);
3240 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003241}
3242
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003243int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003244shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003245
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003246WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003247shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003248{
Tiago Vignattibe143262012-04-16 17:31:41 +03003249 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003250 struct workspace **pws;
3251 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003252
3253 shell = malloc(sizeof *shell);
3254 if (shell == NULL)
3255 return -1;
3256
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003257 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003258 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003259
3260 shell->destroy_listener.notify = shell_destroy;
3261 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3262 shell->lock_listener.notify = lock;
3263 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3264 shell->unlock_listener.notify = unlock;
3265 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003266 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003267 ec->shell_interface.create_shell_surface = create_shell_surface;
3268 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003269 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003270 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003271 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003272
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003273 wl_list_init(&shell->backgrounds);
3274 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003275 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003276
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003277 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3278 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003279 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3280 weston_layer_init(&shell->lock_layer, NULL);
3281
3282 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003283
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003284 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003285
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003286 for (i = 0; i < shell->workspaces.num; i++) {
3287 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3288 if (pws == NULL)
3289 return -1;
3290
3291 *pws = workspace_create();
3292 if (*pws == NULL)
3293 return -1;
3294 }
3295 activate_workspace(shell, 0);
3296
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003297 wl_list_init(&shell->workspaces.animation.link);
3298 shell->workspaces.animation.frame = animate_workspace_change_frame;
3299
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003300 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3301 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003302 return -1;
3303
Kristian Høgsberg75840622011-09-06 13:48:16 -04003304 if (wl_display_add_global(ec->wl_display,
3305 &desktop_shell_interface,
3306 shell, bind_desktop_shell) == NULL)
3307 return -1;
3308
Pekka Paalanen6e168112011-11-24 11:34:05 +02003309 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3310 shell, bind_screensaver) == NULL)
3311 return -1;
3312
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003313 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003314 if (launch_desktop_shell_process(shell) != 0)
3315 return -1;
3316
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003317 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003318 if (ec->seat->seat.pointer)
3319 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3320 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003321
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003322 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003323
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003324 return 0;
3325}