blob: 2222970170209ea6250097e790463d69708ab668 [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;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020072 struct wl_listener show_input_panel_listener;
73 struct wl_listener hide_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020074
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050075 struct weston_layer fullscreen_layer;
76 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050077 struct weston_layer background_layer;
78 struct weston_layer lock_layer;
79
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040080 struct wl_listener pointer_focus_listener;
81 struct weston_surface *busy_surface;
82
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020083 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050084 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020085 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020086 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020087
88 unsigned deathcount;
89 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020090 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020091
92 bool locked;
93 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020094
Kristian Høgsberg730c94d2012-06-26 21:44:35 -040095 struct weston_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050096 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010097
Pekka Paalanen77346a62011-11-30 16:26:35 +020098 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +020099 struct wl_array array;
100 unsigned int current;
101 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200102
103 struct weston_animation animation;
104 int anim_dir;
105 uint32_t anim_timestamp;
106 double anim_current;
107 struct workspace *anim_from;
108 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200109 } workspaces;
110
111 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200112 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200113 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200114 struct wl_resource *binding;
115 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500116 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200117 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500118
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200119 struct {
120 struct wl_resource *binding;
121 struct wl_list surfaces;
122 } input_panel;
123
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300124 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800125 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500126 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400127};
128
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500129enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200130 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500131 SHELL_SURFACE_TOPLEVEL,
132 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500133 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800134 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500135 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200136};
137
Scott Moreauff1db4a2012-04-17 19:06:18 -0600138struct ping_timer {
139 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600140 uint32_t serial;
141};
142
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200143struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200144 struct wl_resource resource;
145
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500146 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200147 struct wl_listener surface_destroy_listener;
Kristian Høgsberg8150b192012-06-27 10:22:58 -0400148 struct weston_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300149 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200150
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400151 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400152 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500153 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800154 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800155 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600156 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100157
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500158 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200159 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500160 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200161 } rotation;
162
163 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700164 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500165 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200166 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500167 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100168 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400169 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500170 } popup;
171
Alex Wu4539b082012-03-01 12:57:46 +0800172 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300173 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400174 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300175 } transient;
176
177 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800178 enum wl_shell_surface_fullscreen_method type;
179 struct weston_transform transform; /* matrix from x, y */
180 uint32_t framerate;
181 struct weston_surface *black_surface;
182 } fullscreen;
183
Scott Moreauff1db4a2012-04-17 19:06:18 -0600184 struct ping_timer *ping_timer;
185
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200186 struct weston_transform workspace_transform;
187
Jonas Ådahl04769742012-06-13 00:01:24 +0200188 struct focus_state *focus_state;
189
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500190 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500191 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100192 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400193
194 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200195};
196
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300197struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700198 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300199 struct shell_surface *shsurf;
200 struct wl_listener shsurf_destroy_listener;
201};
202
203struct weston_move_grab {
204 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100205 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500206};
207
Pekka Paalanen460099f2012-01-20 16:48:25 +0200208struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300209 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500210 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200211 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400212 GLfloat x;
213 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200214 } center;
215};
216
Alex Wubd3354b2012-04-17 17:20:49 +0800217static struct shell_surface *
218get_shell_surface(struct weston_surface *surface);
219
220static struct desktop_shell *
221shell_surface_get_shell(struct shell_surface *shsurf);
222
223static bool
224shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
225{
226 struct desktop_shell *shell;
227 struct weston_surface *top_fs_es;
228
229 shell = shell_surface_get_shell(shsurf);
230
231 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
232 return false;
233
234 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
235 struct weston_surface,
236 layer_link);
237 return (shsurf == get_shell_surface(top_fs_es));
238}
239
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500240static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400241destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300242{
243 struct shell_grab *grab;
244
245 grab = container_of(listener, struct shell_grab,
246 shsurf_destroy_listener);
247
248 grab->shsurf = NULL;
249}
250
251static void
252shell_grab_init(struct shell_grab *grab,
253 const struct wl_pointer_grab_interface *interface,
254 struct shell_surface *shsurf)
255{
256 grab->grab.interface = interface;
257 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400258 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
259 wl_signal_add(&shsurf->resource.destroy_signal,
260 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300261
262}
263
264static void
265shell_grab_finish(struct shell_grab *grab)
266{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400267 if (grab->shsurf)
268 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300269}
270
271static void
Alex Wu4539b082012-03-01 12:57:46 +0800272center_on_output(struct weston_surface *surface,
273 struct weston_output *output);
274
Daniel Stone496ca172012-05-30 16:31:42 +0100275static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300276get_modifier(char *modifier)
277{
278 if (!modifier)
279 return MODIFIER_SUPER;
280
281 if (!strcmp("ctrl", modifier))
282 return MODIFIER_CTRL;
283 else if (!strcmp("alt", modifier))
284 return MODIFIER_ALT;
285 else if (!strcmp("super", modifier))
286 return MODIFIER_SUPER;
287 else
288 return MODIFIER_SUPER;
289}
290
Juan Zhaoe10d2792012-04-25 19:09:52 +0800291static enum animation_type
292get_animation_type(char *animation)
293{
294 if (!animation)
295 return ANIMATION_NONE;
296
297 if (!strcmp("zoom", animation))
298 return ANIMATION_ZOOM;
299 else if (!strcmp("fade", animation))
300 return ANIMATION_FADE;
301 else
302 return ANIMATION_NONE;
303}
304
Alex Wu4539b082012-03-01 12:57:46 +0800305static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300306shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200307{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200308 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200309 char *path = NULL;
310 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200311 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300312 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800313 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200314
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400315 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300316 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800317 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200318 { "num-workspaces",
319 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200320 };
321
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400322 struct config_key saver_keys[] = {
323 { "path", CONFIG_KEY_STRING, &path },
324 { "duration", CONFIG_KEY_INTEGER, &duration },
325 };
326
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200327 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400328 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200329 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
330 };
331
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200332 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500333 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200334 free(config_file);
335
Pekka Paalanen7296e792011-12-07 16:22:00 +0200336 shell->screensaver.path = path;
337 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300338 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800339 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200340 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
341}
342
343static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200344focus_state_destroy(struct focus_state *state)
345{
346 wl_list_remove(&state->seat_destroy_listener.link);
347 wl_list_remove(&state->surface_destroy_listener.link);
348 free(state);
349}
350
351static void
352focus_state_seat_destroy(struct wl_listener *listener, void *data)
353{
354 struct focus_state *state = container_of(listener,
355 struct focus_state,
356 seat_destroy_listener);
357
358 wl_list_remove(&state->link);
359 focus_state_destroy(state);
360}
361
362static void
363focus_state_surface_destroy(struct wl_listener *listener, void *data)
364{
365 struct focus_state *state = container_of(listener,
366 struct focus_state,
367 seat_destroy_listener);
368
369 wl_list_remove(&state->link);
370 focus_state_destroy(state);
371}
372
373static struct focus_state *
374focus_state_create(struct weston_seat *seat)
375{
376 struct wl_keyboard *keyboard = seat->seat.keyboard;
377 struct focus_state *state;
378 struct wl_surface *surface;
379 struct shell_surface *shsurf;
380
381 state = malloc(sizeof *state);
382 if (state == NULL)
383 return NULL;
384
385 surface = keyboard->focus;
386 shsurf = get_shell_surface((struct weston_surface *)keyboard->focus);
387 shsurf->focus_state = state;
388
389 state->seat = seat;
390 state->keyboard_focus = shsurf->surface;
391 wl_list_init(&state->link);
392
393 state->seat_destroy_listener.notify = focus_state_seat_destroy;
394 state->surface_destroy_listener.notify = focus_state_surface_destroy;
395 wl_signal_add(&seat->seat.destroy_signal,
396 &state->seat_destroy_listener);
397 wl_signal_add(&surface->resource.destroy_signal,
398 &state->surface_destroy_listener);
399
400 return state;
401}
402
403static void
404pop_focus_state(struct desktop_shell *shell, struct workspace *ws)
405{
406 struct focus_state *state, *next;
407
408 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
409 if (state->keyboard_focus)
410 wl_keyboard_set_focus(state->seat->seat.keyboard,
411 &state->keyboard_focus->surface);
412
413 focus_state_destroy(state);
414 }
415 wl_list_init(&ws->focus_list);
416}
417
418static void
419push_focus_state(struct desktop_shell *shell, struct workspace *ws)
420{
421 struct weston_seat *seat;
422 struct focus_state *state;
423 struct wl_keyboard *keyboard;
424
425 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
426 keyboard = seat->seat.keyboard;
427 if (keyboard && keyboard->focus) {
428 state = focus_state_create(seat);
429 if (state == NULL)
430 return;
431
432 wl_list_insert(&ws->focus_list, &state->link);
433
434 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
435 }
436 }
437}
438
439static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200440workspace_destroy(struct workspace *ws)
441{
Jonas Ådahl04769742012-06-13 00:01:24 +0200442 struct focus_state *state, *next;
443
444 wl_list_for_each_safe(state, next, &ws->focus_list, link)
445 focus_state_destroy(state);
446
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200447 free(ws);
448}
449
Jonas Ådahl04769742012-06-13 00:01:24 +0200450static void
451seat_destroyed(struct wl_listener *listener, void *data)
452{
453 struct weston_seat *seat = data;
454 struct focus_state *state, *next;
455 struct workspace *ws = container_of(listener,
456 struct workspace,
457 seat_destroyed_listener);
458
459 wl_list_for_each_safe(state, next, &ws->focus_list, link)
460 if (state->seat == seat)
461 wl_list_remove(&state->link);
462}
463
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200464static struct workspace *
465workspace_create(void)
466{
467 struct workspace *ws = malloc(sizeof *ws);
468 if (ws == NULL)
469 return NULL;
470
471 weston_layer_init(&ws->layer, NULL);
472
Jonas Ådahl04769742012-06-13 00:01:24 +0200473 wl_list_init(&ws->focus_list);
474 wl_list_init(&ws->seat_destroyed_listener.link);
475 ws->seat_destroyed_listener.notify = seat_destroyed;
476
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200477 return ws;
478}
479
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200480static int
481workspace_is_empty(struct workspace *ws)
482{
483 return wl_list_empty(&ws->layer.surface_list);
484}
485
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200486static struct workspace *
487get_workspace(struct desktop_shell *shell, unsigned int index)
488{
489 struct workspace **pws = shell->workspaces.array.data;
490 pws += index;
491 return *pws;
492}
493
494static struct workspace *
495get_current_workspace(struct desktop_shell *shell)
496{
497 return get_workspace(shell, shell->workspaces.current);
498}
499
500static void
501activate_workspace(struct desktop_shell *shell, unsigned int index)
502{
503 struct workspace *ws;
504
505 ws = get_workspace(shell, index);
506 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
507
508 shell->workspaces.current = index;
509}
510
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200511static unsigned int
512get_output_height(struct weston_output *output)
513{
514 return abs(output->region.extents.y1 - output->region.extents.y2);
515}
516
517static void
518surface_translate(struct weston_surface *surface, double d)
519{
520 struct shell_surface *shsurf = get_shell_surface(surface);
521 struct weston_transform *transform;
522
523 transform = &shsurf->workspace_transform;
524 if (wl_list_empty(&transform->link))
525 wl_list_insert(surface->geometry.transformation_list.prev,
526 &shsurf->workspace_transform.link);
527
528 weston_matrix_init(&shsurf->workspace_transform.matrix);
529 weston_matrix_translate(&shsurf->workspace_transform.matrix,
530 0.0, d, 0.0);
531 surface->geometry.dirty = 1;
532}
533
534static void
535workspace_translate_out(struct workspace *ws, double fraction)
536{
537 struct weston_surface *surface;
538 unsigned int height;
539 double d;
540
541 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
542 height = get_output_height(surface->output);
543 d = height * fraction;
544
545 surface_translate(surface, d);
546 }
547}
548
549static void
550workspace_translate_in(struct workspace *ws, double fraction)
551{
552 struct weston_surface *surface;
553 unsigned int height;
554 double d;
555
556 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
557 height = get_output_height(surface->output);
558
559 if (fraction > 0)
560 d = -(height - height * fraction);
561 else
562 d = height + height * fraction;
563
564 surface_translate(surface, d);
565 }
566}
567
568static void
569workspace_damage_all_surfaces(struct workspace *ws)
570{
571 struct weston_surface *surface;
572
573 wl_list_for_each(surface, &ws->layer.surface_list, layer_link)
574 weston_surface_damage(surface);
575}
576
577static void
578reverse_workspace_change_animation(struct desktop_shell *shell,
579 unsigned int index,
580 struct workspace *from,
581 struct workspace *to)
582{
583 shell->workspaces.current = index;
584
585 shell->workspaces.anim_to = to;
586 shell->workspaces.anim_from = from;
587 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
588 shell->workspaces.anim_timestamp = 0;
589
Jonas Ådahl04769742012-06-13 00:01:24 +0200590 push_focus_state(shell, from);
591 pop_focus_state(shell, to);
592
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200593 workspace_damage_all_surfaces(from);
594 workspace_damage_all_surfaces(to);
595}
596
597static void
598workspace_deactivate_transforms(struct workspace *ws)
599{
600 struct weston_surface *surface;
601 struct shell_surface *shsurf;
602
603 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
604 shsurf = get_shell_surface(surface);
605 wl_list_remove(&shsurf->workspace_transform.link);
606 wl_list_init(&shsurf->workspace_transform.link);
607 shsurf->surface->geometry.dirty = 1;
608 }
609}
610
611static void
612finish_workspace_change_animation(struct desktop_shell *shell,
613 struct workspace *from,
614 struct workspace *to)
615{
616 workspace_damage_all_surfaces(from);
617 workspace_damage_all_surfaces(to);
618
619 wl_list_remove(&shell->workspaces.animation.link);
620 workspace_deactivate_transforms(from);
621 workspace_deactivate_transforms(to);
622 shell->workspaces.anim_to = NULL;
623
624 wl_list_remove(&shell->workspaces.anim_from->layer.link);
625}
626
627static void
628animate_workspace_change_frame(struct weston_animation *animation,
629 struct weston_output *output, uint32_t msecs)
630{
631 struct desktop_shell *shell =
632 container_of(animation, struct desktop_shell,
633 workspaces.animation);
634 struct workspace *from = shell->workspaces.anim_from;
635 struct workspace *to = shell->workspaces.anim_to;
636 uint32_t t;
637 double x, y;
638
639 if (workspace_is_empty(from) && workspace_is_empty(to)) {
640 finish_workspace_change_animation(shell, from, to);
641 return;
642 }
643
644 if (shell->workspaces.anim_timestamp == 0) {
645 if (shell->workspaces.anim_current == 0.0)
646 shell->workspaces.anim_timestamp = msecs;
647 else
648 shell->workspaces.anim_timestamp =
649 msecs -
650 /* Invers of movement function 'y' below. */
651 (asin(1.0 - shell->workspaces.anim_current) *
652 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
653 M_2_PI);
654 }
655
656 t = msecs - shell->workspaces.anim_timestamp;
657
658 /*
659 * x = [0, π/2]
660 * y(x) = sin(x)
661 */
662 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
663 y = sin(x);
664
665 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
666 workspace_damage_all_surfaces(from);
667 workspace_damage_all_surfaces(to);
668
669 workspace_translate_out(from, shell->workspaces.anim_dir * y);
670 workspace_translate_in(to, shell->workspaces.anim_dir * y);
671 shell->workspaces.anim_current = y;
672
673 workspace_damage_all_surfaces(from);
674 workspace_damage_all_surfaces(to);
675 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200676 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200677 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200678}
679
680static void
681animate_workspace_change(struct desktop_shell *shell,
682 unsigned int index,
683 struct workspace *from,
684 struct workspace *to)
685{
686 struct weston_output *output;
687
688 int dir;
689
690 if (index > shell->workspaces.current)
691 dir = -1;
692 else
693 dir = 1;
694
695 shell->workspaces.current = index;
696
697 shell->workspaces.anim_dir = dir;
698 shell->workspaces.anim_from = from;
699 shell->workspaces.anim_to = to;
700 shell->workspaces.anim_current = 0.0;
701 shell->workspaces.anim_timestamp = 0;
702
703 output = container_of(shell->compositor->output_list.next,
704 struct weston_output, link);
705 wl_list_insert(&output->animation_list,
706 &shell->workspaces.animation.link);
707
708 wl_list_insert(&from->layer.link, &to->layer.link);
709
710 workspace_translate_in(to, 0);
711
Jonas Ådahl04769742012-06-13 00:01:24 +0200712 push_focus_state(shell, from);
713 pop_focus_state(shell, to);
714
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200715 workspace_damage_all_surfaces(from);
716 workspace_damage_all_surfaces(to);
717}
718
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200719static void
720change_workspace(struct desktop_shell *shell, unsigned int index)
721{
722 struct workspace *from;
723 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200724
725 if (index == shell->workspaces.current)
726 return;
727
728 /* Don't change workspace when there is any fullscreen surfaces. */
729 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
730 return;
731
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200732 from = get_current_workspace(shell);
733 to = get_workspace(shell, index);
734
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200735 if (shell->workspaces.anim_from == to &&
736 shell->workspaces.anim_to == from) {
737 reverse_workspace_change_animation(shell, index, from, to);
738 return;
739 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200740
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200741 if (shell->workspaces.anim_to != NULL)
742 finish_workspace_change_animation(shell,
743 shell->workspaces.anim_from,
744 shell->workspaces.anim_to);
745
746 if (workspace_is_empty(to) && workspace_is_empty(from)) {
747 shell->workspaces.current = index;
748 wl_list_insert(&from->layer.link, &to->layer.link);
749 wl_list_remove(&from->layer.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200750
751 push_focus_state(shell, from);
752 pop_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200753 }
754 else
755 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200756}
757
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200758static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400759noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100760 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500761{
762 grab->focus = NULL;
763}
764
765static void
Scott Moreau447013d2012-02-18 05:05:29 -0700766move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100767 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500768{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500769 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100770 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300771 struct shell_surface *shsurf = move->base.shsurf;
772 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100773 int dx = wl_fixed_to_int(pointer->x + move->dx);
774 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300775
776 if (!shsurf)
777 return;
778
779 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500780
Daniel Stone103db7f2012-05-08 17:17:55 +0100781 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200782 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -0400783
784 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500785}
786
787static void
Scott Moreau447013d2012-02-18 05:05:29 -0700788move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100789 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500790{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300791 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
792 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100793 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100794 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500795
Daniel Stone4dbadb12012-05-30 16:31:51 +0100796 if (pointer->button_count == 0 &&
797 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300798 shell_grab_finish(shell_grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100799 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500800 free(grab);
801 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500802}
803
Scott Moreau447013d2012-02-18 05:05:29 -0700804static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500805 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500806 move_grab_motion,
807 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500808};
809
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600810static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400811busy_cursor_grab_focus(struct wl_pointer_grab *base,
812 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600813{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400814 struct shell_grab *grab = (struct shell_grab *) base;
815 struct wl_pointer *pointer = base->pointer;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600816
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400817 if (grab->grab.focus != surface) {
818 shell_grab_finish(grab);
819 wl_pointer_end_grab(pointer);
820 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600821 }
822}
823
824static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400825busy_cursor_grab_motion(struct wl_pointer_grab *grab,
826 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600827{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400828}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600829
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400830static void
831busy_cursor_grab_button(struct wl_pointer_grab *grab,
832 uint32_t time, uint32_t button, uint32_t state)
833{
834}
835
836static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
837 busy_cursor_grab_focus,
838 busy_cursor_grab_motion,
839 busy_cursor_grab_button,
840};
841
842static void
843set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
844{
845 struct shell_grab *grab;
846 struct desktop_shell *shell = shsurf->shell;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400847
848 grab = malloc(sizeof *grab);
849 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600850 return;
851
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400852 shell_grab_init(grab, &busy_cursor_grab_interface, shsurf);
853 grab->grab.focus = &shsurf->surface->surface;
854 wl_pointer_start_grab(pointer, &grab->grab);
855 wl_pointer_set_focus(pointer, &shell->busy_surface->surface, 0, 0);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400856}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600857
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400858static void
859end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
860{
861 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
862
863 if (grab->grab.interface == &busy_cursor_grab_interface) {
864 shell_grab_finish(grab);
865 wl_pointer_end_grab(pointer);
866 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600867 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600868}
869
Scott Moreau9521d5e2012-04-19 13:06:17 -0600870static void
871ping_timer_destroy(struct shell_surface *shsurf)
872{
873 if (!shsurf || !shsurf->ping_timer)
874 return;
875
876 if (shsurf->ping_timer->source)
877 wl_event_source_remove(shsurf->ping_timer->source);
878
879 free(shsurf->ping_timer);
880 shsurf->ping_timer = NULL;
881}
882
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400883static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600884ping_timeout_handler(void *data)
885{
886 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400887 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600888
Scott Moreau9521d5e2012-04-19 13:06:17 -0600889 /* Client is not responding */
890 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400891
892 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
893 if (seat->seat.pointer->focus == &shsurf->surface->surface)
894 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600895
896 return 1;
897}
898
899static void
900ping_handler(struct weston_surface *surface, uint32_t serial)
901{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400902 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600903 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400904 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600905
906 if (!shsurf)
907 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400908 if (!shsurf->resource.client)
909 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600910
911 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300912 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600913 if (!shsurf->ping_timer)
914 return;
915
916 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600917 loop = wl_display_get_event_loop(surface->compositor->wl_display);
918 shsurf->ping_timer->source =
919 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
920 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
921
922 wl_shell_surface_send_ping(&shsurf->resource, serial);
923 }
924}
925
926static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400927handle_pointer_focus(struct wl_listener *listener, void *data)
928{
929 struct wl_pointer *pointer = data;
930 struct weston_surface *surface =
931 (struct weston_surface *) pointer->focus;
932 struct weston_compositor *compositor;
933 struct shell_surface *shsurf;
934 uint32_t serial;
935
936 if (!surface)
937 return;
938
939 compositor = surface->compositor;
940 shsurf = get_shell_surface(surface);
941
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +0300942 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400943 set_busy_cursor(shsurf, pointer);
944 } else {
945 serial = wl_display_next_serial(compositor->wl_display);
946 ping_handler(surface, serial);
947 }
948}
949
950static void
Scott Moreauff1db4a2012-04-17 19:06:18 -0600951shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
952 uint32_t serial)
953{
954 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400955 struct desktop_shell *shell = shsurf->shell;
956 struct weston_seat *seat;
957 struct weston_compositor *ec = shsurf->surface->compositor;
958 struct wl_pointer *pointer;
959 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600960
Scott Moreauff1db4a2012-04-17 19:06:18 -0600961 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400962 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600963 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400964 if (was_unresponsive) {
965 /* Received pong from previously unresponsive client */
966 wl_list_for_each(seat, &ec->seat_list, link) {
967 pointer = seat->seat.pointer;
968 if (pointer->focus ==
969 &shell->busy_surface->surface &&
970 pointer->current ==
971 &shsurf->surface->surface)
972 end_busy_cursor(shsurf, pointer);
973 }
974 }
Scott Moreau9521d5e2012-04-19 13:06:17 -0600975 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600976 }
977}
978
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400979static void
980shell_surface_set_title(struct wl_client *client,
981 struct wl_resource *resource, const char *title)
982{
983 struct shell_surface *shsurf = resource->data;
984
985 free(shsurf->title);
986 shsurf->title = strdup(title);
987}
988
989static void
990shell_surface_set_class(struct wl_client *client,
991 struct wl_resource *resource, const char *class)
992{
993 struct shell_surface *shsurf = resource->data;
994
995 free(shsurf->class);
996 shsurf->class = strdup(class);
997}
998
Scott Moreauff1db4a2012-04-17 19:06:18 -0600999static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001000surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001001{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001002 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001003
1004 if (!shsurf)
1005 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001006
1007 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001008 if (!move)
1009 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001010
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001011 shell_grab_init(&move->base, &move_grab_interface, shsurf);
1012
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001013 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +01001014 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001015 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +01001016 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001017
Daniel Stone37816df2012-05-16 18:45:18 +01001018 wl_pointer_start_grab(ws->seat.pointer, &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001019
Daniel Stone37816df2012-05-16 18:45:18 +01001020 wl_pointer_set_focus(ws->seat.pointer, NULL,
1021 wl_fixed_from_int(0),
1022 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001023
1024 return 0;
1025}
1026
1027static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001028shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001029 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001030{
Daniel Stone37816df2012-05-16 18:45:18 +01001031 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001032 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001033
Daniel Stone37816df2012-05-16 18:45:18 +01001034 if (ws->seat.pointer->button_count == 0 ||
1035 ws->seat.pointer->grab_serial != serial ||
1036 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001037 return;
1038
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001039 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001040 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001041}
1042
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001043struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001044 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001045 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001046 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001047};
1048
1049static void
Scott Moreau447013d2012-02-18 05:05:29 -07001050resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001051 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001052{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001053 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001054 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001055 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001056 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +01001057 wl_fixed_t from_x, from_y;
1058 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001059
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001060 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001061 return;
1062
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001063 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001064 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +01001065 &from_x, &from_y);
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001066 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001067 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001068
Daniel Stone103db7f2012-05-08 17:17:55 +01001069 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001070 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001071 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001072 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001073 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001074 }
1075
Daniel Stone103db7f2012-05-08 17:17:55 +01001076 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001077 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001078 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001079 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001080 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001081 }
1082
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001083 shsurf->client->send_configure(shsurf->surface,
1084 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001085}
1086
1087static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001088send_configure(struct weston_surface *surface,
1089 uint32_t edges, int32_t width, int32_t height)
1090{
1091 struct shell_surface *shsurf = get_shell_surface(surface);
1092
1093 wl_shell_surface_send_configure(&shsurf->resource,
1094 edges, width, height);
1095}
1096
1097static const struct weston_shell_client shell_client = {
1098 send_configure
1099};
1100
1101static void
Scott Moreau447013d2012-02-18 05:05:29 -07001102resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001103 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001104{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001105 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001106 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001107 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001108
Daniel Stone4dbadb12012-05-30 16:31:51 +01001109 if (pointer->button_count == 0 &&
1110 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001111 shell_grab_finish(&resize->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001112 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001113 free(grab);
1114 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001115}
1116
Scott Moreau447013d2012-02-18 05:05:29 -07001117static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001118 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001119 resize_grab_motion,
1120 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001121};
1122
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001123static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001124surface_resize(struct shell_surface *shsurf,
1125 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001126{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001127 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001128
Alex Wu4539b082012-03-01 12:57:46 +08001129 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1130 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001131
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001132 if (edges == 0 || edges > 15 ||
1133 (edges & 3) == 3 || (edges & 12) == 12)
1134 return 0;
1135
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001136 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001137 if (!resize)
1138 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001139
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001140 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
1141
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001142 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001143 resize->width = shsurf->surface->geometry.width;
1144 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001145
Daniel Stone37816df2012-05-16 18:45:18 +01001146 wl_pointer_start_grab(ws->seat.pointer, &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001147
Daniel Stone37816df2012-05-16 18:45:18 +01001148 wl_pointer_set_focus(ws->seat.pointer, NULL,
1149 wl_fixed_from_int(0),
1150 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001151
1152 return 0;
1153}
1154
1155static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001156shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001157 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001158 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001159{
Daniel Stone37816df2012-05-16 18:45:18 +01001160 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001161 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001162
Alex Wu4539b082012-03-01 12:57:46 +08001163 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1164 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001165
Daniel Stone37816df2012-05-16 18:45:18 +01001166 if (ws->seat.pointer->button_count == 0 ||
1167 ws->seat.pointer->grab_serial != serial ||
1168 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001169 return;
1170
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001171 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001172 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001173}
1174
Juan Zhao96879df2012-02-07 08:45:41 +08001175static struct weston_output *
1176get_default_output(struct weston_compositor *compositor)
1177{
1178 return container_of(compositor->output_list.next,
1179 struct weston_output, link);
1180}
1181
Alex Wu4539b082012-03-01 12:57:46 +08001182static void
1183shell_unset_fullscreen(struct shell_surface *shsurf)
1184{
1185 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001186 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1187 shell_surface_is_top_fullscreen(shsurf)) {
1188 weston_output_switch_mode(shsurf->fullscreen_output,
1189 shsurf->fullscreen_output->origin);
1190 }
Alex Wu4539b082012-03-01 12:57:46 +08001191 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1192 shsurf->fullscreen.framerate = 0;
1193 wl_list_remove(&shsurf->fullscreen.transform.link);
1194 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001195 if (shsurf->fullscreen.black_surface)
1196 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001197 shsurf->fullscreen.black_surface = NULL;
1198 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001199 weston_surface_set_position(shsurf->surface,
1200 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001201 if (shsurf->saved_rotation_valid) {
1202 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1203 &shsurf->rotation.transform.link);
1204 shsurf->saved_rotation_valid = false;
1205 }
Alex Wu4539b082012-03-01 12:57:46 +08001206}
1207
Pekka Paalanen98262232011-12-01 10:42:22 +02001208static int
1209reset_shell_surface_type(struct shell_surface *surface)
1210{
1211 switch (surface->type) {
1212 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001213 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001214 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001215 case SHELL_SURFACE_MAXIMIZED:
1216 surface->output = get_default_output(surface->surface->compositor);
1217 weston_surface_set_position(surface->surface,
1218 surface->saved_x,
1219 surface->saved_y);
1220 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001221 case SHELL_SURFACE_NONE:
1222 case SHELL_SURFACE_TOPLEVEL:
1223 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001224 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001225 break;
1226 }
1227
1228 surface->type = SHELL_SURFACE_NONE;
1229 return 0;
1230}
1231
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001232static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001233set_surface_type(struct shell_surface *shsurf)
1234{
1235 struct weston_surface *surface = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001236 struct weston_surface *pes = shsurf->parent;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001237
1238 reset_shell_surface_type(shsurf);
1239
1240 shsurf->type = shsurf->next_type;
1241 shsurf->next_type = SHELL_SURFACE_NONE;
1242
1243 switch (shsurf->type) {
1244 case SHELL_SURFACE_TOPLEVEL:
1245 break;
1246 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001247 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001248 pes->geometry.x + shsurf->transient.x,
1249 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001250 break;
1251
1252 case SHELL_SURFACE_MAXIMIZED:
1253 shsurf->saved_x = surface->geometry.x;
1254 shsurf->saved_y = surface->geometry.y;
1255 shsurf->saved_position_valid = true;
1256 break;
1257
1258 case SHELL_SURFACE_FULLSCREEN:
1259 shsurf->saved_x = surface->geometry.x;
1260 shsurf->saved_y = surface->geometry.y;
1261 shsurf->saved_position_valid = true;
1262
1263 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1264 wl_list_remove(&shsurf->rotation.transform.link);
1265 wl_list_init(&shsurf->rotation.transform.link);
1266 shsurf->surface->geometry.dirty = 1;
1267 shsurf->saved_rotation_valid = true;
1268 }
1269 break;
1270
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001271 default:
1272 break;
1273 }
1274}
1275
1276static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001277set_toplevel(struct shell_surface *shsurf)
1278{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001279 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001280}
1281
1282static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001283shell_surface_set_toplevel(struct wl_client *client,
1284 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001285{
Pekka Paalanen98262232011-12-01 10:42:22 +02001286 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001287
Tiago Vignattibc052c92012-04-19 16:18:18 +03001288 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001289}
1290
1291static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001292set_transient(struct shell_surface *shsurf,
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001293 struct weston_surface *parent, int x, int y, uint32_t flags)
Tiago Vignatti491bac12012-05-18 16:37:43 -04001294{
1295 /* assign to parents output */
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001296 shsurf->parent = parent;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001297 shsurf->transient.x = x;
1298 shsurf->transient.y = y;
1299 shsurf->transient.flags = flags;
1300 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1301}
1302
1303static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001304shell_surface_set_transient(struct wl_client *client,
1305 struct wl_resource *resource,
1306 struct wl_resource *parent_resource,
1307 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001308{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001309 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001310 struct weston_surface *parent = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001311
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001312 set_transient(shsurf, parent, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001313}
1314
Tiago Vignattibe143262012-04-16 17:31:41 +03001315static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001316shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001317{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001318 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001319}
1320
1321static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001322get_output_panel_height(struct desktop_shell *shell,
1323 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001324{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001325 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001326 int panel_height = 0;
1327
1328 if (!output)
1329 return 0;
1330
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001331 wl_list_for_each(surface, &shell->panel_layer.surface_list, link) {
1332 if (surface->output == output) {
1333 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001334 break;
1335 }
1336 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001337
Juan Zhao96879df2012-02-07 08:45:41 +08001338 return panel_height;
1339}
1340
1341static void
1342shell_surface_set_maximized(struct wl_client *client,
1343 struct wl_resource *resource,
1344 struct wl_resource *output_resource )
1345{
1346 struct shell_surface *shsurf = resource->data;
1347 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001348 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001349 uint32_t edges = 0, panel_height = 0;
1350
1351 /* get the default output, if the client set it as NULL
1352 check whether the ouput is available */
1353 if (output_resource)
1354 shsurf->output = output_resource->data;
1355 else
1356 shsurf->output = get_default_output(es->compositor);
1357
Tiago Vignattibe143262012-04-16 17:31:41 +03001358 shell = shell_surface_get_shell(shsurf);
1359 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001360 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001361
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001362 shsurf->client->send_configure(shsurf->surface, edges,
1363 es->output->current->width,
1364 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001365
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001366 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001367}
1368
Alex Wu21858432012-04-01 20:13:08 +08001369static void
1370black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1371
Alex Wu4539b082012-03-01 12:57:46 +08001372static struct weston_surface *
1373create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001374 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001375 GLfloat x, GLfloat y, int w, int h)
1376{
1377 struct weston_surface *surface = NULL;
1378
1379 surface = weston_surface_create(ec);
1380 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001381 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001382 return NULL;
1383 }
1384
Alex Wu21858432012-04-01 20:13:08 +08001385 surface->configure = black_surface_configure;
1386 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001387 weston_surface_configure(surface, x, y, w, h);
1388 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1389 return surface;
1390}
1391
1392/* Create black surface and append it to the associated fullscreen surface.
1393 * Handle size dismatch and positioning according to the method. */
1394static void
1395shell_configure_fullscreen(struct shell_surface *shsurf)
1396{
1397 struct weston_output *output = shsurf->fullscreen_output;
1398 struct weston_surface *surface = shsurf->surface;
1399 struct weston_matrix *matrix;
1400 float scale;
1401
1402 center_on_output(surface, output);
1403
1404 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001405 shsurf->fullscreen.black_surface =
1406 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001407 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001408 output->x, output->y,
1409 output->current->width,
1410 output->current->height);
1411
1412 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1413 wl_list_insert(&surface->layer_link,
1414 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001415 shsurf->fullscreen.black_surface->output = output;
1416
1417 switch (shsurf->fullscreen.type) {
1418 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1419 break;
1420 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1421 matrix = &shsurf->fullscreen.transform.matrix;
1422 weston_matrix_init(matrix);
1423 scale = (float)output->current->width/(float)surface->geometry.width;
1424 weston_matrix_scale(matrix, scale, scale, 1);
1425 wl_list_remove(&shsurf->fullscreen.transform.link);
1426 wl_list_insert(surface->geometry.transformation_list.prev,
1427 &shsurf->fullscreen.transform.link);
1428 weston_surface_set_position(surface, output->x, output->y);
1429 break;
1430 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001431 if (shell_surface_is_top_fullscreen(shsurf)) {
1432 struct weston_mode mode = {0,
1433 surface->geometry.width,
1434 surface->geometry.height,
1435 shsurf->fullscreen.framerate};
1436
1437 if (weston_output_switch_mode(output, &mode) == 0) {
1438 weston_surface_configure(shsurf->fullscreen.black_surface,
1439 output->x, output->y,
1440 output->current->width,
1441 output->current->height);
1442 weston_surface_set_position(surface, output->x, output->y);
1443 break;
1444 }
1445 }
Alex Wu4539b082012-03-01 12:57:46 +08001446 break;
1447 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1448 break;
1449 default:
1450 break;
1451 }
1452}
1453
1454/* make the fullscreen and black surface at the top */
1455static void
1456shell_stack_fullscreen(struct shell_surface *shsurf)
1457{
Alex Wubd3354b2012-04-17 17:20:49 +08001458 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001459 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001460 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001461
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001462 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001463 wl_list_insert(&shell->fullscreen_layer.surface_list,
1464 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001465 weston_surface_damage(surface);
1466
1467 if (!shsurf->fullscreen.black_surface)
1468 shsurf->fullscreen.black_surface =
1469 create_black_surface(surface->compositor,
1470 surface,
1471 output->x, output->y,
1472 output->current->width,
1473 output->current->height);
1474
1475 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001476 wl_list_insert(&surface->layer_link,
1477 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001478 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001479}
1480
1481static void
1482shell_map_fullscreen(struct shell_surface *shsurf)
1483{
Alex Wu4539b082012-03-01 12:57:46 +08001484 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001485 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001486}
1487
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001488static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001489shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001490 struct wl_resource *resource,
1491 uint32_t method,
1492 uint32_t framerate,
1493 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001494{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001495 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001496 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001497
1498 if (output_resource)
1499 shsurf->output = output_resource->data;
1500 else
1501 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001502
Alex Wu4539b082012-03-01 12:57:46 +08001503 shsurf->fullscreen_output = shsurf->output;
1504 shsurf->fullscreen.type = method;
1505 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001506 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001507
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001508 shsurf->client->send_configure(shsurf->surface, 0,
1509 shsurf->output->current->width,
1510 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001511}
1512
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001513static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001514popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001515 struct wl_surface *surface,
1516 wl_fixed_t x,
1517 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001518{
Daniel Stone37816df2012-05-16 18:45:18 +01001519 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001520 struct shell_surface *priv =
1521 container_of(grab, struct shell_surface, popup.grab);
1522 struct wl_client *client = priv->surface->surface.resource.client;
1523
Pekka Paalanencb108432012-01-19 16:25:40 +02001524 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001525 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001526 grab->focus = surface;
1527 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001528 wl_pointer_set_focus(pointer, NULL,
1529 wl_fixed_from_int(0),
1530 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001531 grab->focus = NULL;
1532 }
1533}
1534
1535static void
Scott Moreau447013d2012-02-18 05:05:29 -07001536popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001537 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001538{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001539 struct wl_resource *resource;
1540
Daniel Stone37816df2012-05-16 18:45:18 +01001541 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001542 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001543 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001544}
1545
1546static void
Scott Moreau447013d2012-02-18 05:05:29 -07001547popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001548 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001549{
1550 struct wl_resource *resource;
1551 struct shell_surface *shsurf =
1552 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001553 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001554 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001555 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001556
Daniel Stone37816df2012-05-16 18:45:18 +01001557 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001558 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001559 display = wl_client_get_display(resource->client);
1560 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001561 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001562 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001563 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001564 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001565 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001566 wl_pointer_end_grab(grab->pointer);
1567 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001568 }
1569
Daniel Stone4dbadb12012-05-30 16:31:51 +01001570 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001571 shsurf->popup.initial_up = 1;
1572}
1573
Scott Moreau447013d2012-02-18 05:05:29 -07001574static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001575 popup_grab_focus,
1576 popup_grab_motion,
1577 popup_grab_button,
1578};
1579
1580static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001581shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001582{
Daniel Stone37816df2012-05-16 18:45:18 +01001583 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001584 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg8150b192012-06-27 10:22:58 -04001585 struct weston_surface *parent = shsurf->parent;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001586
1587 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001588 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001589
Pekka Paalanen938269a2012-02-07 14:19:01 +02001590 weston_surface_update_transform(parent);
1591 if (parent->transform.enabled) {
1592 shsurf->popup.parent_transform.matrix =
1593 parent->transform.matrix;
1594 } else {
1595 /* construct x, y translation matrix */
1596 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1597 shsurf->popup.parent_transform.matrix.d[12] =
1598 parent->geometry.x;
1599 shsurf->popup.parent_transform.matrix.d[13] =
1600 parent->geometry.y;
1601 }
1602 wl_list_insert(es->geometry.transformation_list.prev,
1603 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001604 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001605
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001606 shsurf->popup.initial_up = 0;
1607
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001608 /* We don't require the grab to still be active, but if another
1609 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001610 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1611 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001612 } else {
1613 wl_shell_surface_send_popup_done(&shsurf->resource);
1614 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001615}
1616
1617static void
1618shell_surface_set_popup(struct wl_client *client,
1619 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001620 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001621 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001622 struct wl_resource *parent_resource,
1623 int32_t x, int32_t y, uint32_t flags)
1624{
1625 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001626
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001627 shsurf->type = SHELL_SURFACE_POPUP;
1628 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001629 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001630 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001631 shsurf->popup.x = x;
1632 shsurf->popup.y = y;
1633}
1634
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001635static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001636 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001637 shell_surface_move,
1638 shell_surface_resize,
1639 shell_surface_set_toplevel,
1640 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001641 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001642 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001643 shell_surface_set_maximized,
1644 shell_surface_set_title,
1645 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001646};
1647
1648static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001649destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001650{
Daniel Stone37816df2012-05-16 18:45:18 +01001651 if (shsurf->popup.grab.pointer)
1652 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001653
Alex Wubd3354b2012-04-17 17:20:49 +08001654 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1655 shell_surface_is_top_fullscreen(shsurf)) {
1656 weston_output_switch_mode(shsurf->fullscreen_output,
1657 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001658 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001659
Alex Wuaa08e2d2012-03-05 11:01:40 +08001660 if (shsurf->fullscreen.black_surface)
1661 weston_surface_destroy(shsurf->fullscreen.black_surface);
1662
Alex Wubd3354b2012-04-17 17:20:49 +08001663 /* As destroy_resource() use wl_list_for_each_safe(),
1664 * we can always remove the listener.
1665 */
1666 wl_list_remove(&shsurf->surface_destroy_listener.link);
1667 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001668 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001669
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001670 wl_list_remove(&shsurf->link);
1671 free(shsurf);
1672}
1673
1674static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001675shell_destroy_shell_surface(struct wl_resource *resource)
1676{
1677 struct shell_surface *shsurf = resource->data;
1678
1679 destroy_shell_surface(shsurf);
1680}
1681
1682static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001683shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001684{
1685 struct shell_surface *shsurf = container_of(listener,
1686 struct shell_surface,
1687 surface_destroy_listener);
1688
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001689 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001690 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001691 } else {
1692 wl_signal_emit(&shsurf->resource.destroy_signal,
1693 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001694 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001695 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001696}
1697
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001698static void
1699shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1700
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001701static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001702get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001703{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001704 if (surface->configure == shell_surface_configure)
1705 return surface->private;
1706 else
1707 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001708}
1709
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001710static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001711create_shell_surface(void *shell, struct weston_surface *surface,
1712 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001713{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001714 struct shell_surface *shsurf;
1715
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001716 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001717 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001718 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001719 }
1720
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001721 shsurf = calloc(1, sizeof *shsurf);
1722 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001723 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001724 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001725 }
1726
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001727 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001728 surface->private = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001729 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001730
Tiago Vignattibc052c92012-04-19 16:18:18 +03001731 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001732 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001733 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001734 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001735 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001736 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1737 shsurf->fullscreen.framerate = 0;
1738 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001739 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001740 wl_list_init(&shsurf->fullscreen.transform.link);
1741
Tiago Vignattibc052c92012-04-19 16:18:18 +03001742 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001743 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1744 wl_signal_add(&surface->surface.resource.destroy_signal,
1745 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001746
1747 /* init link so its safe to always remove it in destroy_shell_surface */
1748 wl_list_init(&shsurf->link);
1749
Pekka Paalanen460099f2012-01-20 16:48:25 +02001750 /* empty when not in use */
1751 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001752 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001753
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001754 wl_list_init(&shsurf->workspace_transform.link);
1755
Pekka Paalanen98262232011-12-01 10:42:22 +02001756 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001757 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001758
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001759 shsurf->client = client;
1760
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001761 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001762}
1763
1764static void
1765shell_get_shell_surface(struct wl_client *client,
1766 struct wl_resource *resource,
1767 uint32_t id,
1768 struct wl_resource *surface_resource)
1769{
1770 struct weston_surface *surface = surface_resource->data;
1771 struct desktop_shell *shell = resource->data;
1772 struct shell_surface *shsurf;
1773
1774 if (get_shell_surface(surface)) {
1775 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001776 WL_DISPLAY_ERROR_INVALID_OBJECT,
1777 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001778 return;
1779 }
1780
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001781 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001782 if (!shsurf) {
1783 wl_resource_post_error(surface_resource,
1784 WL_DISPLAY_ERROR_INVALID_OBJECT,
1785 "surface->configure already set");
1786 return;
1787 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001788
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001789 shsurf->resource.destroy = shell_destroy_shell_surface;
1790 shsurf->resource.object.id = id;
1791 shsurf->resource.object.interface = &wl_shell_surface_interface;
1792 shsurf->resource.object.implementation =
1793 (void (**)(void)) &shell_surface_implementation;
1794 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001795
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001796 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001797}
1798
1799static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001800 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001801};
1802
Kristian Høgsberg07937562011-04-12 17:25:42 -04001803static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001804handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001805{
1806 proc->pid = 0;
1807}
1808
1809static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001810launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001811{
1812 if (shell->screensaver.binding)
1813 return;
1814
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001815 if (!shell->screensaver.path)
1816 return;
1817
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001818 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001819 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001820 return;
1821 }
1822
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001823 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001824 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001825 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001826 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001827}
1828
1829static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001830terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001831{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001832 if (shell->screensaver.process.pid == 0)
1833 return;
1834
1835 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001836}
1837
1838static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001839configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001840{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001841 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001842
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001843 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1844 if (s->output == es->output && s != es) {
1845 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001846 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001847 }
1848 }
1849
1850 weston_surface_configure(es, es->output->x, es->output->y,
1851 es->buffer->width, es->buffer->height);
1852
1853 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001854 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001855 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001856 }
1857}
1858
1859static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001860background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1861{
1862 struct desktop_shell *shell = es->private;
1863
1864 configure_static_surface(es, &shell->background_layer);
1865}
1866
1867static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001868desktop_shell_set_background(struct wl_client *client,
1869 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001870 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001871 struct wl_resource *surface_resource)
1872{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001873 struct desktop_shell *shell = resource->data;
1874 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001875
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001876 if (surface->configure) {
1877 wl_resource_post_error(surface_resource,
1878 WL_DISPLAY_ERROR_INVALID_OBJECT,
1879 "surface role already assigned");
1880 return;
1881 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001882
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001883 surface->configure = background_configure;
1884 surface->private = shell;
1885 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001886 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001887 surface_resource,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001888 surface->output->current->width,
1889 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001890}
1891
1892static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001893panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1894{
1895 struct desktop_shell *shell = es->private;
1896
1897 configure_static_surface(es, &shell->panel_layer);
1898}
1899
1900static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001901desktop_shell_set_panel(struct wl_client *client,
1902 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001903 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001904 struct wl_resource *surface_resource)
1905{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001906 struct desktop_shell *shell = resource->data;
1907 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001908
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001909 if (surface->configure) {
1910 wl_resource_post_error(surface_resource,
1911 WL_DISPLAY_ERROR_INVALID_OBJECT,
1912 "surface role already assigned");
1913 return;
1914 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001915
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001916 surface->configure = panel_configure;
1917 surface->private = shell;
1918 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001919 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001920 surface_resource,
1921 surface->output->current->width,
1922 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001923}
1924
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001925static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001926lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1927{
1928 struct desktop_shell *shell = surface->private;
1929
1930 center_on_output(surface, get_default_output(shell->compositor));
1931
1932 if (!weston_surface_is_mapped(surface)) {
1933 wl_list_insert(&shell->lock_layer.surface_list,
1934 &surface->layer_link);
1935 weston_surface_assign_output(surface);
1936 weston_compositor_wake(shell->compositor);
1937 }
1938}
1939
1940static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001941handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001942{
Tiago Vignattibe143262012-04-16 17:31:41 +03001943 struct desktop_shell *shell =
1944 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001945
Martin Minarik6d118362012-06-07 18:01:59 +02001946 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001947 shell->lock_surface = NULL;
1948}
1949
1950static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001951desktop_shell_set_lock_surface(struct wl_client *client,
1952 struct wl_resource *resource,
1953 struct wl_resource *surface_resource)
1954{
Tiago Vignattibe143262012-04-16 17:31:41 +03001955 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001956 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001957
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001958 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001959
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001960 if (!shell->locked)
1961 return;
1962
Pekka Paalanen98262232011-12-01 10:42:22 +02001963 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001964
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001965 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1966 wl_signal_add(&surface_resource->destroy_signal,
1967 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001968
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001969 surface->configure = lock_surface_configure;
1970 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001971}
1972
1973static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001974resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001975{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001976 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04001977 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001978
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001979 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
1980 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001981
1982 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001983
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001984 wl_list_remove(&shell->lock_layer.link);
1985 wl_list_insert(&shell->compositor->cursor_layer.link,
1986 &shell->fullscreen_layer.link);
1987 wl_list_insert(&shell->fullscreen_layer.link,
1988 &shell->panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04001989 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001990
Jonas Ådahl04769742012-06-13 00:01:24 +02001991 pop_focus_state(shell, get_current_workspace(shell));
1992
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001993 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001994 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001995 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001996 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001997}
1998
1999static void
2000desktop_shell_unlock(struct wl_client *client,
2001 struct wl_resource *resource)
2002{
Tiago Vignattibe143262012-04-16 17:31:41 +03002003 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002004
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002005 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002006
2007 if (shell->locked)
2008 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002009}
2010
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002011static void
2012desktop_shell_set_busy_surface(struct wl_client *client,
2013 struct wl_resource *resource,
2014 struct wl_resource *surface_resource)
2015{
2016 struct desktop_shell *shell = resource->data;
2017
2018 shell->busy_surface = surface_resource->data;
2019}
2020
Kristian Høgsberg75840622011-09-06 13:48:16 -04002021static const struct desktop_shell_interface desktop_shell_implementation = {
2022 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002023 desktop_shell_set_panel,
2024 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002025 desktop_shell_unlock,
2026 desktop_shell_set_busy_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002027};
2028
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002029static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002030get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002031{
2032 struct shell_surface *shsurf;
2033
2034 shsurf = get_shell_surface(surface);
2035 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002036 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002037 return shsurf->type;
2038}
2039
Kristian Høgsberg75840622011-09-06 13:48:16 -04002040static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002041move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002042{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002043 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002044 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002045 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002046
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002047 if (surface == NULL)
2048 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002049
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002050 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002051 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002052 return;
2053
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002054 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002055}
2056
2057static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002058resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002059{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002060 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002061 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002062 uint32_t edges = 0;
2063 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002064 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002065
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002066 if (surface == NULL)
2067 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002068
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002069 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002070 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002071 return;
2072
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002073 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002074 wl_fixed_to_int(seat->pointer->grab_x),
2075 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002076 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002077
Pekka Paalanen60921e52012-01-25 15:55:43 +02002078 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002079 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002080 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002081 edges |= 0;
2082 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002083 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002084
Pekka Paalanen60921e52012-01-25 15:55:43 +02002085 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002086 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002087 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002088 edges |= 0;
2089 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002090 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002091
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002092 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002093}
2094
2095static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002096surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002097 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002098{
Scott Moreau02709af2012-05-22 01:54:10 -06002099 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002100 struct shell_surface *shsurf;
2101 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002102 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002103
2104 if (surface == NULL)
2105 return;
2106
2107 shsurf = get_shell_surface(surface);
2108 if (!shsurf)
2109 return;
2110
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002111 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002112
Scott Moreau02709af2012-05-22 01:54:10 -06002113 if (surface->alpha > 1.0)
2114 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002115 if (surface->alpha < step)
2116 surface->alpha = step;
2117
2118 surface->geometry.dirty = 1;
2119 weston_surface_damage(surface);
2120}
2121
2122static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002123do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002124 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002125{
Daniel Stone37816df2012-05-16 18:45:18 +01002126 struct weston_seat *ws = (struct weston_seat *) seat;
2127 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002128 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002129 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002130
2131 wl_list_for_each(output, &compositor->output_list, link) {
2132 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002133 wl_fixed_to_double(seat->pointer->x),
2134 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002135 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002136 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002137 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002138 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002139 increment = -output->zoom.increment;
2140 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002141 increment = output->zoom.increment *
2142 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002143 else
2144 increment = 0;
2145
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002146 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002147
Scott Moreaue6603982012-06-11 13:07:51 -06002148 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002149 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002150 else if (output->zoom.level > output->zoom.max_level)
2151 output->zoom.level = output->zoom.max_level;
2152 else
2153 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002154
Scott Moreaue6603982012-06-11 13:07:51 -06002155 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002156
Scott Moreau8dacaab2012-06-17 18:10:58 -06002157 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002158 }
2159 }
2160}
2161
Scott Moreauccbf29d2012-02-22 14:21:41 -07002162static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002163zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002164 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002165{
2166 do_zoom(seat, time, 0, axis, value);
2167}
2168
2169static void
2170zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2171 void *data)
2172{
2173 do_zoom(seat, time, key, 0, 0);
2174}
2175
2176static void
2177terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2178 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002179{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002180 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002181
Daniel Stone325fc2d2012-05-30 16:31:58 +01002182 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002183}
2184
2185static void
Scott Moreau447013d2012-02-18 05:05:29 -07002186rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002187 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002188{
2189 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002190 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002191 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002192 struct shell_surface *shsurf = rotate->base.shsurf;
2193 struct weston_surface *surface;
2194 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2195
2196 if (!shsurf)
2197 return;
2198
2199 surface = shsurf->surface;
2200
2201 cx = 0.5f * surface->geometry.width;
2202 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002203
Daniel Stone37816df2012-05-16 18:45:18 +01002204 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2205 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002206 r = sqrtf(dx * dx + dy * dy);
2207
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002208 wl_list_remove(&shsurf->rotation.transform.link);
2209 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002210
2211 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002212 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002213 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002214
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002215 weston_matrix_init(&rotate->rotation);
2216 rotate->rotation.d[0] = dx / r;
2217 rotate->rotation.d[4] = -dy / r;
2218 rotate->rotation.d[1] = -rotate->rotation.d[4];
2219 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002220
2221 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002222 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002223 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002224 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002225 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002226
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002227 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002228 &shsurf->surface->geometry.transformation_list,
2229 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002230 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002231 wl_list_init(&shsurf->rotation.transform.link);
2232 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002233 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002234 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002235
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002236 /* We need to adjust the position of the surface
2237 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002238 cposx = surface->geometry.x + cx;
2239 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002240 dposx = rotate->center.x - cposx;
2241 dposy = rotate->center.y - cposy;
2242 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002243 weston_surface_set_position(surface,
2244 surface->geometry.x + dposx,
2245 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002246 }
2247
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002248 /* Repaint implies weston_surface_update_transform(), which
2249 * lazily applies the damage due to rotation update.
2250 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002251 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002252}
2253
2254static void
Scott Moreau447013d2012-02-18 05:05:29 -07002255rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002256 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002257{
2258 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002259 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002260 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002261 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002262 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002263
Daniel Stone4dbadb12012-05-30 16:31:51 +01002264 if (pointer->button_count == 0 &&
2265 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002266 if (shsurf)
2267 weston_matrix_multiply(&shsurf->rotation.rotation,
2268 &rotate->rotation);
2269 shell_grab_finish(&rotate->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002270 wl_pointer_end_grab(pointer);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002271 free(rotate);
2272 }
2273}
2274
Scott Moreau447013d2012-02-18 05:05:29 -07002275static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002276 noop_grab_focus,
2277 rotate_grab_motion,
2278 rotate_grab_button,
2279};
2280
2281static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002282rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002283 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002284{
2285 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002286 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002287 struct shell_surface *surface;
2288 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002289 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002290 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002291
2292 if (base_surface == NULL)
2293 return;
2294
2295 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002296 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002297 return;
2298
Pekka Paalanen460099f2012-01-20 16:48:25 +02002299 rotate = malloc(sizeof *rotate);
2300 if (!rotate)
2301 return;
2302
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002303 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02002304
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002305 weston_surface_to_global_float(surface->surface,
2306 surface->surface->geometry.width / 2,
2307 surface->surface->geometry.height / 2,
2308 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002309
Daniel Stone37816df2012-05-16 18:45:18 +01002310 wl_pointer_start_grab(seat->pointer, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002311
Daniel Stone37816df2012-05-16 18:45:18 +01002312 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2313 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002314 r = sqrtf(dx * dx + dy * dy);
2315 if (r > 20.0f) {
2316 struct weston_matrix inverse;
2317
2318 weston_matrix_init(&inverse);
2319 inverse.d[0] = dx / r;
2320 inverse.d[4] = dy / r;
2321 inverse.d[1] = -inverse.d[4];
2322 inverse.d[5] = inverse.d[0];
2323 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002324
2325 weston_matrix_init(&rotate->rotation);
2326 rotate->rotation.d[0] = dx / r;
2327 rotate->rotation.d[4] = -dy / r;
2328 rotate->rotation.d[1] = -rotate->rotation.d[4];
2329 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002330 } else {
2331 weston_matrix_init(&surface->rotation.rotation);
2332 weston_matrix_init(&rotate->rotation);
2333 }
2334
Daniel Stone37816df2012-05-16 18:45:18 +01002335 wl_pointer_set_focus(seat->pointer, NULL,
2336 wl_fixed_from_int(0),
2337 wl_fixed_from_int(0));
Pekka Paalanen460099f2012-01-20 16:48:25 +02002338}
2339
2340static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002341lower_fullscreen_layer(struct desktop_shell *shell)
2342{
2343 struct workspace *ws;
2344 struct weston_surface *surface, *prev;
2345
2346 ws = get_current_workspace(shell);
2347 wl_list_for_each_reverse_safe(surface, prev,
2348 &shell->fullscreen_layer.surface_list,
2349 layer_link)
2350 weston_surface_restack(surface, &ws->layer.surface_list);
2351}
2352
2353static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002354activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002355 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002356{
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002357 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002358
Daniel Stone37816df2012-05-16 18:45:18 +01002359 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002360
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002361 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002362 case SHELL_SURFACE_FULLSCREEN:
2363 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002364 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002365 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002366 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002367 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002368 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002369 lower_fullscreen_layer(shell);
2370 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002371 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002372 }
2373}
2374
Alex Wu21858432012-04-01 20:13:08 +08002375/* no-op func for checking black surface */
2376static void
2377black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2378{
2379}
2380
2381static bool
2382is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2383{
2384 if (es->configure == black_surface_configure) {
2385 if (fs_surface)
2386 *fs_surface = (struct weston_surface *)es->private;
2387 return true;
2388 }
2389 return false;
2390}
2391
Kristian Høgsberg75840622011-09-06 13:48:16 -04002392static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002393click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002394 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002395{
Daniel Stone37816df2012-05-16 18:45:18 +01002396 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002397 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002398 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002399 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002400
Daniel Stone37816df2012-05-16 18:45:18 +01002401 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002402 if (!focus)
2403 return;
2404
Alex Wu21858432012-04-01 20:13:08 +08002405 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002406 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002407
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002408 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2409 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002410
Daniel Stone325fc2d2012-05-30 16:31:58 +01002411 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002412 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002413}
2414
2415static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002416lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002417{
Tiago Vignattibe143262012-04-16 17:31:41 +03002418 struct desktop_shell *shell =
2419 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002420 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002421 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002422
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002423 if (shell->locked) {
2424 wl_list_for_each(output, &shell->compositor->output_list, link)
2425 /* TODO: find a way to jump to other DPMS levels */
2426 if (output->set_dpms)
2427 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002428 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002429 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002430
2431 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002432
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002433 /* Hide all surfaces by removing the fullscreen, panel and
2434 * toplevel layers. This way nothing else can show or receive
2435 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002436
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002437 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002438 wl_list_remove(&shell->fullscreen_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002439 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002440 wl_list_insert(&shell->compositor->cursor_layer.link,
2441 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002442
Pekka Paalanen77346a62011-11-30 16:26:35 +02002443 launch_screensaver(shell);
2444
Jonas Ådahl04769742012-06-13 00:01:24 +02002445 /* stash keyboard foci in current workspace */
2446 push_focus_state(shell, get_current_workspace(shell));
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002447
2448 /* TODO: disable bindings that should not work while locked. */
2449
2450 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002451}
2452
2453static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002454unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002455{
Tiago Vignattibe143262012-04-16 17:31:41 +03002456 struct desktop_shell *shell =
2457 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002458
Pekka Paalanend81c2162011-11-16 13:47:34 +02002459 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002460 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002461 return;
2462 }
2463
2464 /* If desktop-shell client has gone away, unlock immediately. */
2465 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002466 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002467 return;
2468 }
2469
2470 if (shell->prepare_event_sent)
2471 return;
2472
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002473 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002474 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002475}
2476
2477static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002478show_input_panels(struct wl_listener *listener, void *data)
2479{
2480 struct desktop_shell *shell =
2481 container_of(listener, struct desktop_shell, show_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002482 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002483
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002484 wl_list_for_each_safe(surface, next,
2485 &shell->input_panel.surfaces, layer_link) {
2486 wl_list_remove(&surface->layer_link);
2487 wl_list_insert(&shell->panel_layer.surface_list,
2488 &surface->layer_link);
2489 weston_surface_assign_output(surface);
2490 weston_surface_damage(surface);
2491 weston_slide_run(surface,
2492 surface->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002493 }
2494}
2495
2496static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002497input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy);
2498
2499static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002500hide_input_panels(struct wl_listener *listener, void *data)
2501{
2502 struct desktop_shell *shell =
2503 container_of(listener, struct desktop_shell, hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002504 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002505
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002506 wl_list_for_each_safe(surface, next,
2507 &shell->panel_layer.surface_list, layer_link)
2508 if (surface->configure == input_panel_configure) {
2509 weston_surface_unmap(surface);
2510 wl_list_insert(&shell->input_panel.surfaces,
2511 &surface->layer_link);
2512 }
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002513}
2514
2515static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002516center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002517{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002518 struct weston_mode *mode = output->current;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002519 GLfloat x = (mode->width - surface->buffer->width) / 2;
2520 GLfloat y = (mode->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002521
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002522 weston_surface_configure(surface, output->x + x, output->y + y,
2523 surface->buffer->width,
2524 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002525}
2526
2527static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002528map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002529 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002530{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002531 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002532 struct shell_surface *shsurf = get_shell_surface(surface);
2533 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002534 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002535 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002536 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002537 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002538
Pekka Paalanen60921e52012-01-25 15:55:43 +02002539 surface->geometry.width = width;
2540 surface->geometry.height = height;
2541 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002542
2543 /* initial positioning, see also configure() */
2544 switch (surface_type) {
2545 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002546 weston_surface_set_position(surface, 10 + random() % 400,
2547 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002548 break;
Alex Wu4539b082012-03-01 12:57:46 +08002549 case SHELL_SURFACE_FULLSCREEN:
2550 shell_map_fullscreen(shsurf);
2551 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002552 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002553 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002554 panel_height = get_output_panel_height(shell,surface->output);
2555 weston_surface_set_position(surface, surface->output->x,
2556 surface->output->y + panel_height);
2557 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002558 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002559 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002560 case SHELL_SURFACE_NONE:
2561 weston_surface_set_position(surface,
2562 surface->geometry.x + sx,
2563 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002564 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002565 default:
2566 ;
2567 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002568
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002569 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002570 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002571 case SHELL_SURFACE_POPUP:
2572 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsberg8150b192012-06-27 10:22:58 -04002573 parent = shsurf->parent;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002574 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2575 break;
Alex Wu4539b082012-03-01 12:57:46 +08002576 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002577 case SHELL_SURFACE_NONE:
2578 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002579 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002580 ws = get_current_workspace(shell);
2581 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002582 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002583 }
2584
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002585 if (surface_type != SHELL_SURFACE_NONE) {
2586 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002587 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2588 surface->output = shsurf->output;
2589 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002590
Juan Zhao7bb92f02011-12-15 11:31:51 -05002591 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002592 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002593 if (shsurf->transient.flags ==
2594 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2595 break;
2596 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002597 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002598 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002599 if (!shell->locked) {
2600 wl_list_for_each(seat, &compositor->seat_list, link)
2601 activate(shell, surface, seat);
2602 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002603 break;
2604 default:
2605 break;
2606 }
2607
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002608 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002609 {
2610 switch (shell->win_animation_type) {
2611 case ANIMATION_FADE:
2612 weston_fade_run(surface, NULL, NULL);
2613 break;
2614 case ANIMATION_ZOOM:
2615 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2616 break;
2617 default:
2618 break;
2619 }
2620 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002621}
2622
2623static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002624configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002625 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002626{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002627 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2628 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002629
Pekka Paalanen77346a62011-11-30 16:26:35 +02002630 shsurf = get_shell_surface(surface);
2631 if (shsurf)
2632 surface_type = shsurf->type;
2633
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002634 surface->geometry.x = x;
2635 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002636 surface->geometry.width = width;
2637 surface->geometry.height = height;
2638 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002639
2640 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002641 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002642 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002643 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002644 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002645 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002646 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002647 surface->geometry.x = surface->output->x;
2648 surface->geometry.y = surface->output->y +
2649 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002650 break;
Alex Wu4539b082012-03-01 12:57:46 +08002651 case SHELL_SURFACE_TOPLEVEL:
2652 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002653 default:
2654 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002655 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002656
Alex Wu4539b082012-03-01 12:57:46 +08002657 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002658 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002659 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002660
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002661 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08002662 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002663 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002664}
2665
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002666static void
2667shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2668{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002669 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002670 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002671 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002672
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002673 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002674 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002675 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002676 type_changed = 1;
2677 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002678
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002679 if (!weston_surface_is_mapped(es)) {
2680 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002681 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002682 es->geometry.width != es->buffer->width ||
2683 es->geometry.height != es->buffer->height) {
2684 GLfloat from_x, from_y;
2685 GLfloat to_x, to_y;
2686
2687 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2688 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2689 configure(shell, es,
2690 es->geometry.x + to_x - from_x,
2691 es->geometry.y + to_y - from_y,
2692 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002693 }
2694}
2695
Tiago Vignattibe143262012-04-16 17:31:41 +03002696static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002697
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002698static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002699desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002700{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002701 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002702 struct desktop_shell *shell =
2703 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002704
2705 shell->child.process.pid = 0;
2706 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002707
2708 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2709 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002710 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002711 shell->child.deathstamp = time;
2712 shell->child.deathcount = 0;
2713 }
2714
2715 shell->child.deathcount++;
2716 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002717 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002718 return;
2719 }
2720
Martin Minarik6d118362012-06-07 18:01:59 +02002721 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002722 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002723}
2724
2725static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002726launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002727{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002728 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002729
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002730 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002731 &shell->child.process,
2732 shell_exe,
2733 desktop_shell_sigchld);
2734
2735 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002736 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002737 return 0;
2738}
2739
2740static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002741bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2742{
Tiago Vignattibe143262012-04-16 17:31:41 +03002743 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002744
2745 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002746 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002747}
2748
Kristian Høgsberg75840622011-09-06 13:48:16 -04002749static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002750unbind_desktop_shell(struct wl_resource *resource)
2751{
Tiago Vignattibe143262012-04-16 17:31:41 +03002752 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002753
2754 if (shell->locked)
2755 resume_desktop(shell);
2756
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002757 shell->child.desktop_shell = NULL;
2758 shell->prepare_event_sent = false;
2759 free(resource);
2760}
2761
2762static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002763bind_desktop_shell(struct wl_client *client,
2764 void *data, uint32_t version, uint32_t id)
2765{
Tiago Vignattibe143262012-04-16 17:31:41 +03002766 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002767 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002768
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002769 resource = wl_client_add_object(client, &desktop_shell_interface,
2770 &desktop_shell_implementation,
2771 id, shell);
2772
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002773 if (client == shell->child.client) {
2774 resource->destroy = unbind_desktop_shell;
2775 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002776 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002777 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002778
2779 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2780 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002781 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002782}
2783
Pekka Paalanen6e168112011-11-24 11:34:05 +02002784static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002785screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2786{
2787 struct desktop_shell *shell = surface->private;
2788
2789 if (!shell->locked)
2790 return;
2791
2792 center_on_output(surface, surface->output);
2793
2794 if (wl_list_empty(&surface->layer_link)) {
2795 wl_list_insert(shell->lock_layer.surface_list.prev,
2796 &surface->layer_link);
2797 weston_surface_assign_output(surface);
2798 shell->compositor->idle_time = shell->screensaver.duration;
2799 weston_compositor_wake(shell->compositor);
2800 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2801 }
2802}
2803
2804static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02002805screensaver_set_surface(struct wl_client *client,
2806 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002807 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02002808 struct wl_resource *output_resource)
2809{
Tiago Vignattibe143262012-04-16 17:31:41 +03002810 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002811 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002812 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002813
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002814 surface->configure = screensaver_configure;
2815 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002816 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002817}
2818
2819static const struct screensaver_interface screensaver_implementation = {
2820 screensaver_set_surface
2821};
2822
2823static void
2824unbind_screensaver(struct wl_resource *resource)
2825{
Tiago Vignattibe143262012-04-16 17:31:41 +03002826 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002827
Pekka Paalanen77346a62011-11-30 16:26:35 +02002828 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002829 free(resource);
2830}
2831
2832static void
2833bind_screensaver(struct wl_client *client,
2834 void *data, uint32_t version, uint32_t id)
2835{
Tiago Vignattibe143262012-04-16 17:31:41 +03002836 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002837 struct wl_resource *resource;
2838
2839 resource = wl_client_add_object(client, &screensaver_interface,
2840 &screensaver_implementation,
2841 id, shell);
2842
Pekka Paalanen77346a62011-11-30 16:26:35 +02002843 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002844 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002845 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002846 return;
2847 }
2848
2849 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2850 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002851 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002852}
2853
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002854static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002855input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2856{
2857 struct weston_mode *mode = surface->output->current;
2858 GLfloat x = (mode->width - surface->buffer->width) / 2;
2859 GLfloat y = mode->height - surface->buffer->height;
2860
2861 /* Don't map the input panel here, wait for
2862 * show_input_panels signal. */
2863
2864 weston_surface_configure(surface,
2865 surface->output->x + x,
2866 surface->output->y + y,
2867 surface->buffer->width,
2868 surface->buffer->height);
2869}
2870
2871static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002872input_panel_set_surface(struct wl_client *client,
2873 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002874 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002875 struct wl_resource *output_resource)
2876{
2877 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002878 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002879 struct weston_output *output = output_resource->data;
2880
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002881 surface->configure = input_panel_configure;
2882 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002883 surface->output = output;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002884 wl_list_insert(shell->input_panel.surfaces.prev, &surface->layer_link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002885}
2886
2887static const struct input_panel_interface input_panel_implementation = {
2888 input_panel_set_surface
2889};
2890
2891static void
2892unbind_input_panel(struct wl_resource *resource)
2893{
2894 struct desktop_shell *shell = resource->data;
2895
2896 shell->input_panel.binding = NULL;
2897 free(resource);
2898}
2899
2900static void
2901bind_input_panel(struct wl_client *client,
2902 void *data, uint32_t version, uint32_t id)
2903{
2904 struct desktop_shell *shell = data;
2905 struct wl_resource *resource;
2906
2907 resource = wl_client_add_object(client, &input_panel_interface,
2908 &input_panel_implementation,
2909 id, shell);
2910
2911 if (shell->input_panel.binding == NULL) {
2912 resource->destroy = unbind_input_panel;
2913 shell->input_panel.binding = resource;
2914 return;
2915 }
2916
2917 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2918 "interface object already bound");
2919 wl_resource_destroy(resource);
2920}
2921
Kristian Høgsberg07045392012-02-19 18:52:44 -05002922struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002923 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002924 struct weston_surface *current;
2925 struct wl_listener listener;
2926 struct wl_keyboard_grab grab;
2927};
2928
2929static void
2930switcher_next(struct switcher *switcher)
2931{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002932 struct weston_surface *surface;
2933 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002934 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002935 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002936
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002937 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002938 switch (get_shell_surface_type(surface)) {
2939 case SHELL_SURFACE_TOPLEVEL:
2940 case SHELL_SURFACE_FULLSCREEN:
2941 case SHELL_SURFACE_MAXIMIZED:
2942 if (first == NULL)
2943 first = surface;
2944 if (prev == switcher->current)
2945 next = surface;
2946 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002947 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002948 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002949 weston_surface_damage(surface);
2950 break;
2951 default:
2952 break;
2953 }
Alex Wu1659daa2012-04-01 20:13:09 +08002954
2955 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002956 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002957 surface->geometry.dirty = 1;
2958 weston_surface_damage(surface);
2959 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002960 }
2961
2962 if (next == NULL)
2963 next = first;
2964
Alex Wu07b26062012-03-12 16:06:01 +08002965 if (next == NULL)
2966 return;
2967
Kristian Høgsberg07045392012-02-19 18:52:44 -05002968 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002969 wl_signal_add(&next->surface.resource.destroy_signal,
2970 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002971
2972 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002973 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002974
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002975 shsurf = get_shell_surface(switcher->current);
2976 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002977 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002978}
2979
2980static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002981switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002982{
2983 struct switcher *switcher =
2984 container_of(listener, struct switcher, listener);
2985
2986 switcher_next(switcher);
2987}
2988
2989static void
Daniel Stone351eb612012-05-31 15:27:47 -04002990switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002991{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002992 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002993 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002994 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002995
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002996 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002997 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002998 weston_surface_damage(surface);
2999 }
3000
Alex Wu07b26062012-03-12 16:06:01 +08003001 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003002 activate(switcher->shell, switcher->current,
3003 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003004 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003005 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003006 free(switcher);
3007}
3008
3009static void
3010switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003011 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003012{
3013 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003014 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003015
Daniel Stonec9785ea2012-05-30 16:31:52 +01003016 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003017 switcher_next(switcher);
3018}
3019
3020static void
3021switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3022 uint32_t mods_depressed, uint32_t mods_latched,
3023 uint32_t mods_locked, uint32_t group)
3024{
3025 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003026 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003027
Daniel Stone351eb612012-05-31 15:27:47 -04003028 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3029 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003030}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003031
3032static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003033 switcher_key,
3034 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003035};
3036
3037static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003038switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3039 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003040{
Tiago Vignattibe143262012-04-16 17:31:41 +03003041 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003042 struct switcher *switcher;
3043
3044 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003045 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003046 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003047 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003048 wl_list_init(&switcher->listener.link);
3049
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003050 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003051 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003052 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3053 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003054 switcher_next(switcher);
3055}
3056
Pekka Paalanen3c647232011-12-22 13:43:43 +02003057static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003058backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3059 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003060{
3061 struct weston_compositor *compositor = data;
3062 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003063 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003064
3065 /* TODO: we're limiting to simple use cases, where we assume just
3066 * control on the primary display. We'd have to extend later if we
3067 * ever get support for setting backlights on random desktop LCD
3068 * panels though */
3069 output = get_default_output(compositor);
3070 if (!output)
3071 return;
3072
3073 if (!output->set_backlight)
3074 return;
3075
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003076 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3077 backlight_new = output->backlight_current - 25;
3078 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3079 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003080
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003081 if (backlight_new < 5)
3082 backlight_new = 5;
3083 if (backlight_new > 255)
3084 backlight_new = 255;
3085
3086 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003087 output->set_backlight(output, output->backlight_current);
3088}
3089
3090static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003091debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3092 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003093{
Tiago Vignattibe143262012-04-16 17:31:41 +03003094 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003095 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003096 struct weston_surface *surface;
3097
3098 if (shell->debug_repaint_surface) {
3099 weston_surface_destroy(shell->debug_repaint_surface);
3100 shell->debug_repaint_surface = NULL;
3101 } else {
3102 surface = weston_surface_create(compositor);
3103 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3104 weston_surface_configure(surface, 0, 0, 8192, 8192);
3105 wl_list_insert(&compositor->fade_layer.surface_list,
3106 &surface->layer_link);
3107 weston_surface_assign_output(surface);
3108 pixman_region32_init(&surface->input);
3109
3110 /* Here's the dirty little trick that makes the
3111 * repaint debugging work: we force an
3112 * update_transform first to update dependent state
3113 * and clear the geometry.dirty bit. Then we clear
3114 * the surface damage so it only gets repainted
3115 * piecewise as we repaint other things. */
3116
3117 weston_surface_update_transform(surface);
3118 pixman_region32_fini(&surface->damage);
3119 pixman_region32_init(&surface->damage);
3120 shell->debug_repaint_surface = surface;
3121 }
3122}
3123
3124static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003125force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3126 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003127{
3128 struct wl_client *client;
3129 pid_t pid;
3130 uid_t uid;
3131 gid_t gid;
3132
Daniel Stone325fc2d2012-05-30 16:31:58 +01003133 client = seat->keyboard->focus->resource.client;
3134 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003135
Daniel Stone325fc2d2012-05-30 16:31:58 +01003136 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003137}
3138
3139static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003140workspace_up_binding(struct wl_seat *seat, uint32_t time,
3141 uint32_t key, void *data)
3142{
3143 struct desktop_shell *shell = data;
3144 unsigned int new_index = shell->workspaces.current;
3145
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003146 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003147 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003148 if (new_index != 0)
3149 new_index--;
3150
3151 change_workspace(shell, new_index);
3152}
3153
3154static void
3155workspace_down_binding(struct wl_seat *seat, uint32_t time,
3156 uint32_t key, void *data)
3157{
3158 struct desktop_shell *shell = data;
3159 unsigned int new_index = shell->workspaces.current;
3160
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003161 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003162 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003163 if (new_index < shell->workspaces.num - 1)
3164 new_index++;
3165
3166 change_workspace(shell, new_index);
3167}
3168
3169static void
3170workspace_f_binding(struct wl_seat *seat, uint32_t time,
3171 uint32_t key, void *data)
3172{
3173 struct desktop_shell *shell = data;
3174 unsigned int new_index;
3175
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003176 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003177 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003178 new_index = key - KEY_F1;
3179 if (new_index >= shell->workspaces.num)
3180 new_index = shell->workspaces.num - 1;
3181
3182 change_workspace(shell, new_index);
3183}
3184
3185
3186static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003187shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003188{
Tiago Vignattibe143262012-04-16 17:31:41 +03003189 struct desktop_shell *shell =
3190 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003191 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003192
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003193 if (shell->child.client)
3194 wl_client_destroy(shell->child.client);
3195
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003196 wl_list_remove(&shell->lock_listener.link);
3197 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003198 wl_list_remove(&shell->show_input_panel_listener.link);
3199 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003200
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003201 wl_array_for_each(ws, &shell->workspaces.array)
3202 workspace_destroy(*ws);
3203 wl_array_release(&shell->workspaces.array);
3204
Pekka Paalanen3c647232011-12-22 13:43:43 +02003205 free(shell->screensaver.path);
3206 free(shell);
3207}
3208
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003209static void
3210shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3211{
3212 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003213 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003214
3215 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003216 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3217 MODIFIER_CTRL | MODIFIER_ALT,
3218 terminate_binding, ec);
3219 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3220 click_to_activate_binding,
3221 shell);
3222 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3223 MODIFIER_SUPER | MODIFIER_ALT,
3224 surface_opacity_binding, NULL);
3225 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3226 MODIFIER_SUPER, zoom_axis_binding,
3227 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003228
3229 /* configurable bindings */
3230 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003231 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3232 zoom_key_binding, NULL);
3233 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3234 zoom_key_binding, NULL);
3235 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3236 shell);
3237 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3238 resize_binding, shell);
3239 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3240 rotate_binding, NULL);
3241 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3242 shell);
3243 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3244 ec);
3245 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3246 backlight_binding, ec);
3247 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3248 ec);
3249 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3250 backlight_binding, ec);
Kristian Høgsberg73694c82012-06-28 14:13:10 -04003251 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003252 debug_repaint_binding, shell);
3253 weston_compositor_add_key_binding(ec, KEY_K, mod,
3254 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003255 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3256 workspace_up_binding, shell);
3257 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3258 workspace_down_binding, shell);
3259
3260 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3261 if (shell->workspaces.num > 1) {
3262 num_workspace_bindings = shell->workspaces.num;
3263 if (num_workspace_bindings > 6)
3264 num_workspace_bindings = 6;
3265 for (i = 0; i < num_workspace_bindings; i++)
3266 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3267 workspace_f_binding,
3268 shell);
3269 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003270}
3271
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003272int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003273shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003274
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003275WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003276shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003277{
Tiago Vignattibe143262012-04-16 17:31:41 +03003278 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003279 struct workspace **pws;
3280 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003281
3282 shell = malloc(sizeof *shell);
3283 if (shell == NULL)
3284 return -1;
3285
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003286 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003287 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003288
3289 shell->destroy_listener.notify = shell_destroy;
3290 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3291 shell->lock_listener.notify = lock;
3292 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3293 shell->unlock_listener.notify = unlock;
3294 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003295 shell->show_input_panel_listener.notify = show_input_panels;
3296 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3297 shell->hide_input_panel_listener.notify = hide_input_panels;
3298 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003299 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003300 ec->shell_interface.create_shell_surface = create_shell_surface;
3301 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003302 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003303 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003304 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003305
Pekka Paalanen77346a62011-11-30 16:26:35 +02003306 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003307 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003308
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003309 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3310 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003311 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3312 weston_layer_init(&shell->lock_layer, NULL);
3313
3314 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003315
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003316 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003317
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003318 for (i = 0; i < shell->workspaces.num; i++) {
3319 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3320 if (pws == NULL)
3321 return -1;
3322
3323 *pws = workspace_create();
3324 if (*pws == NULL)
3325 return -1;
3326 }
3327 activate_workspace(shell, 0);
3328
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003329 wl_list_init(&shell->workspaces.animation.link);
3330 shell->workspaces.animation.frame = animate_workspace_change_frame;
3331
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003332 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3333 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003334 return -1;
3335
Kristian Høgsberg75840622011-09-06 13:48:16 -04003336 if (wl_display_add_global(ec->wl_display,
3337 &desktop_shell_interface,
3338 shell, bind_desktop_shell) == NULL)
3339 return -1;
3340
Pekka Paalanen6e168112011-11-24 11:34:05 +02003341 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3342 shell, bind_screensaver) == NULL)
3343 return -1;
3344
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003345 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3346 shell, bind_input_panel) == NULL)
3347 return -1;
3348
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003349 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003350 if (launch_desktop_shell_process(shell) != 0)
3351 return -1;
3352
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003353 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003354 if (ec->seat->seat.pointer)
3355 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3356 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003357
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003358 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003359
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003360 return 0;
3361}