blob: 1d82e43f882fbba722f4df8129ddff06c67774b4 [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øgsbergb3cca0a2012-01-04 22:19:14 -0500148 struct shell_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;
1236 struct shell_surface *pshsurf = shsurf->parent;
1237 struct weston_surface *pes;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001238
1239 reset_shell_surface_type(shsurf);
1240
1241 shsurf->type = shsurf->next_type;
1242 shsurf->next_type = SHELL_SURFACE_NONE;
1243
1244 switch (shsurf->type) {
1245 case SHELL_SURFACE_TOPLEVEL:
1246 break;
1247 case SHELL_SURFACE_TRANSIENT:
1248 pes = pshsurf->surface;
1249 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001250 pes->geometry.x + shsurf->transient.x,
1251 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001252 break;
1253
1254 case SHELL_SURFACE_MAXIMIZED:
1255 shsurf->saved_x = surface->geometry.x;
1256 shsurf->saved_y = surface->geometry.y;
1257 shsurf->saved_position_valid = true;
1258 break;
1259
1260 case SHELL_SURFACE_FULLSCREEN:
1261 shsurf->saved_x = surface->geometry.x;
1262 shsurf->saved_y = surface->geometry.y;
1263 shsurf->saved_position_valid = true;
1264
1265 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1266 wl_list_remove(&shsurf->rotation.transform.link);
1267 wl_list_init(&shsurf->rotation.transform.link);
1268 shsurf->surface->geometry.dirty = 1;
1269 shsurf->saved_rotation_valid = true;
1270 }
1271 break;
1272
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001273 default:
1274 break;
1275 }
1276}
1277
1278static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001279set_toplevel(struct shell_surface *shsurf)
1280{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001281 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001282}
1283
1284static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001285shell_surface_set_toplevel(struct wl_client *client,
1286 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001287{
Pekka Paalanen98262232011-12-01 10:42:22 +02001288 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001289
Tiago Vignattibc052c92012-04-19 16:18:18 +03001290 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001291}
1292
1293static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001294set_transient(struct shell_surface *shsurf,
1295 struct shell_surface *pshsurf, int x, int y, uint32_t flags)
1296{
1297 /* assign to parents output */
1298 shsurf->parent = pshsurf;
1299 shsurf->transient.x = x;
1300 shsurf->transient.y = y;
1301 shsurf->transient.flags = flags;
1302 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1303}
1304
1305static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001306shell_surface_set_transient(struct wl_client *client,
1307 struct wl_resource *resource,
1308 struct wl_resource *parent_resource,
1309 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001310{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001311 struct shell_surface *shsurf = resource->data;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001312 struct shell_surface *pshsurf = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001313
Tiago Vignatti491bac12012-05-18 16:37:43 -04001314 set_transient(shsurf, pshsurf, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001315}
1316
Tiago Vignattibe143262012-04-16 17:31:41 +03001317static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001318shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001319{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001320 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001321}
1322
1323static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001324get_output_panel_height(struct desktop_shell *shell,
1325 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001326{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001327 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001328 int panel_height = 0;
1329
1330 if (!output)
1331 return 0;
1332
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001333 wl_list_for_each(surface, &shell->panel_layer.surface_list, link) {
1334 if (surface->output == output) {
1335 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001336 break;
1337 }
1338 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001339
Juan Zhao96879df2012-02-07 08:45:41 +08001340 return panel_height;
1341}
1342
1343static void
1344shell_surface_set_maximized(struct wl_client *client,
1345 struct wl_resource *resource,
1346 struct wl_resource *output_resource )
1347{
1348 struct shell_surface *shsurf = resource->data;
1349 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001350 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001351 uint32_t edges = 0, panel_height = 0;
1352
1353 /* get the default output, if the client set it as NULL
1354 check whether the ouput is available */
1355 if (output_resource)
1356 shsurf->output = output_resource->data;
1357 else
1358 shsurf->output = get_default_output(es->compositor);
1359
Tiago Vignattibe143262012-04-16 17:31:41 +03001360 shell = shell_surface_get_shell(shsurf);
1361 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001362 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001363
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001364 shsurf->client->send_configure(shsurf->surface, edges,
1365 es->output->current->width,
1366 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001367
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001368 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001369}
1370
Alex Wu21858432012-04-01 20:13:08 +08001371static void
1372black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1373
Alex Wu4539b082012-03-01 12:57:46 +08001374static struct weston_surface *
1375create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001376 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001377 GLfloat x, GLfloat y, int w, int h)
1378{
1379 struct weston_surface *surface = NULL;
1380
1381 surface = weston_surface_create(ec);
1382 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001383 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001384 return NULL;
1385 }
1386
Alex Wu21858432012-04-01 20:13:08 +08001387 surface->configure = black_surface_configure;
1388 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001389 weston_surface_configure(surface, x, y, w, h);
1390 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1391 return surface;
1392}
1393
1394/* Create black surface and append it to the associated fullscreen surface.
1395 * Handle size dismatch and positioning according to the method. */
1396static void
1397shell_configure_fullscreen(struct shell_surface *shsurf)
1398{
1399 struct weston_output *output = shsurf->fullscreen_output;
1400 struct weston_surface *surface = shsurf->surface;
1401 struct weston_matrix *matrix;
1402 float scale;
1403
1404 center_on_output(surface, output);
1405
1406 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001407 shsurf->fullscreen.black_surface =
1408 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001409 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001410 output->x, output->y,
1411 output->current->width,
1412 output->current->height);
1413
1414 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1415 wl_list_insert(&surface->layer_link,
1416 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001417 shsurf->fullscreen.black_surface->output = output;
1418
1419 switch (shsurf->fullscreen.type) {
1420 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1421 break;
1422 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1423 matrix = &shsurf->fullscreen.transform.matrix;
1424 weston_matrix_init(matrix);
1425 scale = (float)output->current->width/(float)surface->geometry.width;
1426 weston_matrix_scale(matrix, scale, scale, 1);
1427 wl_list_remove(&shsurf->fullscreen.transform.link);
1428 wl_list_insert(surface->geometry.transformation_list.prev,
1429 &shsurf->fullscreen.transform.link);
1430 weston_surface_set_position(surface, output->x, output->y);
1431 break;
1432 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001433 if (shell_surface_is_top_fullscreen(shsurf)) {
1434 struct weston_mode mode = {0,
1435 surface->geometry.width,
1436 surface->geometry.height,
1437 shsurf->fullscreen.framerate};
1438
1439 if (weston_output_switch_mode(output, &mode) == 0) {
1440 weston_surface_configure(shsurf->fullscreen.black_surface,
1441 output->x, output->y,
1442 output->current->width,
1443 output->current->height);
1444 weston_surface_set_position(surface, output->x, output->y);
1445 break;
1446 }
1447 }
Alex Wu4539b082012-03-01 12:57:46 +08001448 break;
1449 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1450 break;
1451 default:
1452 break;
1453 }
1454}
1455
1456/* make the fullscreen and black surface at the top */
1457static void
1458shell_stack_fullscreen(struct shell_surface *shsurf)
1459{
Alex Wubd3354b2012-04-17 17:20:49 +08001460 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001461 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001462 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001463
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001464 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001465 wl_list_insert(&shell->fullscreen_layer.surface_list,
1466 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001467 weston_surface_damage(surface);
1468
1469 if (!shsurf->fullscreen.black_surface)
1470 shsurf->fullscreen.black_surface =
1471 create_black_surface(surface->compositor,
1472 surface,
1473 output->x, output->y,
1474 output->current->width,
1475 output->current->height);
1476
1477 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001478 wl_list_insert(&surface->layer_link,
1479 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001480 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001481}
1482
1483static void
1484shell_map_fullscreen(struct shell_surface *shsurf)
1485{
Alex Wu4539b082012-03-01 12:57:46 +08001486 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001487 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001488}
1489
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001490static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001491shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001492 struct wl_resource *resource,
1493 uint32_t method,
1494 uint32_t framerate,
1495 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001496{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001497 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001498 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001499
1500 if (output_resource)
1501 shsurf->output = output_resource->data;
1502 else
1503 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001504
Alex Wu4539b082012-03-01 12:57:46 +08001505 shsurf->fullscreen_output = shsurf->output;
1506 shsurf->fullscreen.type = method;
1507 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001508 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001509
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001510 shsurf->client->send_configure(shsurf->surface, 0,
1511 shsurf->output->current->width,
1512 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001513}
1514
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001515static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001516popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001517 struct wl_surface *surface,
1518 wl_fixed_t x,
1519 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001520{
Daniel Stone37816df2012-05-16 18:45:18 +01001521 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001522 struct shell_surface *priv =
1523 container_of(grab, struct shell_surface, popup.grab);
1524 struct wl_client *client = priv->surface->surface.resource.client;
1525
Pekka Paalanencb108432012-01-19 16:25:40 +02001526 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001527 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001528 grab->focus = surface;
1529 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001530 wl_pointer_set_focus(pointer, NULL,
1531 wl_fixed_from_int(0),
1532 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001533 grab->focus = NULL;
1534 }
1535}
1536
1537static void
Scott Moreau447013d2012-02-18 05:05:29 -07001538popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001539 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001540{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001541 struct wl_resource *resource;
1542
Daniel Stone37816df2012-05-16 18:45:18 +01001543 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001544 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001545 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001546}
1547
1548static void
Scott Moreau447013d2012-02-18 05:05:29 -07001549popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001550 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001551{
1552 struct wl_resource *resource;
1553 struct shell_surface *shsurf =
1554 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001555 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001556 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001557 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001558
Daniel Stone37816df2012-05-16 18:45:18 +01001559 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001560 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001561 display = wl_client_get_display(resource->client);
1562 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001563 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001564 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001565 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001566 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001567 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001568 wl_pointer_end_grab(grab->pointer);
1569 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001570 }
1571
Daniel Stone4dbadb12012-05-30 16:31:51 +01001572 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001573 shsurf->popup.initial_up = 1;
1574}
1575
Scott Moreau447013d2012-02-18 05:05:29 -07001576static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001577 popup_grab_focus,
1578 popup_grab_motion,
1579 popup_grab_button,
1580};
1581
1582static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001583shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001584{
Daniel Stone37816df2012-05-16 18:45:18 +01001585 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001586 struct weston_surface *es = shsurf->surface;
1587 struct weston_surface *parent = shsurf->parent->surface;
1588
1589 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001590 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001591
Pekka Paalanen938269a2012-02-07 14:19:01 +02001592 weston_surface_update_transform(parent);
1593 if (parent->transform.enabled) {
1594 shsurf->popup.parent_transform.matrix =
1595 parent->transform.matrix;
1596 } else {
1597 /* construct x, y translation matrix */
1598 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1599 shsurf->popup.parent_transform.matrix.d[12] =
1600 parent->geometry.x;
1601 shsurf->popup.parent_transform.matrix.d[13] =
1602 parent->geometry.y;
1603 }
1604 wl_list_insert(es->geometry.transformation_list.prev,
1605 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001606 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001607
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001608 shsurf->popup.initial_up = 0;
1609
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001610 /* We don't require the grab to still be active, but if another
1611 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001612 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1613 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001614 } else {
1615 wl_shell_surface_send_popup_done(&shsurf->resource);
1616 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001617}
1618
1619static void
1620shell_surface_set_popup(struct wl_client *client,
1621 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001622 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001623 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001624 struct wl_resource *parent_resource,
1625 int32_t x, int32_t y, uint32_t flags)
1626{
1627 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001628
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001629 shsurf->type = SHELL_SURFACE_POPUP;
1630 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001631 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001632 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001633 shsurf->popup.x = x;
1634 shsurf->popup.y = y;
1635}
1636
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001637static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001638 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001639 shell_surface_move,
1640 shell_surface_resize,
1641 shell_surface_set_toplevel,
1642 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001643 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001644 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001645 shell_surface_set_maximized,
1646 shell_surface_set_title,
1647 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001648};
1649
1650static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001651destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001652{
Daniel Stone37816df2012-05-16 18:45:18 +01001653 if (shsurf->popup.grab.pointer)
1654 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001655
Alex Wubd3354b2012-04-17 17:20:49 +08001656 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1657 shell_surface_is_top_fullscreen(shsurf)) {
1658 weston_output_switch_mode(shsurf->fullscreen_output,
1659 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001660 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001661
Alex Wuaa08e2d2012-03-05 11:01:40 +08001662 if (shsurf->fullscreen.black_surface)
1663 weston_surface_destroy(shsurf->fullscreen.black_surface);
1664
Alex Wubd3354b2012-04-17 17:20:49 +08001665 /* As destroy_resource() use wl_list_for_each_safe(),
1666 * we can always remove the listener.
1667 */
1668 wl_list_remove(&shsurf->surface_destroy_listener.link);
1669 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001670 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001671
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001672 wl_list_remove(&shsurf->link);
1673 free(shsurf);
1674}
1675
1676static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001677shell_destroy_shell_surface(struct wl_resource *resource)
1678{
1679 struct shell_surface *shsurf = resource->data;
1680
1681 destroy_shell_surface(shsurf);
1682}
1683
1684static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001685shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001686{
1687 struct shell_surface *shsurf = container_of(listener,
1688 struct shell_surface,
1689 surface_destroy_listener);
1690
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001691 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001692 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001693 } else {
1694 wl_signal_emit(&shsurf->resource.destroy_signal,
1695 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001696 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001697 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001698}
1699
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001700static void
1701shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1702
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001703static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001704get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001705{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001706 if (surface->configure == shell_surface_configure)
1707 return surface->private;
1708 else
1709 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001710}
1711
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001712static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001713create_shell_surface(void *shell, struct weston_surface *surface,
1714 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001715{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001716 struct shell_surface *shsurf;
1717
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001718 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001719 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001720 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001721 }
1722
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001723 shsurf = calloc(1, sizeof *shsurf);
1724 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001725 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001726 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001727 }
1728
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001729 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001730 surface->private = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001731 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001732
Tiago Vignattibc052c92012-04-19 16:18:18 +03001733 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001734 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001735 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001736 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001737 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001738 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1739 shsurf->fullscreen.framerate = 0;
1740 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001741 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001742 wl_list_init(&shsurf->fullscreen.transform.link);
1743
Tiago Vignattibc052c92012-04-19 16:18:18 +03001744 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001745 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1746 wl_signal_add(&surface->surface.resource.destroy_signal,
1747 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001748
1749 /* init link so its safe to always remove it in destroy_shell_surface */
1750 wl_list_init(&shsurf->link);
1751
Pekka Paalanen460099f2012-01-20 16:48:25 +02001752 /* empty when not in use */
1753 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001754 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001755
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001756 wl_list_init(&shsurf->workspace_transform.link);
1757
Pekka Paalanen98262232011-12-01 10:42:22 +02001758 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001759 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001760
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001761 shsurf->client = client;
1762
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001763 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001764}
1765
1766static void
1767shell_get_shell_surface(struct wl_client *client,
1768 struct wl_resource *resource,
1769 uint32_t id,
1770 struct wl_resource *surface_resource)
1771{
1772 struct weston_surface *surface = surface_resource->data;
1773 struct desktop_shell *shell = resource->data;
1774 struct shell_surface *shsurf;
1775
1776 if (get_shell_surface(surface)) {
1777 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001778 WL_DISPLAY_ERROR_INVALID_OBJECT,
1779 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001780 return;
1781 }
1782
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001783 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001784 if (!shsurf) {
1785 wl_resource_post_error(surface_resource,
1786 WL_DISPLAY_ERROR_INVALID_OBJECT,
1787 "surface->configure already set");
1788 return;
1789 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001790
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001791 shsurf->resource.destroy = shell_destroy_shell_surface;
1792 shsurf->resource.object.id = id;
1793 shsurf->resource.object.interface = &wl_shell_surface_interface;
1794 shsurf->resource.object.implementation =
1795 (void (**)(void)) &shell_surface_implementation;
1796 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001797
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001798 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001799}
1800
1801static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001802 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001803};
1804
Kristian Høgsberg07937562011-04-12 17:25:42 -04001805static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001806handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001807{
1808 proc->pid = 0;
1809}
1810
1811static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001812launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001813{
1814 if (shell->screensaver.binding)
1815 return;
1816
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001817 if (!shell->screensaver.path)
1818 return;
1819
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001820 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001821 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001822 return;
1823 }
1824
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001825 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001826 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001827 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001828 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001829}
1830
1831static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001832terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001833{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001834 if (shell->screensaver.process.pid == 0)
1835 return;
1836
1837 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001838}
1839
1840static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001841configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001842{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001843 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001844
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001845 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1846 if (s->output == es->output && s != es) {
1847 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001848 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001849 }
1850 }
1851
1852 weston_surface_configure(es, es->output->x, es->output->y,
1853 es->buffer->width, es->buffer->height);
1854
1855 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001856 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001857 weston_surface_assign_output(es);
1858 }
1859}
1860
1861static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001862background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1863{
1864 struct desktop_shell *shell = es->private;
1865
1866 configure_static_surface(es, &shell->background_layer);
1867}
1868
1869static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001870desktop_shell_set_background(struct wl_client *client,
1871 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001872 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001873 struct wl_resource *surface_resource)
1874{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001875 struct desktop_shell *shell = resource->data;
1876 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001877
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001878 if (surface->configure) {
1879 wl_resource_post_error(surface_resource,
1880 WL_DISPLAY_ERROR_INVALID_OBJECT,
1881 "surface role already assigned");
1882 return;
1883 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001884
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001885 surface->configure = background_configure;
1886 surface->private = shell;
1887 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001888 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001889 surface_resource,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001890 surface->output->current->width,
1891 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001892}
1893
1894static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001895panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1896{
1897 struct desktop_shell *shell = es->private;
1898
1899 configure_static_surface(es, &shell->panel_layer);
1900}
1901
1902static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001903desktop_shell_set_panel(struct wl_client *client,
1904 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001905 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001906 struct wl_resource *surface_resource)
1907{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001908 struct desktop_shell *shell = resource->data;
1909 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001910
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001911 if (surface->configure) {
1912 wl_resource_post_error(surface_resource,
1913 WL_DISPLAY_ERROR_INVALID_OBJECT,
1914 "surface role already assigned");
1915 return;
1916 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001917
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001918 surface->configure = panel_configure;
1919 surface->private = shell;
1920 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001921 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001922 surface_resource,
1923 surface->output->current->width,
1924 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001925}
1926
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001927static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001928lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1929{
1930 struct desktop_shell *shell = surface->private;
1931
1932 center_on_output(surface, get_default_output(shell->compositor));
1933
1934 if (!weston_surface_is_mapped(surface)) {
1935 wl_list_insert(&shell->lock_layer.surface_list,
1936 &surface->layer_link);
1937 weston_surface_assign_output(surface);
1938 weston_compositor_wake(shell->compositor);
1939 }
1940}
1941
1942static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001943handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001944{
Tiago Vignattibe143262012-04-16 17:31:41 +03001945 struct desktop_shell *shell =
1946 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001947
Martin Minarik6d118362012-06-07 18:01:59 +02001948 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001949 shell->lock_surface = NULL;
1950}
1951
1952static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001953desktop_shell_set_lock_surface(struct wl_client *client,
1954 struct wl_resource *resource,
1955 struct wl_resource *surface_resource)
1956{
Tiago Vignattibe143262012-04-16 17:31:41 +03001957 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001958 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001959
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001960 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001961
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001962 if (!shell->locked)
1963 return;
1964
Pekka Paalanen98262232011-12-01 10:42:22 +02001965 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001966
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001967 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1968 wl_signal_add(&surface_resource->destroy_signal,
1969 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001970
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001971 surface->configure = lock_surface_configure;
1972 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001973}
1974
1975static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001976resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001977{
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001978 struct weston_surface *surface;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04001979 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001980
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04001981 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
1982 weston_surface_unmap(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001983
1984 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001985
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001986 wl_list_remove(&shell->lock_layer.link);
1987 wl_list_insert(&shell->compositor->cursor_layer.link,
1988 &shell->fullscreen_layer.link);
1989 wl_list_insert(&shell->fullscreen_layer.link,
1990 &shell->panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04001991 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001992
Jonas Ådahl04769742012-06-13 00:01:24 +02001993 pop_focus_state(shell, get_current_workspace(shell));
1994
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001995 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001996 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001997 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001998 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001999}
2000
2001static void
2002desktop_shell_unlock(struct wl_client *client,
2003 struct wl_resource *resource)
2004{
Tiago Vignattibe143262012-04-16 17:31:41 +03002005 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002006
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002007 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002008
2009 if (shell->locked)
2010 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002011}
2012
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002013static void
2014desktop_shell_set_busy_surface(struct wl_client *client,
2015 struct wl_resource *resource,
2016 struct wl_resource *surface_resource)
2017{
2018 struct desktop_shell *shell = resource->data;
2019
2020 shell->busy_surface = surface_resource->data;
2021}
2022
Kristian Høgsberg75840622011-09-06 13:48:16 -04002023static const struct desktop_shell_interface desktop_shell_implementation = {
2024 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002025 desktop_shell_set_panel,
2026 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002027 desktop_shell_unlock,
2028 desktop_shell_set_busy_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002029};
2030
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002031static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002032get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002033{
2034 struct shell_surface *shsurf;
2035
2036 shsurf = get_shell_surface(surface);
2037 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002038 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002039 return shsurf->type;
2040}
2041
Kristian Høgsberg75840622011-09-06 13:48:16 -04002042static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002043move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002044{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002045 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002046 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002047 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002048
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002049 if (surface == NULL)
2050 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002051
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002052 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002053 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002054 return;
2055
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002056 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002057}
2058
2059static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002060resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002061{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002062 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002063 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002064 uint32_t edges = 0;
2065 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002066 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002067
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002068 if (surface == NULL)
2069 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002070
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002071 shsurf = get_shell_surface(surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002072 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002073 return;
2074
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002075 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002076 wl_fixed_to_int(seat->pointer->grab_x),
2077 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002078 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002079
Pekka Paalanen60921e52012-01-25 15:55:43 +02002080 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002081 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002082 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002083 edges |= 0;
2084 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002085 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002086
Pekka Paalanen60921e52012-01-25 15:55:43 +02002087 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002088 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002089 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002090 edges |= 0;
2091 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002092 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002093
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002094 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002095}
2096
2097static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002098surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002099 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002100{
Scott Moreau02709af2012-05-22 01:54:10 -06002101 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002102 struct shell_surface *shsurf;
2103 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002104 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002105
2106 if (surface == NULL)
2107 return;
2108
2109 shsurf = get_shell_surface(surface);
2110 if (!shsurf)
2111 return;
2112
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002113 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002114
Scott Moreau02709af2012-05-22 01:54:10 -06002115 if (surface->alpha > 1.0)
2116 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002117 if (surface->alpha < step)
2118 surface->alpha = step;
2119
2120 surface->geometry.dirty = 1;
2121 weston_surface_damage(surface);
2122}
2123
2124static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002125do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002126 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002127{
Daniel Stone37816df2012-05-16 18:45:18 +01002128 struct weston_seat *ws = (struct weston_seat *) seat;
2129 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002130 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002131 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002132
2133 wl_list_for_each(output, &compositor->output_list, link) {
2134 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002135 wl_fixed_to_double(seat->pointer->x),
2136 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002137 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002138 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002139 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002140 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002141 increment = -output->zoom.increment;
2142 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002143 increment = output->zoom.increment *
2144 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002145 else
2146 increment = 0;
2147
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002148 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002149
Scott Moreaue6603982012-06-11 13:07:51 -06002150 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002151 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002152 else if (output->zoom.level > output->zoom.max_level)
2153 output->zoom.level = output->zoom.max_level;
2154 else
2155 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002156
Scott Moreaue6603982012-06-11 13:07:51 -06002157 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002158
Scott Moreau8dacaab2012-06-17 18:10:58 -06002159 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002160 }
2161 }
2162}
2163
Scott Moreauccbf29d2012-02-22 14:21:41 -07002164static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002165zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002166 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002167{
2168 do_zoom(seat, time, 0, axis, value);
2169}
2170
2171static void
2172zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2173 void *data)
2174{
2175 do_zoom(seat, time, key, 0, 0);
2176}
2177
2178static void
2179terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2180 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002181{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002182 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002183
Daniel Stone325fc2d2012-05-30 16:31:58 +01002184 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002185}
2186
2187static void
Scott Moreau447013d2012-02-18 05:05:29 -07002188rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002189 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002190{
2191 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002192 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002193 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002194 struct shell_surface *shsurf = rotate->base.shsurf;
2195 struct weston_surface *surface;
2196 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2197
2198 if (!shsurf)
2199 return;
2200
2201 surface = shsurf->surface;
2202
2203 cx = 0.5f * surface->geometry.width;
2204 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002205
Daniel Stone37816df2012-05-16 18:45:18 +01002206 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2207 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002208 r = sqrtf(dx * dx + dy * dy);
2209
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002210 wl_list_remove(&shsurf->rotation.transform.link);
2211 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002212
2213 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002214 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002215 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002216
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002217 weston_matrix_init(&rotate->rotation);
2218 rotate->rotation.d[0] = dx / r;
2219 rotate->rotation.d[4] = -dy / r;
2220 rotate->rotation.d[1] = -rotate->rotation.d[4];
2221 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002222
2223 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002224 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002225 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002226 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002227 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002228
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002229 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002230 &shsurf->surface->geometry.transformation_list,
2231 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002232 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002233 wl_list_init(&shsurf->rotation.transform.link);
2234 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002235 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002236 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002237
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002238 /* We need to adjust the position of the surface
2239 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002240 cposx = surface->geometry.x + cx;
2241 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002242 dposx = rotate->center.x - cposx;
2243 dposy = rotate->center.y - cposy;
2244 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002245 weston_surface_set_position(surface,
2246 surface->geometry.x + dposx,
2247 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002248 }
2249
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002250 /* Repaint implies weston_surface_update_transform(), which
2251 * lazily applies the damage due to rotation update.
2252 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002253 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002254}
2255
2256static void
Scott Moreau447013d2012-02-18 05:05:29 -07002257rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002258 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002259{
2260 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002261 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002262 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002263 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002264 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002265
Daniel Stone4dbadb12012-05-30 16:31:51 +01002266 if (pointer->button_count == 0 &&
2267 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002268 if (shsurf)
2269 weston_matrix_multiply(&shsurf->rotation.rotation,
2270 &rotate->rotation);
2271 shell_grab_finish(&rotate->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002272 wl_pointer_end_grab(pointer);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002273 free(rotate);
2274 }
2275}
2276
Scott Moreau447013d2012-02-18 05:05:29 -07002277static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002278 noop_grab_focus,
2279 rotate_grab_motion,
2280 rotate_grab_button,
2281};
2282
2283static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002284rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002285 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002286{
2287 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002288 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002289 struct shell_surface *surface;
2290 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002291 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002292 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002293
2294 if (base_surface == NULL)
2295 return;
2296
2297 surface = get_shell_surface(base_surface);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002298 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002299 return;
2300
Pekka Paalanen460099f2012-01-20 16:48:25 +02002301 rotate = malloc(sizeof *rotate);
2302 if (!rotate)
2303 return;
2304
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002305 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02002306
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002307 weston_surface_to_global_float(surface->surface,
2308 surface->surface->geometry.width / 2,
2309 surface->surface->geometry.height / 2,
2310 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002311
Daniel Stone37816df2012-05-16 18:45:18 +01002312 wl_pointer_start_grab(seat->pointer, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002313
Daniel Stone37816df2012-05-16 18:45:18 +01002314 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2315 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002316 r = sqrtf(dx * dx + dy * dy);
2317 if (r > 20.0f) {
2318 struct weston_matrix inverse;
2319
2320 weston_matrix_init(&inverse);
2321 inverse.d[0] = dx / r;
2322 inverse.d[4] = dy / r;
2323 inverse.d[1] = -inverse.d[4];
2324 inverse.d[5] = inverse.d[0];
2325 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002326
2327 weston_matrix_init(&rotate->rotation);
2328 rotate->rotation.d[0] = dx / r;
2329 rotate->rotation.d[4] = -dy / r;
2330 rotate->rotation.d[1] = -rotate->rotation.d[4];
2331 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002332 } else {
2333 weston_matrix_init(&surface->rotation.rotation);
2334 weston_matrix_init(&rotate->rotation);
2335 }
2336
Daniel Stone37816df2012-05-16 18:45:18 +01002337 wl_pointer_set_focus(seat->pointer, NULL,
2338 wl_fixed_from_int(0),
2339 wl_fixed_from_int(0));
Pekka Paalanen460099f2012-01-20 16:48:25 +02002340}
2341
2342static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002343lower_fullscreen_layer(struct desktop_shell *shell)
2344{
2345 struct workspace *ws;
2346 struct weston_surface *surface, *prev;
2347
2348 ws = get_current_workspace(shell);
2349 wl_list_for_each_reverse_safe(surface, prev,
2350 &shell->fullscreen_layer.surface_list,
2351 layer_link)
2352 weston_surface_restack(surface, &ws->layer.surface_list);
2353}
2354
2355static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002356activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002357 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002358{
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002359 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002360
Daniel Stone37816df2012-05-16 18:45:18 +01002361 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002362
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002363 switch (get_shell_surface_type(es)) {
Alex Wu4539b082012-03-01 12:57:46 +08002364 case SHELL_SURFACE_FULLSCREEN:
2365 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002366 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002367 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002368 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002369 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002370 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002371 lower_fullscreen_layer(shell);
2372 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002373 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002374 }
2375}
2376
Alex Wu21858432012-04-01 20:13:08 +08002377/* no-op func for checking black surface */
2378static void
2379black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2380{
2381}
2382
2383static bool
2384is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2385{
2386 if (es->configure == black_surface_configure) {
2387 if (fs_surface)
2388 *fs_surface = (struct weston_surface *)es->private;
2389 return true;
2390 }
2391 return false;
2392}
2393
Kristian Høgsberg75840622011-09-06 13:48:16 -04002394static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002395click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002396 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002397{
Daniel Stone37816df2012-05-16 18:45:18 +01002398 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002399 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002400 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002401 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002402
Daniel Stone37816df2012-05-16 18:45:18 +01002403 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002404 if (!focus)
2405 return;
2406
Alex Wu21858432012-04-01 20:13:08 +08002407 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002408 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002409
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002410 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2411 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002412
Daniel Stone325fc2d2012-05-30 16:31:58 +01002413 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002414 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002415}
2416
2417static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002418lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002419{
Tiago Vignattibe143262012-04-16 17:31:41 +03002420 struct desktop_shell *shell =
2421 container_of(listener, struct desktop_shell, lock_listener);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002422 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002423 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002424
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002425 if (shell->locked) {
2426 wl_list_for_each(output, &shell->compositor->output_list, link)
2427 /* TODO: find a way to jump to other DPMS levels */
2428 if (output->set_dpms)
2429 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002430 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002431 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002432
2433 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002434
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002435 /* Hide all surfaces by removing the fullscreen, panel and
2436 * toplevel layers. This way nothing else can show or receive
2437 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002438
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002439 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002440 wl_list_remove(&shell->fullscreen_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002441 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002442 wl_list_insert(&shell->compositor->cursor_layer.link,
2443 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002444
Pekka Paalanen77346a62011-11-30 16:26:35 +02002445 launch_screensaver(shell);
2446
Jonas Ådahl04769742012-06-13 00:01:24 +02002447 /* stash keyboard foci in current workspace */
2448 push_focus_state(shell, get_current_workspace(shell));
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002449
2450 /* TODO: disable bindings that should not work while locked. */
2451
2452 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002453}
2454
2455static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002456unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002457{
Tiago Vignattibe143262012-04-16 17:31:41 +03002458 struct desktop_shell *shell =
2459 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002460
Pekka Paalanend81c2162011-11-16 13:47:34 +02002461 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002462 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002463 return;
2464 }
2465
2466 /* If desktop-shell client has gone away, unlock immediately. */
2467 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002468 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002469 return;
2470 }
2471
2472 if (shell->prepare_event_sent)
2473 return;
2474
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002475 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002476 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002477}
2478
2479static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002480show_input_panels(struct wl_listener *listener, void *data)
2481{
2482 struct desktop_shell *shell =
2483 container_of(listener, struct desktop_shell, show_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002484 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002485
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002486 wl_list_for_each_safe(surface, next,
2487 &shell->input_panel.surfaces, layer_link) {
2488 wl_list_remove(&surface->layer_link);
2489 wl_list_insert(&shell->panel_layer.surface_list,
2490 &surface->layer_link);
2491 weston_surface_assign_output(surface);
2492 weston_surface_damage(surface);
2493 weston_slide_run(surface,
2494 surface->geometry.height, 0, NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002495 }
2496}
2497
2498static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002499input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy);
2500
2501static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002502hide_input_panels(struct wl_listener *listener, void *data)
2503{
2504 struct desktop_shell *shell =
2505 container_of(listener, struct desktop_shell, hide_input_panel_listener);
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002506 struct weston_surface *surface, *next;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002507
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002508 wl_list_for_each_safe(surface, next,
2509 &shell->panel_layer.surface_list, layer_link)
2510 if (surface->configure == input_panel_configure) {
2511 weston_surface_unmap(surface);
2512 wl_list_insert(&shell->input_panel.surfaces,
2513 &surface->layer_link);
2514 }
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002515}
2516
2517static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002518center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002519{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002520 struct weston_mode *mode = output->current;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002521 GLfloat x = (mode->width - surface->buffer->width) / 2;
2522 GLfloat y = (mode->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002523
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002524 weston_surface_configure(surface, output->x + x, output->y + y,
2525 surface->buffer->width,
2526 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002527}
2528
2529static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002530map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002531 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002532{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002533 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002534 struct shell_surface *shsurf = get_shell_surface(surface);
2535 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002536 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002537 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002538 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002539 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002540
Pekka Paalanen60921e52012-01-25 15:55:43 +02002541 surface->geometry.width = width;
2542 surface->geometry.height = height;
2543 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002544
2545 /* initial positioning, see also configure() */
2546 switch (surface_type) {
2547 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002548 weston_surface_set_position(surface, 10 + random() % 400,
2549 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002550 break;
Alex Wu4539b082012-03-01 12:57:46 +08002551 case SHELL_SURFACE_FULLSCREEN:
2552 shell_map_fullscreen(shsurf);
2553 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002554 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002555 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002556 panel_height = get_output_panel_height(shell,surface->output);
2557 weston_surface_set_position(surface, surface->output->x,
2558 surface->output->y + panel_height);
2559 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002560 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002561 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002562 case SHELL_SURFACE_NONE:
2563 weston_surface_set_position(surface,
2564 surface->geometry.x + sx,
2565 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002566 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002567 default:
2568 ;
2569 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002570
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002571 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002572 switch (surface_type) {
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002573 case SHELL_SURFACE_POPUP:
2574 case SHELL_SURFACE_TRANSIENT:
2575 parent = shsurf->parent->surface;
2576 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2577 break;
Alex Wu4539b082012-03-01 12:57:46 +08002578 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002579 case SHELL_SURFACE_NONE:
2580 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002581 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002582 ws = get_current_workspace(shell);
2583 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002584 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002585 }
2586
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002587 if (surface_type != SHELL_SURFACE_NONE) {
2588 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002589 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2590 surface->output = shsurf->output;
2591 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002592
Juan Zhao7bb92f02011-12-15 11:31:51 -05002593 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002594 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002595 if (shsurf->transient.flags ==
2596 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2597 break;
2598 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002599 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002600 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002601 if (!shell->locked) {
2602 wl_list_for_each(seat, &compositor->seat_list, link)
2603 activate(shell, surface, seat);
2604 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002605 break;
2606 default:
2607 break;
2608 }
2609
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002610 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002611 {
2612 switch (shell->win_animation_type) {
2613 case ANIMATION_FADE:
2614 weston_fade_run(surface, NULL, NULL);
2615 break;
2616 case ANIMATION_ZOOM:
2617 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2618 break;
2619 default:
2620 break;
2621 }
2622 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002623}
2624
2625static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002626configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002627 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002628{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002629 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2630 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002631
Pekka Paalanen77346a62011-11-30 16:26:35 +02002632 shsurf = get_shell_surface(surface);
2633 if (shsurf)
2634 surface_type = shsurf->type;
2635
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002636 surface->geometry.x = x;
2637 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002638 surface->geometry.width = width;
2639 surface->geometry.height = height;
2640 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002641
2642 switch (surface_type) {
Alex Wu4539b082012-03-01 12:57:46 +08002643 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002644 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002645 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002646 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002647 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002648 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002649 surface->geometry.x = surface->output->x;
2650 surface->geometry.y = surface->output->y +
2651 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002652 break;
Alex Wu4539b082012-03-01 12:57:46 +08002653 case SHELL_SURFACE_TOPLEVEL:
2654 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002655 default:
2656 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002657 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002658
Alex Wu4539b082012-03-01 12:57:46 +08002659 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002660 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002661 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002662
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002663 if (surface_type == SHELL_SURFACE_MAXIMIZED)
Juan Zhao96879df2012-02-07 08:45:41 +08002664 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002665 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002666}
2667
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002668static void
2669shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2670{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002671 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002672 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002673 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002674
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002675 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002676 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002677 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002678 type_changed = 1;
2679 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002680
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002681 if (!weston_surface_is_mapped(es)) {
2682 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002683 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002684 es->geometry.width != es->buffer->width ||
2685 es->geometry.height != es->buffer->height) {
2686 GLfloat from_x, from_y;
2687 GLfloat to_x, to_y;
2688
2689 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2690 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2691 configure(shell, es,
2692 es->geometry.x + to_x - from_x,
2693 es->geometry.y + to_y - from_y,
2694 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002695 }
2696}
2697
Tiago Vignattibe143262012-04-16 17:31:41 +03002698static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002699
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002700static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002701desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002702{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002703 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002704 struct desktop_shell *shell =
2705 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002706
2707 shell->child.process.pid = 0;
2708 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002709
2710 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2711 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002712 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002713 shell->child.deathstamp = time;
2714 shell->child.deathcount = 0;
2715 }
2716
2717 shell->child.deathcount++;
2718 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002719 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002720 return;
2721 }
2722
Martin Minarik6d118362012-06-07 18:01:59 +02002723 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002724 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002725}
2726
2727static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002728launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002729{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002730 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002731
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002732 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002733 &shell->child.process,
2734 shell_exe,
2735 desktop_shell_sigchld);
2736
2737 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002738 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002739 return 0;
2740}
2741
2742static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002743bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2744{
Tiago Vignattibe143262012-04-16 17:31:41 +03002745 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002746
2747 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002748 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002749}
2750
Kristian Høgsberg75840622011-09-06 13:48:16 -04002751static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002752unbind_desktop_shell(struct wl_resource *resource)
2753{
Tiago Vignattibe143262012-04-16 17:31:41 +03002754 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002755
2756 if (shell->locked)
2757 resume_desktop(shell);
2758
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002759 shell->child.desktop_shell = NULL;
2760 shell->prepare_event_sent = false;
2761 free(resource);
2762}
2763
2764static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002765bind_desktop_shell(struct wl_client *client,
2766 void *data, uint32_t version, uint32_t id)
2767{
Tiago Vignattibe143262012-04-16 17:31:41 +03002768 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002769 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002770
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002771 resource = wl_client_add_object(client, &desktop_shell_interface,
2772 &desktop_shell_implementation,
2773 id, shell);
2774
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002775 if (client == shell->child.client) {
2776 resource->destroy = unbind_desktop_shell;
2777 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002778 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002779 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002780
2781 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2782 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002783 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002784}
2785
Pekka Paalanen6e168112011-11-24 11:34:05 +02002786static void
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002787screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2788{
2789 struct desktop_shell *shell = surface->private;
2790
2791 if (!shell->locked)
2792 return;
2793
2794 center_on_output(surface, surface->output);
2795
2796 if (wl_list_empty(&surface->layer_link)) {
2797 wl_list_insert(shell->lock_layer.surface_list.prev,
2798 &surface->layer_link);
2799 weston_surface_assign_output(surface);
2800 shell->compositor->idle_time = shell->screensaver.duration;
2801 weston_compositor_wake(shell->compositor);
2802 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2803 }
2804}
2805
2806static void
Pekka Paalanen6e168112011-11-24 11:34:05 +02002807screensaver_set_surface(struct wl_client *client,
2808 struct wl_resource *resource,
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002809 struct wl_resource *surface_resource,
Pekka Paalanen6e168112011-11-24 11:34:05 +02002810 struct wl_resource *output_resource)
2811{
Tiago Vignattibe143262012-04-16 17:31:41 +03002812 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002813 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002814 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002815
Kristian Høgsberg1a73a632012-06-26 22:15:53 -04002816 surface->configure = screensaver_configure;
2817 surface->private = shell;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002818 surface->output = output;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002819}
2820
2821static const struct screensaver_interface screensaver_implementation = {
2822 screensaver_set_surface
2823};
2824
2825static void
2826unbind_screensaver(struct wl_resource *resource)
2827{
Tiago Vignattibe143262012-04-16 17:31:41 +03002828 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002829
Pekka Paalanen77346a62011-11-30 16:26:35 +02002830 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002831 free(resource);
2832}
2833
2834static void
2835bind_screensaver(struct wl_client *client,
2836 void *data, uint32_t version, uint32_t id)
2837{
Tiago Vignattibe143262012-04-16 17:31:41 +03002838 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002839 struct wl_resource *resource;
2840
2841 resource = wl_client_add_object(client, &screensaver_interface,
2842 &screensaver_implementation,
2843 id, shell);
2844
Pekka Paalanen77346a62011-11-30 16:26:35 +02002845 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002846 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002847 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002848 return;
2849 }
2850
2851 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2852 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002853 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002854}
2855
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002856static void
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002857input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2858{
2859 struct weston_mode *mode = surface->output->current;
2860 GLfloat x = (mode->width - surface->buffer->width) / 2;
2861 GLfloat y = mode->height - surface->buffer->height;
2862
2863 /* Don't map the input panel here, wait for
2864 * show_input_panels signal. */
2865
2866 weston_surface_configure(surface,
2867 surface->output->x + x,
2868 surface->output->y + y,
2869 surface->buffer->width,
2870 surface->buffer->height);
2871}
2872
2873static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002874input_panel_set_surface(struct wl_client *client,
2875 struct wl_resource *resource,
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002876 struct wl_resource *surface_resource,
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002877 struct wl_resource *output_resource)
2878{
2879 struct desktop_shell *shell = resource->data;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002880 struct weston_surface *surface = surface_resource->data;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002881 struct weston_output *output = output_resource->data;
2882
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002883 surface->configure = input_panel_configure;
2884 surface->private = shell;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002885 surface->output = output;
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04002886 wl_list_insert(shell->input_panel.surfaces.prev, &surface->layer_link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002887}
2888
2889static const struct input_panel_interface input_panel_implementation = {
2890 input_panel_set_surface
2891};
2892
2893static void
2894unbind_input_panel(struct wl_resource *resource)
2895{
2896 struct desktop_shell *shell = resource->data;
2897
2898 shell->input_panel.binding = NULL;
2899 free(resource);
2900}
2901
2902static void
2903bind_input_panel(struct wl_client *client,
2904 void *data, uint32_t version, uint32_t id)
2905{
2906 struct desktop_shell *shell = data;
2907 struct wl_resource *resource;
2908
2909 resource = wl_client_add_object(client, &input_panel_interface,
2910 &input_panel_implementation,
2911 id, shell);
2912
2913 if (shell->input_panel.binding == NULL) {
2914 resource->destroy = unbind_input_panel;
2915 shell->input_panel.binding = resource;
2916 return;
2917 }
2918
2919 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2920 "interface object already bound");
2921 wl_resource_destroy(resource);
2922}
2923
Kristian Høgsberg07045392012-02-19 18:52:44 -05002924struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002925 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002926 struct weston_surface *current;
2927 struct wl_listener listener;
2928 struct wl_keyboard_grab grab;
2929};
2930
2931static void
2932switcher_next(struct switcher *switcher)
2933{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002934 struct weston_surface *surface;
2935 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002936 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002937 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002938
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002939 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002940 switch (get_shell_surface_type(surface)) {
2941 case SHELL_SURFACE_TOPLEVEL:
2942 case SHELL_SURFACE_FULLSCREEN:
2943 case SHELL_SURFACE_MAXIMIZED:
2944 if (first == NULL)
2945 first = surface;
2946 if (prev == switcher->current)
2947 next = surface;
2948 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002949 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002950 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002951 weston_surface_damage(surface);
2952 break;
2953 default:
2954 break;
2955 }
Alex Wu1659daa2012-04-01 20:13:09 +08002956
2957 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002958 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002959 surface->geometry.dirty = 1;
2960 weston_surface_damage(surface);
2961 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002962 }
2963
2964 if (next == NULL)
2965 next = first;
2966
Alex Wu07b26062012-03-12 16:06:01 +08002967 if (next == NULL)
2968 return;
2969
Kristian Høgsberg07045392012-02-19 18:52:44 -05002970 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002971 wl_signal_add(&next->surface.resource.destroy_signal,
2972 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002973
2974 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002975 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002976
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002977 shsurf = get_shell_surface(switcher->current);
2978 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002979 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002980}
2981
2982static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002983switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002984{
2985 struct switcher *switcher =
2986 container_of(listener, struct switcher, listener);
2987
2988 switcher_next(switcher);
2989}
2990
2991static void
Daniel Stone351eb612012-05-31 15:27:47 -04002992switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002993{
Kristian Høgsberg07045392012-02-19 18:52:44 -05002994 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002995 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002996 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002997
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002998 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002999 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003000 weston_surface_damage(surface);
3001 }
3002
Alex Wu07b26062012-03-12 16:06:01 +08003003 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003004 activate(switcher->shell, switcher->current,
3005 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003006 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003007 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003008 free(switcher);
3009}
3010
3011static void
3012switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003013 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003014{
3015 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003016 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003017
Daniel Stonec9785ea2012-05-30 16:31:52 +01003018 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003019 switcher_next(switcher);
3020}
3021
3022static void
3023switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3024 uint32_t mods_depressed, uint32_t mods_latched,
3025 uint32_t mods_locked, uint32_t group)
3026{
3027 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003028 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003029
Daniel Stone351eb612012-05-31 15:27:47 -04003030 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3031 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003032}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003033
3034static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003035 switcher_key,
3036 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003037};
3038
3039static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003040switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3041 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003042{
Tiago Vignattibe143262012-04-16 17:31:41 +03003043 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003044 struct switcher *switcher;
3045
3046 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003047 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003048 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003049 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003050 wl_list_init(&switcher->listener.link);
3051
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003052 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003053 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003054 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3055 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003056 switcher_next(switcher);
3057}
3058
Pekka Paalanen3c647232011-12-22 13:43:43 +02003059static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003060backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3061 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003062{
3063 struct weston_compositor *compositor = data;
3064 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003065 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003066
3067 /* TODO: we're limiting to simple use cases, where we assume just
3068 * control on the primary display. We'd have to extend later if we
3069 * ever get support for setting backlights on random desktop LCD
3070 * panels though */
3071 output = get_default_output(compositor);
3072 if (!output)
3073 return;
3074
3075 if (!output->set_backlight)
3076 return;
3077
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003078 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3079 backlight_new = output->backlight_current - 25;
3080 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3081 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003082
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003083 if (backlight_new < 5)
3084 backlight_new = 5;
3085 if (backlight_new > 255)
3086 backlight_new = 255;
3087
3088 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003089 output->set_backlight(output, output->backlight_current);
3090}
3091
3092static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003093debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3094 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003095{
Tiago Vignattibe143262012-04-16 17:31:41 +03003096 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003097 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003098 struct weston_surface *surface;
3099
3100 if (shell->debug_repaint_surface) {
3101 weston_surface_destroy(shell->debug_repaint_surface);
3102 shell->debug_repaint_surface = NULL;
3103 } else {
3104 surface = weston_surface_create(compositor);
3105 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3106 weston_surface_configure(surface, 0, 0, 8192, 8192);
3107 wl_list_insert(&compositor->fade_layer.surface_list,
3108 &surface->layer_link);
3109 weston_surface_assign_output(surface);
3110 pixman_region32_init(&surface->input);
3111
3112 /* Here's the dirty little trick that makes the
3113 * repaint debugging work: we force an
3114 * update_transform first to update dependent state
3115 * and clear the geometry.dirty bit. Then we clear
3116 * the surface damage so it only gets repainted
3117 * piecewise as we repaint other things. */
3118
3119 weston_surface_update_transform(surface);
3120 pixman_region32_fini(&surface->damage);
3121 pixman_region32_init(&surface->damage);
3122 shell->debug_repaint_surface = surface;
3123 }
3124}
3125
3126static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003127force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3128 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003129{
3130 struct wl_client *client;
3131 pid_t pid;
3132 uid_t uid;
3133 gid_t gid;
3134
Daniel Stone325fc2d2012-05-30 16:31:58 +01003135 client = seat->keyboard->focus->resource.client;
3136 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003137
Daniel Stone325fc2d2012-05-30 16:31:58 +01003138 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003139}
3140
3141static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003142workspace_up_binding(struct wl_seat *seat, uint32_t time,
3143 uint32_t key, void *data)
3144{
3145 struct desktop_shell *shell = data;
3146 unsigned int new_index = shell->workspaces.current;
3147
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003148 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003149 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003150 if (new_index != 0)
3151 new_index--;
3152
3153 change_workspace(shell, new_index);
3154}
3155
3156static void
3157workspace_down_binding(struct wl_seat *seat, uint32_t time,
3158 uint32_t key, void *data)
3159{
3160 struct desktop_shell *shell = data;
3161 unsigned int new_index = shell->workspaces.current;
3162
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003163 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003164 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003165 if (new_index < shell->workspaces.num - 1)
3166 new_index++;
3167
3168 change_workspace(shell, new_index);
3169}
3170
3171static void
3172workspace_f_binding(struct wl_seat *seat, uint32_t time,
3173 uint32_t key, void *data)
3174{
3175 struct desktop_shell *shell = data;
3176 unsigned int new_index;
3177
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003178 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003179 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003180 new_index = key - KEY_F1;
3181 if (new_index >= shell->workspaces.num)
3182 new_index = shell->workspaces.num - 1;
3183
3184 change_workspace(shell, new_index);
3185}
3186
3187
3188static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003189shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003190{
Tiago Vignattibe143262012-04-16 17:31:41 +03003191 struct desktop_shell *shell =
3192 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003193 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003194
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003195 if (shell->child.client)
3196 wl_client_destroy(shell->child.client);
3197
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003198 wl_list_remove(&shell->lock_listener.link);
3199 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003200 wl_list_remove(&shell->show_input_panel_listener.link);
3201 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003202
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003203 wl_array_for_each(ws, &shell->workspaces.array)
3204 workspace_destroy(*ws);
3205 wl_array_release(&shell->workspaces.array);
3206
Pekka Paalanen3c647232011-12-22 13:43:43 +02003207 free(shell->screensaver.path);
3208 free(shell);
3209}
3210
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003211static void
3212shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3213{
3214 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003215 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003216
3217 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003218 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3219 MODIFIER_CTRL | MODIFIER_ALT,
3220 terminate_binding, ec);
3221 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3222 click_to_activate_binding,
3223 shell);
3224 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3225 MODIFIER_SUPER | MODIFIER_ALT,
3226 surface_opacity_binding, NULL);
3227 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3228 MODIFIER_SUPER, zoom_axis_binding,
3229 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003230
3231 /* configurable bindings */
3232 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003233 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3234 zoom_key_binding, NULL);
3235 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3236 zoom_key_binding, NULL);
3237 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3238 shell);
3239 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3240 resize_binding, shell);
3241 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3242 rotate_binding, NULL);
3243 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3244 shell);
3245 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3246 ec);
3247 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3248 backlight_binding, ec);
3249 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3250 ec);
3251 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3252 backlight_binding, ec);
3253 weston_compositor_add_key_binding(ec, KEY_SPACE, mod,
3254 debug_repaint_binding, shell);
3255 weston_compositor_add_key_binding(ec, KEY_K, mod,
3256 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003257 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3258 workspace_up_binding, shell);
3259 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3260 workspace_down_binding, shell);
3261
3262 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3263 if (shell->workspaces.num > 1) {
3264 num_workspace_bindings = shell->workspaces.num;
3265 if (num_workspace_bindings > 6)
3266 num_workspace_bindings = 6;
3267 for (i = 0; i < num_workspace_bindings; i++)
3268 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3269 workspace_f_binding,
3270 shell);
3271 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003272}
3273
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003274int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003275shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003276
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003277WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003278shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003279{
Tiago Vignattibe143262012-04-16 17:31:41 +03003280 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003281 struct workspace **pws;
3282 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003283
3284 shell = malloc(sizeof *shell);
3285 if (shell == NULL)
3286 return -1;
3287
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003288 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003289 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003290
3291 shell->destroy_listener.notify = shell_destroy;
3292 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3293 shell->lock_listener.notify = lock;
3294 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3295 shell->unlock_listener.notify = unlock;
3296 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003297 shell->show_input_panel_listener.notify = show_input_panels;
3298 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3299 shell->hide_input_panel_listener.notify = hide_input_panels;
3300 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003301 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003302 ec->shell_interface.create_shell_surface = create_shell_surface;
3303 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003304 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003305 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003306 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003307
Pekka Paalanen77346a62011-11-30 16:26:35 +02003308 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003309 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003310
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003311 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3312 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003313 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3314 weston_layer_init(&shell->lock_layer, NULL);
3315
3316 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003317
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003318 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003319
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003320 for (i = 0; i < shell->workspaces.num; i++) {
3321 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3322 if (pws == NULL)
3323 return -1;
3324
3325 *pws = workspace_create();
3326 if (*pws == NULL)
3327 return -1;
3328 }
3329 activate_workspace(shell, 0);
3330
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003331 wl_list_init(&shell->workspaces.animation.link);
3332 shell->workspaces.animation.frame = animate_workspace_change_frame;
3333
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003334 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3335 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003336 return -1;
3337
Kristian Høgsberg75840622011-09-06 13:48:16 -04003338 if (wl_display_add_global(ec->wl_display,
3339 &desktop_shell_interface,
3340 shell, bind_desktop_shell) == NULL)
3341 return -1;
3342
Pekka Paalanen6e168112011-11-24 11:34:05 +02003343 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3344 shell, bind_screensaver) == NULL)
3345 return -1;
3346
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003347 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3348 shell, bind_input_panel) == NULL)
3349 return -1;
3350
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003351 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003352 if (launch_desktop_shell_process(shell) != 0)
3353 return -1;
3354
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003355 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003356 if (ec->seat->seat.pointer)
3357 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3358 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003359
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003360 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003361
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003362 return 0;
3363}