blob: 95b010daaf78fcedca15eee9da125a81ad0de723 [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
Pekka Paalanen77346a62011-11-30 16:26:35 +0200132 SHELL_SURFACE_SCREENSAVER,
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200133 SHELL_SURFACE_INPUT_PANEL,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500134
135 SHELL_SURFACE_TOPLEVEL,
136 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500137 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800138 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500139 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200140};
141
Scott Moreauff1db4a2012-04-17 19:06:18 -0600142struct ping_timer {
143 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600144 uint32_t serial;
145};
146
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200147struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200148 struct wl_resource resource;
149
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500150 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200151 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500152 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300153 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200154
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400155 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400156 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500157 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800158 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800159 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600160 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100161
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500162 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200163 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500164 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200165 } rotation;
166
167 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700168 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500169 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200170 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500171 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100172 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400173 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500174 } popup;
175
Alex Wu4539b082012-03-01 12:57:46 +0800176 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300177 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400178 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300179 } transient;
180
181 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800182 enum wl_shell_surface_fullscreen_method type;
183 struct weston_transform transform; /* matrix from x, y */
184 uint32_t framerate;
185 struct weston_surface *black_surface;
186 } fullscreen;
187
Scott Moreauff1db4a2012-04-17 19:06:18 -0600188 struct ping_timer *ping_timer;
189
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200190 struct weston_transform workspace_transform;
191
Jonas Ådahl04769742012-06-13 00:01:24 +0200192 struct focus_state *focus_state;
193
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500194 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500195 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100196 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400197
198 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200199};
200
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300201struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700202 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300203 struct shell_surface *shsurf;
204 struct wl_listener shsurf_destroy_listener;
205};
206
207struct weston_move_grab {
208 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100209 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500210};
211
Pekka Paalanen460099f2012-01-20 16:48:25 +0200212struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300213 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500214 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200215 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400216 GLfloat x;
217 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200218 } center;
219};
220
Alex Wubd3354b2012-04-17 17:20:49 +0800221static struct shell_surface *
222get_shell_surface(struct weston_surface *surface);
223
224static struct desktop_shell *
225shell_surface_get_shell(struct shell_surface *shsurf);
226
227static bool
228shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
229{
230 struct desktop_shell *shell;
231 struct weston_surface *top_fs_es;
232
233 shell = shell_surface_get_shell(shsurf);
234
235 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
236 return false;
237
238 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
239 struct weston_surface,
240 layer_link);
241 return (shsurf == get_shell_surface(top_fs_es));
242}
243
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500244static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400245destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300246{
247 struct shell_grab *grab;
248
249 grab = container_of(listener, struct shell_grab,
250 shsurf_destroy_listener);
251
252 grab->shsurf = NULL;
253}
254
255static void
256shell_grab_init(struct shell_grab *grab,
257 const struct wl_pointer_grab_interface *interface,
258 struct shell_surface *shsurf)
259{
260 grab->grab.interface = interface;
261 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400262 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
263 wl_signal_add(&shsurf->resource.destroy_signal,
264 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300265
266}
267
268static void
269shell_grab_finish(struct shell_grab *grab)
270{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400271 if (grab->shsurf)
272 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300273}
274
275static void
Alex Wu4539b082012-03-01 12:57:46 +0800276center_on_output(struct weston_surface *surface,
277 struct weston_output *output);
278
Daniel Stone496ca172012-05-30 16:31:42 +0100279static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300280get_modifier(char *modifier)
281{
282 if (!modifier)
283 return MODIFIER_SUPER;
284
285 if (!strcmp("ctrl", modifier))
286 return MODIFIER_CTRL;
287 else if (!strcmp("alt", modifier))
288 return MODIFIER_ALT;
289 else if (!strcmp("super", modifier))
290 return MODIFIER_SUPER;
291 else
292 return MODIFIER_SUPER;
293}
294
Juan Zhaoe10d2792012-04-25 19:09:52 +0800295static enum animation_type
296get_animation_type(char *animation)
297{
298 if (!animation)
299 return ANIMATION_NONE;
300
301 if (!strcmp("zoom", animation))
302 return ANIMATION_ZOOM;
303 else if (!strcmp("fade", animation))
304 return ANIMATION_FADE;
305 else
306 return ANIMATION_NONE;
307}
308
Alex Wu4539b082012-03-01 12:57:46 +0800309static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300310shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200311{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200312 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200313 char *path = NULL;
314 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200315 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300316 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800317 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200318
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400319 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300320 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800321 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200322 { "num-workspaces",
323 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200324 };
325
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400326 struct config_key saver_keys[] = {
327 { "path", CONFIG_KEY_STRING, &path },
328 { "duration", CONFIG_KEY_INTEGER, &duration },
329 };
330
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200331 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400332 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200333 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
334 };
335
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200336 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500337 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200338 free(config_file);
339
Pekka Paalanen7296e792011-12-07 16:22:00 +0200340 shell->screensaver.path = path;
341 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300342 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800343 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200344 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
345}
346
347static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200348focus_state_destroy(struct focus_state *state)
349{
350 wl_list_remove(&state->seat_destroy_listener.link);
351 wl_list_remove(&state->surface_destroy_listener.link);
352 free(state);
353}
354
355static void
356focus_state_seat_destroy(struct wl_listener *listener, void *data)
357{
358 struct focus_state *state = container_of(listener,
359 struct focus_state,
360 seat_destroy_listener);
361
362 wl_list_remove(&state->link);
363 focus_state_destroy(state);
364}
365
366static void
367focus_state_surface_destroy(struct wl_listener *listener, void *data)
368{
369 struct focus_state *state = container_of(listener,
370 struct focus_state,
371 seat_destroy_listener);
372
373 wl_list_remove(&state->link);
374 focus_state_destroy(state);
375}
376
377static struct focus_state *
378focus_state_create(struct weston_seat *seat)
379{
380 struct wl_keyboard *keyboard = seat->seat.keyboard;
381 struct focus_state *state;
382 struct wl_surface *surface;
383 struct shell_surface *shsurf;
384
385 state = malloc(sizeof *state);
386 if (state == NULL)
387 return NULL;
388
389 surface = keyboard->focus;
390 shsurf = get_shell_surface((struct weston_surface *)keyboard->focus);
391 shsurf->focus_state = state;
392
393 state->seat = seat;
394 state->keyboard_focus = shsurf->surface;
395 wl_list_init(&state->link);
396
397 state->seat_destroy_listener.notify = focus_state_seat_destroy;
398 state->surface_destroy_listener.notify = focus_state_surface_destroy;
399 wl_signal_add(&seat->seat.destroy_signal,
400 &state->seat_destroy_listener);
401 wl_signal_add(&surface->resource.destroy_signal,
402 &state->surface_destroy_listener);
403
404 return state;
405}
406
407static void
408pop_focus_state(struct desktop_shell *shell, struct workspace *ws)
409{
410 struct focus_state *state, *next;
411
412 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
413 if (state->keyboard_focus)
414 wl_keyboard_set_focus(state->seat->seat.keyboard,
415 &state->keyboard_focus->surface);
416
417 focus_state_destroy(state);
418 }
419 wl_list_init(&ws->focus_list);
420}
421
422static void
423push_focus_state(struct desktop_shell *shell, struct workspace *ws)
424{
425 struct weston_seat *seat;
426 struct focus_state *state;
427 struct wl_keyboard *keyboard;
428
429 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
430 keyboard = seat->seat.keyboard;
431 if (keyboard && keyboard->focus) {
432 state = focus_state_create(seat);
433 if (state == NULL)
434 return;
435
436 wl_list_insert(&ws->focus_list, &state->link);
437
438 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
439 }
440 }
441}
442
443static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200444workspace_destroy(struct workspace *ws)
445{
Jonas Ådahl04769742012-06-13 00:01:24 +0200446 struct focus_state *state, *next;
447
448 wl_list_for_each_safe(state, next, &ws->focus_list, link)
449 focus_state_destroy(state);
450
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200451 free(ws);
452}
453
Jonas Ådahl04769742012-06-13 00:01:24 +0200454static void
455seat_destroyed(struct wl_listener *listener, void *data)
456{
457 struct weston_seat *seat = data;
458 struct focus_state *state, *next;
459 struct workspace *ws = container_of(listener,
460 struct workspace,
461 seat_destroyed_listener);
462
463 wl_list_for_each_safe(state, next, &ws->focus_list, link)
464 if (state->seat == seat)
465 wl_list_remove(&state->link);
466}
467
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200468static struct workspace *
469workspace_create(void)
470{
471 struct workspace *ws = malloc(sizeof *ws);
472 if (ws == NULL)
473 return NULL;
474
475 weston_layer_init(&ws->layer, NULL);
476
Jonas Ådahl04769742012-06-13 00:01:24 +0200477 wl_list_init(&ws->focus_list);
478 wl_list_init(&ws->seat_destroyed_listener.link);
479 ws->seat_destroyed_listener.notify = seat_destroyed;
480
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200481 return ws;
482}
483
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200484static int
485workspace_is_empty(struct workspace *ws)
486{
487 return wl_list_empty(&ws->layer.surface_list);
488}
489
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200490static struct workspace *
491get_workspace(struct desktop_shell *shell, unsigned int index)
492{
493 struct workspace **pws = shell->workspaces.array.data;
494 pws += index;
495 return *pws;
496}
497
498static struct workspace *
499get_current_workspace(struct desktop_shell *shell)
500{
501 return get_workspace(shell, shell->workspaces.current);
502}
503
504static void
505activate_workspace(struct desktop_shell *shell, unsigned int index)
506{
507 struct workspace *ws;
508
509 ws = get_workspace(shell, index);
510 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
511
512 shell->workspaces.current = index;
513}
514
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200515static unsigned int
516get_output_height(struct weston_output *output)
517{
518 return abs(output->region.extents.y1 - output->region.extents.y2);
519}
520
521static void
522surface_translate(struct weston_surface *surface, double d)
523{
524 struct shell_surface *shsurf = get_shell_surface(surface);
525 struct weston_transform *transform;
526
527 transform = &shsurf->workspace_transform;
528 if (wl_list_empty(&transform->link))
529 wl_list_insert(surface->geometry.transformation_list.prev,
530 &shsurf->workspace_transform.link);
531
532 weston_matrix_init(&shsurf->workspace_transform.matrix);
533 weston_matrix_translate(&shsurf->workspace_transform.matrix,
534 0.0, d, 0.0);
535 surface->geometry.dirty = 1;
536}
537
538static void
539workspace_translate_out(struct workspace *ws, double fraction)
540{
541 struct weston_surface *surface;
542 unsigned int height;
543 double d;
544
545 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
546 height = get_output_height(surface->output);
547 d = height * fraction;
548
549 surface_translate(surface, d);
550 }
551}
552
553static void
554workspace_translate_in(struct workspace *ws, double fraction)
555{
556 struct weston_surface *surface;
557 unsigned int height;
558 double d;
559
560 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
561 height = get_output_height(surface->output);
562
563 if (fraction > 0)
564 d = -(height - height * fraction);
565 else
566 d = height + height * fraction;
567
568 surface_translate(surface, d);
569 }
570}
571
572static void
573workspace_damage_all_surfaces(struct workspace *ws)
574{
575 struct weston_surface *surface;
576
577 wl_list_for_each(surface, &ws->layer.surface_list, layer_link)
578 weston_surface_damage(surface);
579}
580
581static void
582reverse_workspace_change_animation(struct desktop_shell *shell,
583 unsigned int index,
584 struct workspace *from,
585 struct workspace *to)
586{
587 shell->workspaces.current = index;
588
589 shell->workspaces.anim_to = to;
590 shell->workspaces.anim_from = from;
591 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
592 shell->workspaces.anim_timestamp = 0;
593
Jonas Ådahl04769742012-06-13 00:01:24 +0200594 push_focus_state(shell, from);
595 pop_focus_state(shell, to);
596
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200597 workspace_damage_all_surfaces(from);
598 workspace_damage_all_surfaces(to);
599}
600
601static void
602workspace_deactivate_transforms(struct workspace *ws)
603{
604 struct weston_surface *surface;
605 struct shell_surface *shsurf;
606
607 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
608 shsurf = get_shell_surface(surface);
609 wl_list_remove(&shsurf->workspace_transform.link);
610 wl_list_init(&shsurf->workspace_transform.link);
611 shsurf->surface->geometry.dirty = 1;
612 }
613}
614
615static void
616finish_workspace_change_animation(struct desktop_shell *shell,
617 struct workspace *from,
618 struct workspace *to)
619{
620 workspace_damage_all_surfaces(from);
621 workspace_damage_all_surfaces(to);
622
623 wl_list_remove(&shell->workspaces.animation.link);
624 workspace_deactivate_transforms(from);
625 workspace_deactivate_transforms(to);
626 shell->workspaces.anim_to = NULL;
627
628 wl_list_remove(&shell->workspaces.anim_from->layer.link);
629}
630
631static void
632animate_workspace_change_frame(struct weston_animation *animation,
633 struct weston_output *output, uint32_t msecs)
634{
635 struct desktop_shell *shell =
636 container_of(animation, struct desktop_shell,
637 workspaces.animation);
638 struct workspace *from = shell->workspaces.anim_from;
639 struct workspace *to = shell->workspaces.anim_to;
640 uint32_t t;
641 double x, y;
642
643 if (workspace_is_empty(from) && workspace_is_empty(to)) {
644 finish_workspace_change_animation(shell, from, to);
645 return;
646 }
647
648 if (shell->workspaces.anim_timestamp == 0) {
649 if (shell->workspaces.anim_current == 0.0)
650 shell->workspaces.anim_timestamp = msecs;
651 else
652 shell->workspaces.anim_timestamp =
653 msecs -
654 /* Invers of movement function 'y' below. */
655 (asin(1.0 - shell->workspaces.anim_current) *
656 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
657 M_2_PI);
658 }
659
660 t = msecs - shell->workspaces.anim_timestamp;
661
662 /*
663 * x = [0, π/2]
664 * y(x) = sin(x)
665 */
666 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
667 y = sin(x);
668
669 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
670 workspace_damage_all_surfaces(from);
671 workspace_damage_all_surfaces(to);
672
673 workspace_translate_out(from, shell->workspaces.anim_dir * y);
674 workspace_translate_in(to, shell->workspaces.anim_dir * y);
675 shell->workspaces.anim_current = y;
676
677 workspace_damage_all_surfaces(from);
678 workspace_damage_all_surfaces(to);
679 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200680 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200681 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200682}
683
684static void
685animate_workspace_change(struct desktop_shell *shell,
686 unsigned int index,
687 struct workspace *from,
688 struct workspace *to)
689{
690 struct weston_output *output;
691
692 int dir;
693
694 if (index > shell->workspaces.current)
695 dir = -1;
696 else
697 dir = 1;
698
699 shell->workspaces.current = index;
700
701 shell->workspaces.anim_dir = dir;
702 shell->workspaces.anim_from = from;
703 shell->workspaces.anim_to = to;
704 shell->workspaces.anim_current = 0.0;
705 shell->workspaces.anim_timestamp = 0;
706
707 output = container_of(shell->compositor->output_list.next,
708 struct weston_output, link);
709 wl_list_insert(&output->animation_list,
710 &shell->workspaces.animation.link);
711
712 wl_list_insert(&from->layer.link, &to->layer.link);
713
714 workspace_translate_in(to, 0);
715
Jonas Ådahl04769742012-06-13 00:01:24 +0200716 push_focus_state(shell, from);
717 pop_focus_state(shell, to);
718
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200719 workspace_damage_all_surfaces(from);
720 workspace_damage_all_surfaces(to);
721}
722
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200723static void
724change_workspace(struct desktop_shell *shell, unsigned int index)
725{
726 struct workspace *from;
727 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200728
729 if (index == shell->workspaces.current)
730 return;
731
732 /* Don't change workspace when there is any fullscreen surfaces. */
733 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
734 return;
735
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200736 from = get_current_workspace(shell);
737 to = get_workspace(shell, index);
738
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200739 if (shell->workspaces.anim_from == to &&
740 shell->workspaces.anim_to == from) {
741 reverse_workspace_change_animation(shell, index, from, to);
742 return;
743 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200744
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200745 if (shell->workspaces.anim_to != NULL)
746 finish_workspace_change_animation(shell,
747 shell->workspaces.anim_from,
748 shell->workspaces.anim_to);
749
750 if (workspace_is_empty(to) && workspace_is_empty(from)) {
751 shell->workspaces.current = index;
752 wl_list_insert(&from->layer.link, &to->layer.link);
753 wl_list_remove(&from->layer.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200754
755 push_focus_state(shell, from);
756 pop_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200757 }
758 else
759 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200760}
761
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200762static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400763noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100764 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500765{
766 grab->focus = NULL;
767}
768
769static void
Scott Moreau447013d2012-02-18 05:05:29 -0700770move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100771 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500772{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500773 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100774 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300775 struct shell_surface *shsurf = move->base.shsurf;
776 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100777 int dx = wl_fixed_to_int(pointer->x + move->dx);
778 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300779
780 if (!shsurf)
781 return;
782
783 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500784
Daniel Stone103db7f2012-05-08 17:17:55 +0100785 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200786 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -0400787
788 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500789}
790
791static void
Scott Moreau447013d2012-02-18 05:05:29 -0700792move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100793 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500794{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300795 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
796 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100797 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100798 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500799
Daniel Stone4dbadb12012-05-30 16:31:51 +0100800 if (pointer->button_count == 0 &&
801 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300802 shell_grab_finish(shell_grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100803 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500804 free(grab);
805 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500806}
807
Scott Moreau447013d2012-02-18 05:05:29 -0700808static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500809 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500810 move_grab_motion,
811 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500812};
813
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600814static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400815busy_cursor_grab_focus(struct wl_pointer_grab *base,
816 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600817{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400818 struct shell_grab *grab = (struct shell_grab *) base;
819 struct wl_pointer *pointer = base->pointer;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600820
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400821 if (grab->grab.focus != surface) {
822 shell_grab_finish(grab);
823 wl_pointer_end_grab(pointer);
824 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600825 }
826}
827
828static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400829busy_cursor_grab_motion(struct wl_pointer_grab *grab,
830 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600831{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400832}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600833
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400834static void
835busy_cursor_grab_button(struct wl_pointer_grab *grab,
836 uint32_t time, uint32_t button, uint32_t state)
837{
838}
839
840static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
841 busy_cursor_grab_focus,
842 busy_cursor_grab_motion,
843 busy_cursor_grab_button,
844};
845
846static void
847set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
848{
849 struct shell_grab *grab;
850 struct desktop_shell *shell = shsurf->shell;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400851
852 grab = malloc(sizeof *grab);
853 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600854 return;
855
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400856 shell_grab_init(grab, &busy_cursor_grab_interface, shsurf);
857 grab->grab.focus = &shsurf->surface->surface;
858 wl_pointer_start_grab(pointer, &grab->grab);
859 wl_pointer_set_focus(pointer, &shell->busy_surface->surface, 0, 0);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400860}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600861
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400862static void
863end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
864{
865 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
866
867 if (grab->grab.interface == &busy_cursor_grab_interface) {
868 shell_grab_finish(grab);
869 wl_pointer_end_grab(pointer);
870 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600871 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600872}
873
Scott Moreau9521d5e2012-04-19 13:06:17 -0600874static void
875ping_timer_destroy(struct shell_surface *shsurf)
876{
877 if (!shsurf || !shsurf->ping_timer)
878 return;
879
880 if (shsurf->ping_timer->source)
881 wl_event_source_remove(shsurf->ping_timer->source);
882
883 free(shsurf->ping_timer);
884 shsurf->ping_timer = NULL;
885}
886
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400887static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600888ping_timeout_handler(void *data)
889{
890 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400891 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600892
Scott Moreau9521d5e2012-04-19 13:06:17 -0600893 /* Client is not responding */
894 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400895
896 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
897 if (seat->seat.pointer->focus == &shsurf->surface->surface)
898 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600899
900 return 1;
901}
902
903static void
904ping_handler(struct weston_surface *surface, uint32_t serial)
905{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400906 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600907 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400908 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600909
910 if (!shsurf)
911 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400912 if (!shsurf->resource.client)
913 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600914
915 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300916 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600917 if (!shsurf->ping_timer)
918 return;
919
920 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600921 loop = wl_display_get_event_loop(surface->compositor->wl_display);
922 shsurf->ping_timer->source =
923 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
924 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
925
926 wl_shell_surface_send_ping(&shsurf->resource, serial);
927 }
928}
929
930static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400931handle_pointer_focus(struct wl_listener *listener, void *data)
932{
933 struct wl_pointer *pointer = data;
934 struct weston_surface *surface =
935 (struct weston_surface *) pointer->focus;
936 struct weston_compositor *compositor;
937 struct shell_surface *shsurf;
938 uint32_t serial;
939
940 if (!surface)
941 return;
942
943 compositor = surface->compositor;
944 shsurf = get_shell_surface(surface);
945
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +0300946 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400947 set_busy_cursor(shsurf, pointer);
948 } else {
949 serial = wl_display_next_serial(compositor->wl_display);
950 ping_handler(surface, serial);
951 }
952}
953
954static void
Scott Moreauff1db4a2012-04-17 19:06:18 -0600955shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
956 uint32_t serial)
957{
958 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400959 struct desktop_shell *shell = shsurf->shell;
960 struct weston_seat *seat;
961 struct weston_compositor *ec = shsurf->surface->compositor;
962 struct wl_pointer *pointer;
963 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600964
Scott Moreauff1db4a2012-04-17 19:06:18 -0600965 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400966 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600967 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400968 if (was_unresponsive) {
969 /* Received pong from previously unresponsive client */
970 wl_list_for_each(seat, &ec->seat_list, link) {
971 pointer = seat->seat.pointer;
972 if (pointer->focus ==
973 &shell->busy_surface->surface &&
974 pointer->current ==
975 &shsurf->surface->surface)
976 end_busy_cursor(shsurf, pointer);
977 }
978 }
Scott Moreau9521d5e2012-04-19 13:06:17 -0600979 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600980 }
981}
982
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400983static void
984shell_surface_set_title(struct wl_client *client,
985 struct wl_resource *resource, const char *title)
986{
987 struct shell_surface *shsurf = resource->data;
988
989 free(shsurf->title);
990 shsurf->title = strdup(title);
991}
992
993static void
994shell_surface_set_class(struct wl_client *client,
995 struct wl_resource *resource, const char *class)
996{
997 struct shell_surface *shsurf = resource->data;
998
999 free(shsurf->class);
1000 shsurf->class = strdup(class);
1001}
1002
Scott Moreauff1db4a2012-04-17 19:06:18 -06001003static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001004surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001005{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001006 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001007
1008 if (!shsurf)
1009 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001010
1011 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001012 if (!move)
1013 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001014
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001015 shell_grab_init(&move->base, &move_grab_interface, shsurf);
1016
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001017 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +01001018 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001019 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +01001020 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001021
Daniel Stone37816df2012-05-16 18:45:18 +01001022 wl_pointer_start_grab(ws->seat.pointer, &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001023
Daniel Stone37816df2012-05-16 18:45:18 +01001024 wl_pointer_set_focus(ws->seat.pointer, NULL,
1025 wl_fixed_from_int(0),
1026 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001027
1028 return 0;
1029}
1030
1031static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001032shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001033 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001034{
Daniel Stone37816df2012-05-16 18:45:18 +01001035 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001036 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001037
Daniel Stone37816df2012-05-16 18:45:18 +01001038 if (ws->seat.pointer->button_count == 0 ||
1039 ws->seat.pointer->grab_serial != serial ||
1040 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001041 return;
1042
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001043 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001044 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001045}
1046
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001047struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001048 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001049 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001050 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001051};
1052
1053static void
Scott Moreau447013d2012-02-18 05:05:29 -07001054resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001055 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001056{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001057 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001058 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001059 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001060 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +01001061 wl_fixed_t from_x, from_y;
1062 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001063
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001064 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001065 return;
1066
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001067 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001068 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +01001069 &from_x, &from_y);
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001070 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001071 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001072
Daniel Stone103db7f2012-05-08 17:17:55 +01001073 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001074 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001075 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001076 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001077 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001078 }
1079
Daniel Stone103db7f2012-05-08 17:17:55 +01001080 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001081 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001082 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001083 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001084 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001085 }
1086
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001087 shsurf->client->send_configure(shsurf->surface,
1088 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001089}
1090
1091static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001092send_configure(struct weston_surface *surface,
1093 uint32_t edges, int32_t width, int32_t height)
1094{
1095 struct shell_surface *shsurf = get_shell_surface(surface);
1096
1097 wl_shell_surface_send_configure(&shsurf->resource,
1098 edges, width, height);
1099}
1100
1101static const struct weston_shell_client shell_client = {
1102 send_configure
1103};
1104
1105static void
Scott Moreau447013d2012-02-18 05:05:29 -07001106resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001107 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001108{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001109 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001110 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001111 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001112
Daniel Stone4dbadb12012-05-30 16:31:51 +01001113 if (pointer->button_count == 0 &&
1114 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001115 shell_grab_finish(&resize->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001116 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001117 free(grab);
1118 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001119}
1120
Scott Moreau447013d2012-02-18 05:05:29 -07001121static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001122 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001123 resize_grab_motion,
1124 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001125};
1126
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001127static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001128surface_resize(struct shell_surface *shsurf,
1129 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001130{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001131 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001132
Alex Wu4539b082012-03-01 12:57:46 +08001133 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1134 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001135
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001136 if (edges == 0 || edges > 15 ||
1137 (edges & 3) == 3 || (edges & 12) == 12)
1138 return 0;
1139
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001140 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001141 if (!resize)
1142 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001143
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001144 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
1145
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001146 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001147 resize->width = shsurf->surface->geometry.width;
1148 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001149
Daniel Stone37816df2012-05-16 18:45:18 +01001150 wl_pointer_start_grab(ws->seat.pointer, &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001151
Daniel Stone37816df2012-05-16 18:45:18 +01001152 wl_pointer_set_focus(ws->seat.pointer, NULL,
1153 wl_fixed_from_int(0),
1154 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001155
1156 return 0;
1157}
1158
1159static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001160shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001161 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001162 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001163{
Daniel Stone37816df2012-05-16 18:45:18 +01001164 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001165 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001166
Alex Wu4539b082012-03-01 12:57:46 +08001167 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1168 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001169
Daniel Stone37816df2012-05-16 18:45:18 +01001170 if (ws->seat.pointer->button_count == 0 ||
1171 ws->seat.pointer->grab_serial != serial ||
1172 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001173 return;
1174
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001175 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001176 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001177}
1178
Juan Zhao96879df2012-02-07 08:45:41 +08001179static struct weston_output *
1180get_default_output(struct weston_compositor *compositor)
1181{
1182 return container_of(compositor->output_list.next,
1183 struct weston_output, link);
1184}
1185
Alex Wu4539b082012-03-01 12:57:46 +08001186static void
1187shell_unset_fullscreen(struct shell_surface *shsurf)
1188{
1189 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001190 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1191 shell_surface_is_top_fullscreen(shsurf)) {
1192 weston_output_switch_mode(shsurf->fullscreen_output,
1193 shsurf->fullscreen_output->origin);
1194 }
Alex Wu4539b082012-03-01 12:57:46 +08001195 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1196 shsurf->fullscreen.framerate = 0;
1197 wl_list_remove(&shsurf->fullscreen.transform.link);
1198 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001199 if (shsurf->fullscreen.black_surface)
1200 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001201 shsurf->fullscreen.black_surface = NULL;
1202 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001203 weston_surface_set_position(shsurf->surface,
1204 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001205 if (shsurf->saved_rotation_valid) {
1206 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1207 &shsurf->rotation.transform.link);
1208 shsurf->saved_rotation_valid = false;
1209 }
Alex Wu4539b082012-03-01 12:57:46 +08001210}
1211
Pekka Paalanen98262232011-12-01 10:42:22 +02001212static int
1213reset_shell_surface_type(struct shell_surface *surface)
1214{
1215 switch (surface->type) {
1216 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001217 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001218 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001219 case SHELL_SURFACE_MAXIMIZED:
1220 surface->output = get_default_output(surface->surface->compositor);
1221 weston_surface_set_position(surface->surface,
1222 surface->saved_x,
1223 surface->saved_y);
1224 break;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02001225 case SHELL_SURFACE_INPUT_PANEL:
Pekka Paalanen98262232011-12-01 10:42:22 +02001226 wl_list_remove(&surface->link);
1227 wl_list_init(&surface->link);
1228 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001229 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +02001230 wl_resource_post_error(&surface->resource,
1231 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +02001232 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +02001233 return -1;
1234 case SHELL_SURFACE_NONE:
1235 case SHELL_SURFACE_TOPLEVEL:
1236 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001237 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001238 break;
1239 }
1240
1241 surface->type = SHELL_SURFACE_NONE;
1242 return 0;
1243}
1244
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001245static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001246set_surface_type(struct shell_surface *shsurf)
1247{
1248 struct weston_surface *surface = shsurf->surface;
1249 struct shell_surface *pshsurf = shsurf->parent;
1250 struct weston_surface *pes;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001251
1252 reset_shell_surface_type(shsurf);
1253
1254 shsurf->type = shsurf->next_type;
1255 shsurf->next_type = SHELL_SURFACE_NONE;
1256
1257 switch (shsurf->type) {
1258 case SHELL_SURFACE_TOPLEVEL:
1259 break;
1260 case SHELL_SURFACE_TRANSIENT:
1261 pes = pshsurf->surface;
1262 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001263 pes->geometry.x + shsurf->transient.x,
1264 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001265 break;
1266
1267 case SHELL_SURFACE_MAXIMIZED:
1268 shsurf->saved_x = surface->geometry.x;
1269 shsurf->saved_y = surface->geometry.y;
1270 shsurf->saved_position_valid = true;
1271 break;
1272
1273 case SHELL_SURFACE_FULLSCREEN:
1274 shsurf->saved_x = surface->geometry.x;
1275 shsurf->saved_y = surface->geometry.y;
1276 shsurf->saved_position_valid = true;
1277
1278 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1279 wl_list_remove(&shsurf->rotation.transform.link);
1280 wl_list_init(&shsurf->rotation.transform.link);
1281 shsurf->surface->geometry.dirty = 1;
1282 shsurf->saved_rotation_valid = true;
1283 }
1284 break;
1285
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001286 default:
1287 break;
1288 }
1289}
1290
1291static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001292set_toplevel(struct shell_surface *shsurf)
1293{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001294 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001295}
1296
1297static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001298shell_surface_set_toplevel(struct wl_client *client,
1299 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001300{
Pekka Paalanen98262232011-12-01 10:42:22 +02001301 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001302
Tiago Vignattibc052c92012-04-19 16:18:18 +03001303 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001304}
1305
1306static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001307set_transient(struct shell_surface *shsurf,
1308 struct shell_surface *pshsurf, int x, int y, uint32_t flags)
1309{
1310 /* assign to parents output */
1311 shsurf->parent = pshsurf;
1312 shsurf->transient.x = x;
1313 shsurf->transient.y = y;
1314 shsurf->transient.flags = flags;
1315 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1316}
1317
1318static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001319shell_surface_set_transient(struct wl_client *client,
1320 struct wl_resource *resource,
1321 struct wl_resource *parent_resource,
1322 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001323{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001324 struct shell_surface *shsurf = resource->data;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001325 struct shell_surface *pshsurf = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001326
Tiago Vignatti491bac12012-05-18 16:37:43 -04001327 set_transient(shsurf, pshsurf, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001328}
1329
Tiago Vignattibe143262012-04-16 17:31:41 +03001330static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001331shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001332{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001333 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001334}
1335
1336static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001337get_output_panel_height(struct desktop_shell *shell,
1338 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001339{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001340 struct weston_surface *surface;
Juan Zhao96879df2012-02-07 08:45:41 +08001341 int panel_height = 0;
1342
1343 if (!output)
1344 return 0;
1345
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001346 wl_list_for_each(surface, &shell->panel_layer.surface_list, link) {
1347 if (surface->output == output) {
1348 panel_height = surface->geometry.height;
Juan Zhao96879df2012-02-07 08:45:41 +08001349 break;
1350 }
1351 }
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001352
Juan Zhao96879df2012-02-07 08:45:41 +08001353 return panel_height;
1354}
1355
1356static void
1357shell_surface_set_maximized(struct wl_client *client,
1358 struct wl_resource *resource,
1359 struct wl_resource *output_resource )
1360{
1361 struct shell_surface *shsurf = resource->data;
1362 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001363 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001364 uint32_t edges = 0, panel_height = 0;
1365
1366 /* get the default output, if the client set it as NULL
1367 check whether the ouput is available */
1368 if (output_resource)
1369 shsurf->output = output_resource->data;
1370 else
1371 shsurf->output = get_default_output(es->compositor);
1372
Tiago Vignattibe143262012-04-16 17:31:41 +03001373 shell = shell_surface_get_shell(shsurf);
1374 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001375 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001376
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001377 shsurf->client->send_configure(shsurf->surface, edges,
1378 es->output->current->width,
1379 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001380
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001381 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001382}
1383
Alex Wu21858432012-04-01 20:13:08 +08001384static void
1385black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1386
Alex Wu4539b082012-03-01 12:57:46 +08001387static struct weston_surface *
1388create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001389 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001390 GLfloat x, GLfloat y, int w, int h)
1391{
1392 struct weston_surface *surface = NULL;
1393
1394 surface = weston_surface_create(ec);
1395 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001396 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001397 return NULL;
1398 }
1399
Alex Wu21858432012-04-01 20:13:08 +08001400 surface->configure = black_surface_configure;
1401 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001402 weston_surface_configure(surface, x, y, w, h);
1403 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1404 return surface;
1405}
1406
1407/* Create black surface and append it to the associated fullscreen surface.
1408 * Handle size dismatch and positioning according to the method. */
1409static void
1410shell_configure_fullscreen(struct shell_surface *shsurf)
1411{
1412 struct weston_output *output = shsurf->fullscreen_output;
1413 struct weston_surface *surface = shsurf->surface;
1414 struct weston_matrix *matrix;
1415 float scale;
1416
1417 center_on_output(surface, output);
1418
1419 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001420 shsurf->fullscreen.black_surface =
1421 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001422 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001423 output->x, output->y,
1424 output->current->width,
1425 output->current->height);
1426
1427 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1428 wl_list_insert(&surface->layer_link,
1429 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001430 shsurf->fullscreen.black_surface->output = output;
1431
1432 switch (shsurf->fullscreen.type) {
1433 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1434 break;
1435 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1436 matrix = &shsurf->fullscreen.transform.matrix;
1437 weston_matrix_init(matrix);
1438 scale = (float)output->current->width/(float)surface->geometry.width;
1439 weston_matrix_scale(matrix, scale, scale, 1);
1440 wl_list_remove(&shsurf->fullscreen.transform.link);
1441 wl_list_insert(surface->geometry.transformation_list.prev,
1442 &shsurf->fullscreen.transform.link);
1443 weston_surface_set_position(surface, output->x, output->y);
1444 break;
1445 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001446 if (shell_surface_is_top_fullscreen(shsurf)) {
1447 struct weston_mode mode = {0,
1448 surface->geometry.width,
1449 surface->geometry.height,
1450 shsurf->fullscreen.framerate};
1451
1452 if (weston_output_switch_mode(output, &mode) == 0) {
1453 weston_surface_configure(shsurf->fullscreen.black_surface,
1454 output->x, output->y,
1455 output->current->width,
1456 output->current->height);
1457 weston_surface_set_position(surface, output->x, output->y);
1458 break;
1459 }
1460 }
Alex Wu4539b082012-03-01 12:57:46 +08001461 break;
1462 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1463 break;
1464 default:
1465 break;
1466 }
1467}
1468
1469/* make the fullscreen and black surface at the top */
1470static void
1471shell_stack_fullscreen(struct shell_surface *shsurf)
1472{
Alex Wubd3354b2012-04-17 17:20:49 +08001473 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001474 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001475 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001476
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001477 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001478 wl_list_insert(&shell->fullscreen_layer.surface_list,
1479 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001480 weston_surface_damage(surface);
1481
1482 if (!shsurf->fullscreen.black_surface)
1483 shsurf->fullscreen.black_surface =
1484 create_black_surface(surface->compositor,
1485 surface,
1486 output->x, output->y,
1487 output->current->width,
1488 output->current->height);
1489
1490 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001491 wl_list_insert(&surface->layer_link,
1492 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001493 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001494}
1495
1496static void
1497shell_map_fullscreen(struct shell_surface *shsurf)
1498{
Alex Wu4539b082012-03-01 12:57:46 +08001499 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001500 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001501}
1502
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001503static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001504shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001505 struct wl_resource *resource,
1506 uint32_t method,
1507 uint32_t framerate,
1508 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001509{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001510 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001511 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001512
1513 if (output_resource)
1514 shsurf->output = output_resource->data;
1515 else
1516 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001517
Alex Wu4539b082012-03-01 12:57:46 +08001518 shsurf->fullscreen_output = shsurf->output;
1519 shsurf->fullscreen.type = method;
1520 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001521 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001522
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001523 shsurf->client->send_configure(shsurf->surface, 0,
1524 shsurf->output->current->width,
1525 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001526}
1527
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001528static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001529popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001530 struct wl_surface *surface,
1531 wl_fixed_t x,
1532 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001533{
Daniel Stone37816df2012-05-16 18:45:18 +01001534 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001535 struct shell_surface *priv =
1536 container_of(grab, struct shell_surface, popup.grab);
1537 struct wl_client *client = priv->surface->surface.resource.client;
1538
Pekka Paalanencb108432012-01-19 16:25:40 +02001539 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001540 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001541 grab->focus = surface;
1542 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001543 wl_pointer_set_focus(pointer, NULL,
1544 wl_fixed_from_int(0),
1545 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001546 grab->focus = NULL;
1547 }
1548}
1549
1550static void
Scott Moreau447013d2012-02-18 05:05:29 -07001551popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001552 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001553{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001554 struct wl_resource *resource;
1555
Daniel Stone37816df2012-05-16 18:45:18 +01001556 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001557 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001558 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001559}
1560
1561static void
Scott Moreau447013d2012-02-18 05:05:29 -07001562popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001563 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001564{
1565 struct wl_resource *resource;
1566 struct shell_surface *shsurf =
1567 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001568 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001569 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001570 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001571
Daniel Stone37816df2012-05-16 18:45:18 +01001572 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001573 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001574 display = wl_client_get_display(resource->client);
1575 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001576 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001577 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001578 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001579 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001580 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001581 wl_pointer_end_grab(grab->pointer);
1582 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001583 }
1584
Daniel Stone4dbadb12012-05-30 16:31:51 +01001585 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001586 shsurf->popup.initial_up = 1;
1587}
1588
Scott Moreau447013d2012-02-18 05:05:29 -07001589static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001590 popup_grab_focus,
1591 popup_grab_motion,
1592 popup_grab_button,
1593};
1594
1595static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001596shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001597{
Daniel Stone37816df2012-05-16 18:45:18 +01001598 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001599 struct weston_surface *es = shsurf->surface;
1600 struct weston_surface *parent = shsurf->parent->surface;
1601
1602 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001603 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001604
Pekka Paalanen938269a2012-02-07 14:19:01 +02001605 weston_surface_update_transform(parent);
1606 if (parent->transform.enabled) {
1607 shsurf->popup.parent_transform.matrix =
1608 parent->transform.matrix;
1609 } else {
1610 /* construct x, y translation matrix */
1611 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1612 shsurf->popup.parent_transform.matrix.d[12] =
1613 parent->geometry.x;
1614 shsurf->popup.parent_transform.matrix.d[13] =
1615 parent->geometry.y;
1616 }
1617 wl_list_insert(es->geometry.transformation_list.prev,
1618 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001619 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001620
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001621 shsurf->popup.initial_up = 0;
1622
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001623 /* We don't require the grab to still be active, but if another
1624 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001625 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1626 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001627 } else {
1628 wl_shell_surface_send_popup_done(&shsurf->resource);
1629 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001630}
1631
1632static void
1633shell_surface_set_popup(struct wl_client *client,
1634 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001635 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001636 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001637 struct wl_resource *parent_resource,
1638 int32_t x, int32_t y, uint32_t flags)
1639{
1640 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001641
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001642 shsurf->type = SHELL_SURFACE_POPUP;
1643 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001644 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001645 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001646 shsurf->popup.x = x;
1647 shsurf->popup.y = y;
1648}
1649
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001650static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001651 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001652 shell_surface_move,
1653 shell_surface_resize,
1654 shell_surface_set_toplevel,
1655 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001656 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001657 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001658 shell_surface_set_maximized,
1659 shell_surface_set_title,
1660 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001661};
1662
1663static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001664destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001665{
Daniel Stone37816df2012-05-16 18:45:18 +01001666 if (shsurf->popup.grab.pointer)
1667 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001668
Alex Wubd3354b2012-04-17 17:20:49 +08001669 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1670 shell_surface_is_top_fullscreen(shsurf)) {
1671 weston_output_switch_mode(shsurf->fullscreen_output,
1672 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001673 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001674
Alex Wuaa08e2d2012-03-05 11:01:40 +08001675 if (shsurf->fullscreen.black_surface)
1676 weston_surface_destroy(shsurf->fullscreen.black_surface);
1677
Alex Wubd3354b2012-04-17 17:20:49 +08001678 /* As destroy_resource() use wl_list_for_each_safe(),
1679 * we can always remove the listener.
1680 */
1681 wl_list_remove(&shsurf->surface_destroy_listener.link);
1682 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001683 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001684
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001685 wl_list_remove(&shsurf->link);
1686 free(shsurf);
1687}
1688
1689static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001690shell_destroy_shell_surface(struct wl_resource *resource)
1691{
1692 struct shell_surface *shsurf = resource->data;
1693
1694 destroy_shell_surface(shsurf);
1695}
1696
1697static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001698shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001699{
1700 struct shell_surface *shsurf = container_of(listener,
1701 struct shell_surface,
1702 surface_destroy_listener);
1703
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001704 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001705 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001706 } else {
1707 wl_signal_emit(&shsurf->resource.destroy_signal,
1708 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001709 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001710 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001711}
1712
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001713static void
1714shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1715
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001716static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001717get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001718{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001719 if (surface->configure == shell_surface_configure)
1720 return surface->private;
1721 else
1722 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001723}
1724
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001725static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001726create_shell_surface(void *shell, struct weston_surface *surface,
1727 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001728{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001729 struct shell_surface *shsurf;
1730
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001731 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001732 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001733 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001734 }
1735
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001736 shsurf = calloc(1, sizeof *shsurf);
1737 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001738 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001739 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001740 }
1741
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001742 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001743 surface->private = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001744 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001745
Tiago Vignattibc052c92012-04-19 16:18:18 +03001746 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001747 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001748 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001749 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001750 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001751 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1752 shsurf->fullscreen.framerate = 0;
1753 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001754 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001755 wl_list_init(&shsurf->fullscreen.transform.link);
1756
Tiago Vignattibc052c92012-04-19 16:18:18 +03001757 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001758 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1759 wl_signal_add(&surface->surface.resource.destroy_signal,
1760 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001761
1762 /* init link so its safe to always remove it in destroy_shell_surface */
1763 wl_list_init(&shsurf->link);
1764
Pekka Paalanen460099f2012-01-20 16:48:25 +02001765 /* empty when not in use */
1766 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001767 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001768
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001769 wl_list_init(&shsurf->workspace_transform.link);
1770
Pekka Paalanen98262232011-12-01 10:42:22 +02001771 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001772 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001773
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001774 shsurf->client = client;
1775
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001776 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001777}
1778
1779static void
1780shell_get_shell_surface(struct wl_client *client,
1781 struct wl_resource *resource,
1782 uint32_t id,
1783 struct wl_resource *surface_resource)
1784{
1785 struct weston_surface *surface = surface_resource->data;
1786 struct desktop_shell *shell = resource->data;
1787 struct shell_surface *shsurf;
1788
1789 if (get_shell_surface(surface)) {
1790 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001791 WL_DISPLAY_ERROR_INVALID_OBJECT,
1792 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001793 return;
1794 }
1795
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001796 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001797 if (!shsurf) {
1798 wl_resource_post_error(surface_resource,
1799 WL_DISPLAY_ERROR_INVALID_OBJECT,
1800 "surface->configure already set");
1801 return;
1802 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001803
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001804 shsurf->resource.destroy = shell_destroy_shell_surface;
1805 shsurf->resource.object.id = id;
1806 shsurf->resource.object.interface = &wl_shell_surface_interface;
1807 shsurf->resource.object.implementation =
1808 (void (**)(void)) &shell_surface_implementation;
1809 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001810
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001811 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001812}
1813
1814static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001815 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001816};
1817
Kristian Høgsberg07937562011-04-12 17:25:42 -04001818static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001819handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001820{
1821 proc->pid = 0;
1822}
1823
1824static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001825launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001826{
1827 if (shell->screensaver.binding)
1828 return;
1829
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001830 if (!shell->screensaver.path)
1831 return;
1832
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001833 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001834 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001835 return;
1836 }
1837
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001838 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001839 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001840 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001841 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001842}
1843
1844static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001845terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001846{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001847 if (shell->screensaver.process.pid == 0)
1848 return;
1849
1850 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001851}
1852
1853static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001854show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001855{
1856 struct wl_list *list;
1857
1858 if (shell->lock_surface)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001859 list = &shell->lock_surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001860 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001861 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001862
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001863 wl_list_remove(&surface->surface->layer_link);
1864 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001865 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001866 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001867}
1868
1869static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001870hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001871{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001872 wl_list_remove(&surface->surface->layer_link);
1873 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001874 surface->surface->output = NULL;
1875}
1876
1877static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02001878show_input_panel(struct desktop_shell *shell, struct shell_surface *surface)
1879{
Kristian Høgsberg1ce6a2a2012-06-21 22:34:39 -04001880 if (weston_surface_is_mapped(surface->surface))
1881 return;
1882
Jan Arne Petersen42feced2012-06-21 21:52:17 +02001883 wl_list_remove(&surface->surface->layer_link);
1884 wl_list_insert(&shell->panel_layer.surface_list, &surface->surface->layer_link);
1885 surface->surface->output = surface->output;
1886 weston_surface_damage(surface->surface);
Kristian Høgsberg414bd422012-06-21 22:07:30 -04001887
Kristian Høgsberg1ce6a2a2012-06-21 22:34:39 -04001888 weston_slide_run(surface->surface,
1889 surface->surface->geometry.height, 0,
1890 NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02001891}
1892
1893static void
1894hide_input_panel(struct desktop_shell *shell, struct shell_surface *surface)
1895{
1896 weston_surface_damage_below(surface->surface);
1897 wl_list_remove(&surface->surface->layer_link);
1898 wl_list_init(&surface->surface->layer_link);
1899 surface->surface->output = NULL;
1900
1901 weston_compositor_schedule_repaint(surface->surface->compositor);
1902}
1903
1904static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001905configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001906{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001907 struct weston_surface *s, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001908
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001909 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1910 if (s->output == es->output && s != es) {
1911 weston_surface_unmap(s);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001912 s->configure = NULL;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001913 }
1914 }
1915
1916 weston_surface_configure(es, es->output->x, es->output->y,
1917 es->buffer->width, es->buffer->height);
1918
1919 if (wl_list_empty(&es->layer_link)) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001920 wl_list_insert(&layer->surface_list, &es->layer_link);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001921 weston_surface_assign_output(es);
1922 }
1923}
1924
1925static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001926background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1927{
1928 struct desktop_shell *shell = es->private;
1929
1930 configure_static_surface(es, &shell->background_layer);
1931}
1932
1933static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001934desktop_shell_set_background(struct wl_client *client,
1935 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001936 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001937 struct wl_resource *surface_resource)
1938{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001939 struct desktop_shell *shell = resource->data;
1940 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001941
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001942 if (surface->configure) {
1943 wl_resource_post_error(surface_resource,
1944 WL_DISPLAY_ERROR_INVALID_OBJECT,
1945 "surface role already assigned");
1946 return;
1947 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001948
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001949 surface->configure = background_configure;
1950 surface->private = shell;
1951 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001952 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001953 surface_resource,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001954 surface->output->current->width,
1955 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001956}
1957
1958static void
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001959panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1960{
1961 struct desktop_shell *shell = es->private;
1962
1963 configure_static_surface(es, &shell->panel_layer);
1964}
1965
1966static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001967desktop_shell_set_panel(struct wl_client *client,
1968 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001969 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001970 struct wl_resource *surface_resource)
1971{
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001972 struct desktop_shell *shell = resource->data;
1973 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001974
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001975 if (surface->configure) {
1976 wl_resource_post_error(surface_resource,
1977 WL_DISPLAY_ERROR_INVALID_OBJECT,
1978 "surface role already assigned");
1979 return;
1980 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001981
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001982 surface->configure = panel_configure;
1983 surface->private = shell;
1984 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001985 desktop_shell_send_configure(resource, 0,
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001986 surface_resource,
1987 surface->output->current->width,
1988 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001989}
1990
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001991static void
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04001992lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1993{
1994 struct desktop_shell *shell = surface->private;
1995
1996 center_on_output(surface, get_default_output(shell->compositor));
1997
1998 if (!weston_surface_is_mapped(surface)) {
1999 wl_list_insert(&shell->lock_layer.surface_list,
2000 &surface->layer_link);
2001 weston_surface_assign_output(surface);
2002 weston_compositor_wake(shell->compositor);
2003 }
2004}
2005
2006static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002007handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002008{
Tiago Vignattibe143262012-04-16 17:31:41 +03002009 struct desktop_shell *shell =
2010 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002011
Martin Minarik6d118362012-06-07 18:01:59 +02002012 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002013 shell->lock_surface = NULL;
2014}
2015
2016static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002017desktop_shell_set_lock_surface(struct wl_client *client,
2018 struct wl_resource *resource,
2019 struct wl_resource *surface_resource)
2020{
Tiago Vignattibe143262012-04-16 17:31:41 +03002021 struct desktop_shell *shell = resource->data;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002022 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002023
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002024 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002025
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002026 if (!shell->locked)
2027 return;
2028
Pekka Paalanen98262232011-12-01 10:42:22 +02002029 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002030
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002031 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2032 wl_signal_add(&surface_resource->destroy_signal,
2033 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002034
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002035 surface->configure = lock_surface_configure;
2036 surface->private = shell;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002037}
2038
2039static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002040resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002041{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002042 struct shell_surface *tmp;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002043 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002044
2045 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
2046 hide_screensaver(shell, tmp);
2047
2048 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002049
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002050 wl_list_remove(&shell->lock_layer.link);
2051 wl_list_insert(&shell->compositor->cursor_layer.link,
2052 &shell->fullscreen_layer.link);
2053 wl_list_insert(&shell->fullscreen_layer.link,
2054 &shell->panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002055 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002056
Jonas Ådahl04769742012-06-13 00:01:24 +02002057 pop_focus_state(shell, get_current_workspace(shell));
2058
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002059 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002060 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002061 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002062 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002063}
2064
2065static void
2066desktop_shell_unlock(struct wl_client *client,
2067 struct wl_resource *resource)
2068{
Tiago Vignattibe143262012-04-16 17:31:41 +03002069 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002070
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002071 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002072
2073 if (shell->locked)
2074 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002075}
2076
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002077static void
2078desktop_shell_set_busy_surface(struct wl_client *client,
2079 struct wl_resource *resource,
2080 struct wl_resource *surface_resource)
2081{
2082 struct desktop_shell *shell = resource->data;
2083
2084 shell->busy_surface = surface_resource->data;
2085}
2086
Kristian Høgsberg75840622011-09-06 13:48:16 -04002087static const struct desktop_shell_interface desktop_shell_implementation = {
2088 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002089 desktop_shell_set_panel,
2090 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002091 desktop_shell_unlock,
2092 desktop_shell_set_busy_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002093};
2094
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002095static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002096get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002097{
2098 struct shell_surface *shsurf;
2099
2100 shsurf = get_shell_surface(surface);
2101 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002102 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002103 return shsurf->type;
2104}
2105
Kristian Høgsberg75840622011-09-06 13:48:16 -04002106static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002107move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002108{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002109 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002110 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002111 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002112
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002113 if (surface == NULL)
2114 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002115
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002116 shsurf = get_shell_surface(surface);
2117 if (shsurf == NULL)
2118 return;
2119
2120 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002121 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02002122 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberga4a4de42012-06-21 16:46:14 -04002123 case SHELL_SURFACE_INPUT_PANEL:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002124 return;
2125 default:
2126 break;
2127 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04002128
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002129 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002130}
2131
2132static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002133resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002134{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002135 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002136 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002137 uint32_t edges = 0;
2138 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002139 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002140
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002141 if (surface == NULL)
2142 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002143
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002144 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002145 if (!shsurf)
2146 return;
2147
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002148 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002149 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02002150 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberga4a4de42012-06-21 16:46:14 -04002151 case SHELL_SURFACE_INPUT_PANEL:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002152 return;
2153 default:
2154 break;
2155 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04002156
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002157 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002158 wl_fixed_to_int(seat->pointer->grab_x),
2159 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002160 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002161
Pekka Paalanen60921e52012-01-25 15:55:43 +02002162 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002163 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002164 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002165 edges |= 0;
2166 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002167 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002168
Pekka Paalanen60921e52012-01-25 15:55:43 +02002169 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002170 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002171 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002172 edges |= 0;
2173 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002174 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002175
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002176 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002177}
2178
2179static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002180surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002181 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002182{
Scott Moreau02709af2012-05-22 01:54:10 -06002183 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002184 struct shell_surface *shsurf;
2185 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002186 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002187
2188 if (surface == NULL)
2189 return;
2190
2191 shsurf = get_shell_surface(surface);
2192 if (!shsurf)
2193 return;
2194
2195 switch (shsurf->type) {
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002196 case SHELL_SURFACE_SCREENSAVER:
2197 return;
2198 default:
2199 break;
2200 }
2201
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002202 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002203
Scott Moreau02709af2012-05-22 01:54:10 -06002204 if (surface->alpha > 1.0)
2205 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002206 if (surface->alpha < step)
2207 surface->alpha = step;
2208
2209 surface->geometry.dirty = 1;
2210 weston_surface_damage(surface);
2211}
2212
2213static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002214do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002215 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002216{
Daniel Stone37816df2012-05-16 18:45:18 +01002217 struct weston_seat *ws = (struct weston_seat *) seat;
2218 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002219 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002220 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002221
2222 wl_list_for_each(output, &compositor->output_list, link) {
2223 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002224 wl_fixed_to_double(seat->pointer->x),
2225 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002226 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002227 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002228 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002229 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002230 increment = -output->zoom.increment;
2231 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002232 increment = output->zoom.increment *
2233 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002234 else
2235 increment = 0;
2236
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002237 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002238
Scott Moreaue6603982012-06-11 13:07:51 -06002239 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002240 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002241 else if (output->zoom.level > output->zoom.max_level)
2242 output->zoom.level = output->zoom.max_level;
2243 else
2244 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002245
Scott Moreaue6603982012-06-11 13:07:51 -06002246 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002247
Scott Moreau8dacaab2012-06-17 18:10:58 -06002248 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002249 }
2250 }
2251}
2252
Scott Moreauccbf29d2012-02-22 14:21:41 -07002253static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002254zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002255 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002256{
2257 do_zoom(seat, time, 0, axis, value);
2258}
2259
2260static void
2261zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2262 void *data)
2263{
2264 do_zoom(seat, time, key, 0, 0);
2265}
2266
2267static void
2268terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2269 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002270{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002271 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002272
Daniel Stone325fc2d2012-05-30 16:31:58 +01002273 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002274}
2275
2276static void
Scott Moreau447013d2012-02-18 05:05:29 -07002277rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002278 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002279{
2280 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002281 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002282 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002283 struct shell_surface *shsurf = rotate->base.shsurf;
2284 struct weston_surface *surface;
2285 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2286
2287 if (!shsurf)
2288 return;
2289
2290 surface = shsurf->surface;
2291
2292 cx = 0.5f * surface->geometry.width;
2293 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002294
Daniel Stone37816df2012-05-16 18:45:18 +01002295 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2296 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002297 r = sqrtf(dx * dx + dy * dy);
2298
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002299 wl_list_remove(&shsurf->rotation.transform.link);
2300 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002301
2302 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002303 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002304 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002305
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002306 weston_matrix_init(&rotate->rotation);
2307 rotate->rotation.d[0] = dx / r;
2308 rotate->rotation.d[4] = -dy / r;
2309 rotate->rotation.d[1] = -rotate->rotation.d[4];
2310 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002311
2312 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002313 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002314 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002315 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002316 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002317
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002318 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002319 &shsurf->surface->geometry.transformation_list,
2320 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002321 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002322 wl_list_init(&shsurf->rotation.transform.link);
2323 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002324 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002325 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002326
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002327 /* We need to adjust the position of the surface
2328 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002329 cposx = surface->geometry.x + cx;
2330 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002331 dposx = rotate->center.x - cposx;
2332 dposy = rotate->center.y - cposy;
2333 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002334 weston_surface_set_position(surface,
2335 surface->geometry.x + dposx,
2336 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002337 }
2338
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002339 /* Repaint implies weston_surface_update_transform(), which
2340 * lazily applies the damage due to rotation update.
2341 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002342 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002343}
2344
2345static void
Scott Moreau447013d2012-02-18 05:05:29 -07002346rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002347 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002348{
2349 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002350 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002351 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002352 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002353 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002354
Daniel Stone4dbadb12012-05-30 16:31:51 +01002355 if (pointer->button_count == 0 &&
2356 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002357 if (shsurf)
2358 weston_matrix_multiply(&shsurf->rotation.rotation,
2359 &rotate->rotation);
2360 shell_grab_finish(&rotate->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002361 wl_pointer_end_grab(pointer);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002362 free(rotate);
2363 }
2364}
2365
Scott Moreau447013d2012-02-18 05:05:29 -07002366static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002367 noop_grab_focus,
2368 rotate_grab_motion,
2369 rotate_grab_button,
2370};
2371
2372static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002373rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002374 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002375{
2376 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002377 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002378 struct shell_surface *surface;
2379 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002380 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002381 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002382
2383 if (base_surface == NULL)
2384 return;
2385
2386 surface = get_shell_surface(base_surface);
2387 if (!surface)
2388 return;
2389
2390 switch (surface->type) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002391 case SHELL_SURFACE_FULLSCREEN:
2392 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberga4a4de42012-06-21 16:46:14 -04002393 case SHELL_SURFACE_INPUT_PANEL:
Pekka Paalanen460099f2012-01-20 16:48:25 +02002394 return;
2395 default:
2396 break;
2397 }
2398
Pekka Paalanen460099f2012-01-20 16:48:25 +02002399 rotate = malloc(sizeof *rotate);
2400 if (!rotate)
2401 return;
2402
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002403 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02002404
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002405 weston_surface_to_global_float(surface->surface,
2406 surface->surface->geometry.width / 2,
2407 surface->surface->geometry.height / 2,
2408 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002409
Daniel Stone37816df2012-05-16 18:45:18 +01002410 wl_pointer_start_grab(seat->pointer, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002411
Daniel Stone37816df2012-05-16 18:45:18 +01002412 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2413 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002414 r = sqrtf(dx * dx + dy * dy);
2415 if (r > 20.0f) {
2416 struct weston_matrix inverse;
2417
2418 weston_matrix_init(&inverse);
2419 inverse.d[0] = dx / r;
2420 inverse.d[4] = dy / r;
2421 inverse.d[1] = -inverse.d[4];
2422 inverse.d[5] = inverse.d[0];
2423 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002424
2425 weston_matrix_init(&rotate->rotation);
2426 rotate->rotation.d[0] = dx / r;
2427 rotate->rotation.d[4] = -dy / r;
2428 rotate->rotation.d[1] = -rotate->rotation.d[4];
2429 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002430 } else {
2431 weston_matrix_init(&surface->rotation.rotation);
2432 weston_matrix_init(&rotate->rotation);
2433 }
2434
Daniel Stone37816df2012-05-16 18:45:18 +01002435 wl_pointer_set_focus(seat->pointer, NULL,
2436 wl_fixed_from_int(0),
2437 wl_fixed_from_int(0));
Pekka Paalanen460099f2012-01-20 16:48:25 +02002438}
2439
2440static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002441lower_fullscreen_layer(struct desktop_shell *shell)
2442{
2443 struct workspace *ws;
2444 struct weston_surface *surface, *prev;
2445
2446 ws = get_current_workspace(shell);
2447 wl_list_for_each_reverse_safe(surface, prev,
2448 &shell->fullscreen_layer.surface_list,
2449 layer_link)
2450 weston_surface_restack(surface, &ws->layer.surface_list);
2451}
2452
2453static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002454activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002455 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002456{
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002457 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002458
Daniel Stone37816df2012-05-16 18:45:18 +01002459 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002460
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002461 switch (get_shell_surface_type(es)) {
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002462 case SHELL_SURFACE_INPUT_PANEL:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002463 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002464
Pekka Paalanen77346a62011-11-30 16:26:35 +02002465 case SHELL_SURFACE_SCREENSAVER:
2466 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002467 if (shell->lock_surface)
2468 weston_surface_restack(es,
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002469 &shell->lock_surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002470 break;
Alex Wu4539b082012-03-01 12:57:46 +08002471 case SHELL_SURFACE_FULLSCREEN:
2472 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002473 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002474 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002475 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002476 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002477 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002478 lower_fullscreen_layer(shell);
2479 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002480 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002481 }
2482}
2483
Alex Wu21858432012-04-01 20:13:08 +08002484/* no-op func for checking black surface */
2485static void
2486black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2487{
2488}
2489
2490static bool
2491is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2492{
2493 if (es->configure == black_surface_configure) {
2494 if (fs_surface)
2495 *fs_surface = (struct weston_surface *)es->private;
2496 return true;
2497 }
2498 return false;
2499}
2500
Kristian Høgsberg75840622011-09-06 13:48:16 -04002501static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002502click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002503 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002504{
Daniel Stone37816df2012-05-16 18:45:18 +01002505 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002506 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002507 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002508 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002509
Daniel Stone37816df2012-05-16 18:45:18 +01002510 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002511 if (!focus)
2512 return;
2513
Alex Wu21858432012-04-01 20:13:08 +08002514 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002515 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002516
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002517 switch (get_shell_surface_type(focus)) {
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002518 case SHELL_SURFACE_SCREENSAVER:
2519 case SHELL_SURFACE_INPUT_PANEL:
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002520 case SHELL_SURFACE_NONE:
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002521 return;
2522 default:
2523 break;
2524 }
2525
Daniel Stone325fc2d2012-05-30 16:31:58 +01002526 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002527 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002528}
2529
2530static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002531lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002532{
Tiago Vignattibe143262012-04-16 17:31:41 +03002533 struct desktop_shell *shell =
2534 container_of(listener, struct desktop_shell, lock_listener);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002535 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002536 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002537 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002538
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002539 if (shell->locked) {
2540 wl_list_for_each(output, &shell->compositor->output_list, link)
2541 /* TODO: find a way to jump to other DPMS levels */
2542 if (output->set_dpms)
2543 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002544 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002545 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002546
2547 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002548
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002549 /* Hide all surfaces by removing the fullscreen, panel and
2550 * toplevel layers. This way nothing else can show or receive
2551 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002552
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002553 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002554 wl_list_remove(&shell->fullscreen_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002555 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002556 wl_list_insert(&shell->compositor->cursor_layer.link,
2557 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002558
Pekka Paalanen77346a62011-11-30 16:26:35 +02002559 launch_screensaver(shell);
2560
2561 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
2562 show_screensaver(shell, shsurf);
2563
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002564 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02002565 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002566 weston_compositor_wake(shell->compositor);
2567 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002568 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002569
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002570 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002571 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002572
Jonas Ådahl04769742012-06-13 00:01:24 +02002573 /* stash keyboard foci in current workspace */
2574 push_focus_state(shell, get_current_workspace(shell));
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002575
2576 /* TODO: disable bindings that should not work while locked. */
2577
2578 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002579}
2580
2581static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002582unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002583{
Tiago Vignattibe143262012-04-16 17:31:41 +03002584 struct desktop_shell *shell =
2585 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002586
Pekka Paalanend81c2162011-11-16 13:47:34 +02002587 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002588 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002589 return;
2590 }
2591
2592 /* If desktop-shell client has gone away, unlock immediately. */
2593 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002594 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002595 return;
2596 }
2597
2598 if (shell->prepare_event_sent)
2599 return;
2600
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002601 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002602 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002603}
2604
2605static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002606show_input_panels(struct wl_listener *listener, void *data)
2607{
2608 struct desktop_shell *shell =
2609 container_of(listener, struct desktop_shell, show_input_panel_listener);
2610 struct shell_surface *priv;
2611
2612 wl_list_for_each(priv, &shell->input_panel.surfaces, link) {
2613 show_input_panel(shell, priv);
2614 }
2615}
2616
2617static void
2618hide_input_panels(struct wl_listener *listener, void *data)
2619{
2620 struct desktop_shell *shell =
2621 container_of(listener, struct desktop_shell, hide_input_panel_listener);
2622 struct shell_surface *priv;
2623
2624 wl_list_for_each(priv, &shell->input_panel.surfaces, link) {
2625 hide_input_panel(shell, priv);
2626 }
2627}
2628
2629static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002630center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002631{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002632 struct weston_mode *mode = output->current;
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002633 GLfloat x = (mode->width - surface->buffer->width) / 2;
2634 GLfloat y = (mode->height - surface->buffer->height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002635
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04002636 weston_surface_configure(surface, output->x + x, output->y + y,
2637 surface->buffer->width,
2638 surface->buffer->height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002639}
2640
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002641
2642static void
2643bottom_center_on_output(struct weston_surface *surface, struct weston_output *output)
2644{
2645 struct weston_mode *mode = output->current;
2646 GLfloat x = (mode->width - surface->geometry.width) / 2;
2647 GLfloat y = mode->height - surface->geometry.height;
2648
2649 weston_surface_set_position(surface, output->x + x, output->y + y);
2650}
2651
Pekka Paalanen77346a62011-11-30 16:26:35 +02002652static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002653map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002654 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002655{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002656 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002657 struct shell_surface *shsurf = get_shell_surface(surface);
2658 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002659 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002660 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002661 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002662 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002663
Pekka Paalanen60921e52012-01-25 15:55:43 +02002664 surface->geometry.width = width;
2665 surface->geometry.height = height;
2666 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002667
2668 /* initial positioning, see also configure() */
2669 switch (surface_type) {
2670 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002671 weston_surface_set_position(surface, 10 + random() % 400,
2672 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002673 break;
2674 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002675 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002676 break;
Alex Wu4539b082012-03-01 12:57:46 +08002677 case SHELL_SURFACE_FULLSCREEN:
2678 shell_map_fullscreen(shsurf);
2679 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002680 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002681 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002682 panel_height = get_output_panel_height(shell,surface->output);
2683 weston_surface_set_position(surface, surface->output->x,
2684 surface->output->y + panel_height);
2685 break;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002686 case SHELL_SURFACE_INPUT_PANEL:
2687 bottom_center_on_output(surface, get_default_output(compositor));
Kristian Høgsberg1ce6a2a2012-06-21 22:34:39 -04002688 /* Don't map the input panel here, wait for
2689 * show_input_panels signal. */
2690 return;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002691 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002692 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002693 case SHELL_SURFACE_NONE:
2694 weston_surface_set_position(surface,
2695 surface->geometry.x + sx,
2696 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002697 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002698 default:
2699 ;
2700 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002701
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002702 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002703 switch (surface_type) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002704 case SHELL_SURFACE_SCREENSAVER:
2705 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002706 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002707 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002708 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002709 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002710 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002711 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002712 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002713 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002714 case SHELL_SURFACE_POPUP:
2715 case SHELL_SURFACE_TRANSIENT:
2716 parent = shsurf->parent->surface;
2717 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2718 break;
Alex Wu4539b082012-03-01 12:57:46 +08002719 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002720 case SHELL_SURFACE_NONE:
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002721 case SHELL_SURFACE_INPUT_PANEL:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002722 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002723 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002724 ws = get_current_workspace(shell);
2725 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002726 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002727 }
2728
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002729 if (surface_type != SHELL_SURFACE_NONE) {
2730 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002731 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2732 surface->output = shsurf->output;
2733 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002734
Juan Zhao7bb92f02011-12-15 11:31:51 -05002735 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002736 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002737 if (shsurf->transient.flags ==
2738 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2739 break;
2740 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002741 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002742 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002743 if (!shell->locked) {
2744 wl_list_for_each(seat, &compositor->seat_list, link)
2745 activate(shell, surface, seat);
2746 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002747 break;
2748 default:
2749 break;
2750 }
2751
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002752 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002753 {
2754 switch (shell->win_animation_type) {
2755 case ANIMATION_FADE:
2756 weston_fade_run(surface, NULL, NULL);
2757 break;
2758 case ANIMATION_ZOOM:
2759 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2760 break;
2761 default:
2762 break;
2763 }
2764 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002765}
2766
2767static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002768configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002769 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002770{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002771 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2772 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002773
Pekka Paalanen77346a62011-11-30 16:26:35 +02002774 shsurf = get_shell_surface(surface);
2775 if (shsurf)
2776 surface_type = shsurf->type;
2777
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002778 surface->geometry.x = x;
2779 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002780 surface->geometry.width = width;
2781 surface->geometry.height = height;
2782 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002783
2784 switch (surface_type) {
2785 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002786 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002787 break;
Alex Wu4539b082012-03-01 12:57:46 +08002788 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002789 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002790 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002791 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002792 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002793 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002794 surface->geometry.x = surface->output->x;
2795 surface->geometry.y = surface->output->y +
2796 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002797 break;
Alex Wu4539b082012-03-01 12:57:46 +08002798 case SHELL_SURFACE_TOPLEVEL:
2799 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002800 default:
2801 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002802 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002803
Alex Wu4539b082012-03-01 12:57:46 +08002804 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002805 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002806 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002807
2808 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2809 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002810 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2811 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002812 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002813}
2814
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002815static void
2816shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2817{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002818 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002819 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002820 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002821
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002822 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002823 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002824 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002825 type_changed = 1;
2826 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002827
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002828 if (!weston_surface_is_mapped(es)) {
2829 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002830 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002831 es->geometry.width != es->buffer->width ||
2832 es->geometry.height != es->buffer->height) {
2833 GLfloat from_x, from_y;
2834 GLfloat to_x, to_y;
2835
2836 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2837 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2838 configure(shell, es,
2839 es->geometry.x + to_x - from_x,
2840 es->geometry.y + to_y - from_y,
2841 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002842 }
2843}
2844
Tiago Vignattibe143262012-04-16 17:31:41 +03002845static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002846
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002847static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002848desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002849{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002850 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002851 struct desktop_shell *shell =
2852 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002853
2854 shell->child.process.pid = 0;
2855 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002856
2857 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2858 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002859 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002860 shell->child.deathstamp = time;
2861 shell->child.deathcount = 0;
2862 }
2863
2864 shell->child.deathcount++;
2865 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002866 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002867 return;
2868 }
2869
Martin Minarik6d118362012-06-07 18:01:59 +02002870 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002871 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002872}
2873
2874static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002875launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002876{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002877 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002878
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002879 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002880 &shell->child.process,
2881 shell_exe,
2882 desktop_shell_sigchld);
2883
2884 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002885 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002886 return 0;
2887}
2888
2889static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002890bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2891{
Tiago Vignattibe143262012-04-16 17:31:41 +03002892 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002893
2894 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002895 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002896}
2897
Kristian Høgsberg75840622011-09-06 13:48:16 -04002898static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002899unbind_desktop_shell(struct wl_resource *resource)
2900{
Tiago Vignattibe143262012-04-16 17:31:41 +03002901 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002902
2903 if (shell->locked)
2904 resume_desktop(shell);
2905
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002906 shell->child.desktop_shell = NULL;
2907 shell->prepare_event_sent = false;
2908 free(resource);
2909}
2910
2911static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002912bind_desktop_shell(struct wl_client *client,
2913 void *data, uint32_t version, uint32_t id)
2914{
Tiago Vignattibe143262012-04-16 17:31:41 +03002915 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002916 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002917
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002918 resource = wl_client_add_object(client, &desktop_shell_interface,
2919 &desktop_shell_implementation,
2920 id, shell);
2921
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002922 if (client == shell->child.client) {
2923 resource->destroy = unbind_desktop_shell;
2924 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002925 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002926 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002927
2928 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2929 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002930 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002931}
2932
Pekka Paalanen6e168112011-11-24 11:34:05 +02002933static void
2934screensaver_set_surface(struct wl_client *client,
2935 struct wl_resource *resource,
2936 struct wl_resource *shell_surface_resource,
2937 struct wl_resource *output_resource)
2938{
Tiago Vignattibe143262012-04-16 17:31:41 +03002939 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002940 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002941 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002942
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002943 surface->next_type = SHELL_SURFACE_SCREENSAVER;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002944
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002945 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002946 surface->output = output;
2947 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002948}
2949
2950static const struct screensaver_interface screensaver_implementation = {
2951 screensaver_set_surface
2952};
2953
2954static void
2955unbind_screensaver(struct wl_resource *resource)
2956{
Tiago Vignattibe143262012-04-16 17:31:41 +03002957 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002958
Pekka Paalanen77346a62011-11-30 16:26:35 +02002959 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002960 free(resource);
2961}
2962
2963static void
2964bind_screensaver(struct wl_client *client,
2965 void *data, uint32_t version, uint32_t id)
2966{
Tiago Vignattibe143262012-04-16 17:31:41 +03002967 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002968 struct wl_resource *resource;
2969
2970 resource = wl_client_add_object(client, &screensaver_interface,
2971 &screensaver_implementation,
2972 id, shell);
2973
Pekka Paalanen77346a62011-11-30 16:26:35 +02002974 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002975 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002976 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002977 return;
2978 }
2979
2980 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2981 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002982 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002983}
2984
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002985static void
2986input_panel_set_surface(struct wl_client *client,
2987 struct wl_resource *resource,
2988 struct wl_resource *shell_surface_resource,
2989 struct wl_resource *output_resource)
2990{
2991 struct desktop_shell *shell = resource->data;
2992 struct shell_surface *surface = shell_surface_resource->data;
2993 struct weston_output *output = output_resource->data;
2994
2995 surface->next_type = SHELL_SURFACE_INPUT_PANEL;
2996
2997 surface->fullscreen_output = output;
2998 surface->output = output;
2999 wl_list_insert(shell->input_panel.surfaces.prev, &surface->link);
3000}
3001
3002static const struct input_panel_interface input_panel_implementation = {
3003 input_panel_set_surface
3004};
3005
3006static void
3007unbind_input_panel(struct wl_resource *resource)
3008{
3009 struct desktop_shell *shell = resource->data;
3010
3011 shell->input_panel.binding = NULL;
3012 free(resource);
3013}
3014
3015static void
3016bind_input_panel(struct wl_client *client,
3017 void *data, uint32_t version, uint32_t id)
3018{
3019 struct desktop_shell *shell = data;
3020 struct wl_resource *resource;
3021
3022 resource = wl_client_add_object(client, &input_panel_interface,
3023 &input_panel_implementation,
3024 id, shell);
3025
3026 if (shell->input_panel.binding == NULL) {
3027 resource->destroy = unbind_input_panel;
3028 shell->input_panel.binding = resource;
3029 return;
3030 }
3031
3032 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3033 "interface object already bound");
3034 wl_resource_destroy(resource);
3035}
3036
Kristian Høgsberg07045392012-02-19 18:52:44 -05003037struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003038 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003039 struct weston_surface *current;
3040 struct wl_listener listener;
3041 struct wl_keyboard_grab grab;
3042};
3043
3044static void
3045switcher_next(struct switcher *switcher)
3046{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003047 struct weston_surface *surface;
3048 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003049 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003050 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003051
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003052 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003053 switch (get_shell_surface_type(surface)) {
3054 case SHELL_SURFACE_TOPLEVEL:
3055 case SHELL_SURFACE_FULLSCREEN:
3056 case SHELL_SURFACE_MAXIMIZED:
3057 if (first == NULL)
3058 first = surface;
3059 if (prev == switcher->current)
3060 next = surface;
3061 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003062 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003063 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003064 weston_surface_damage(surface);
3065 break;
3066 default:
3067 break;
3068 }
Alex Wu1659daa2012-04-01 20:13:09 +08003069
3070 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003071 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003072 surface->geometry.dirty = 1;
3073 weston_surface_damage(surface);
3074 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003075 }
3076
3077 if (next == NULL)
3078 next = first;
3079
Alex Wu07b26062012-03-12 16:06:01 +08003080 if (next == NULL)
3081 return;
3082
Kristian Høgsberg07045392012-02-19 18:52:44 -05003083 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003084 wl_signal_add(&next->surface.resource.destroy_signal,
3085 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003086
3087 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003088 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003089
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003090 shsurf = get_shell_surface(switcher->current);
3091 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003092 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003093}
3094
3095static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003096switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003097{
3098 struct switcher *switcher =
3099 container_of(listener, struct switcher, listener);
3100
3101 switcher_next(switcher);
3102}
3103
3104static void
Daniel Stone351eb612012-05-31 15:27:47 -04003105switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003106{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003107 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003108 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003109 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003110
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003111 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003112 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003113 weston_surface_damage(surface);
3114 }
3115
Alex Wu07b26062012-03-12 16:06:01 +08003116 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003117 activate(switcher->shell, switcher->current,
3118 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003119 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003120 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003121 free(switcher);
3122}
3123
3124static void
3125switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003126 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003127{
3128 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003129 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003130
Daniel Stonec9785ea2012-05-30 16:31:52 +01003131 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003132 switcher_next(switcher);
3133}
3134
3135static void
3136switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3137 uint32_t mods_depressed, uint32_t mods_latched,
3138 uint32_t mods_locked, uint32_t group)
3139{
3140 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003141 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003142
Daniel Stone351eb612012-05-31 15:27:47 -04003143 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3144 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003145}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003146
3147static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003148 switcher_key,
3149 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003150};
3151
3152static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003153switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3154 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003155{
Tiago Vignattibe143262012-04-16 17:31:41 +03003156 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003157 struct switcher *switcher;
3158
3159 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003160 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003161 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003162 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003163 wl_list_init(&switcher->listener.link);
3164
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003165 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003166 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003167 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3168 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003169 switcher_next(switcher);
3170}
3171
Pekka Paalanen3c647232011-12-22 13:43:43 +02003172static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003173backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3174 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003175{
3176 struct weston_compositor *compositor = data;
3177 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003178 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003179
3180 /* TODO: we're limiting to simple use cases, where we assume just
3181 * control on the primary display. We'd have to extend later if we
3182 * ever get support for setting backlights on random desktop LCD
3183 * panels though */
3184 output = get_default_output(compositor);
3185 if (!output)
3186 return;
3187
3188 if (!output->set_backlight)
3189 return;
3190
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003191 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3192 backlight_new = output->backlight_current - 25;
3193 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3194 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003195
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003196 if (backlight_new < 5)
3197 backlight_new = 5;
3198 if (backlight_new > 255)
3199 backlight_new = 255;
3200
3201 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003202 output->set_backlight(output, output->backlight_current);
3203}
3204
3205static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003206debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3207 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003208{
Tiago Vignattibe143262012-04-16 17:31:41 +03003209 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003210 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003211 struct weston_surface *surface;
3212
3213 if (shell->debug_repaint_surface) {
3214 weston_surface_destroy(shell->debug_repaint_surface);
3215 shell->debug_repaint_surface = NULL;
3216 } else {
3217 surface = weston_surface_create(compositor);
3218 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3219 weston_surface_configure(surface, 0, 0, 8192, 8192);
3220 wl_list_insert(&compositor->fade_layer.surface_list,
3221 &surface->layer_link);
3222 weston_surface_assign_output(surface);
3223 pixman_region32_init(&surface->input);
3224
3225 /* Here's the dirty little trick that makes the
3226 * repaint debugging work: we force an
3227 * update_transform first to update dependent state
3228 * and clear the geometry.dirty bit. Then we clear
3229 * the surface damage so it only gets repainted
3230 * piecewise as we repaint other things. */
3231
3232 weston_surface_update_transform(surface);
3233 pixman_region32_fini(&surface->damage);
3234 pixman_region32_init(&surface->damage);
3235 shell->debug_repaint_surface = surface;
3236 }
3237}
3238
3239static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003240force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3241 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003242{
3243 struct wl_client *client;
3244 pid_t pid;
3245 uid_t uid;
3246 gid_t gid;
3247
Daniel Stone325fc2d2012-05-30 16:31:58 +01003248 client = seat->keyboard->focus->resource.client;
3249 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003250
Daniel Stone325fc2d2012-05-30 16:31:58 +01003251 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003252}
3253
3254static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003255workspace_up_binding(struct wl_seat *seat, uint32_t time,
3256 uint32_t key, void *data)
3257{
3258 struct desktop_shell *shell = data;
3259 unsigned int new_index = shell->workspaces.current;
3260
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003261 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003262 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003263 if (new_index != 0)
3264 new_index--;
3265
3266 change_workspace(shell, new_index);
3267}
3268
3269static void
3270workspace_down_binding(struct wl_seat *seat, uint32_t time,
3271 uint32_t key, void *data)
3272{
3273 struct desktop_shell *shell = data;
3274 unsigned int new_index = shell->workspaces.current;
3275
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003276 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003277 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003278 if (new_index < shell->workspaces.num - 1)
3279 new_index++;
3280
3281 change_workspace(shell, new_index);
3282}
3283
3284static void
3285workspace_f_binding(struct wl_seat *seat, uint32_t time,
3286 uint32_t key, void *data)
3287{
3288 struct desktop_shell *shell = data;
3289 unsigned int new_index;
3290
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003291 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003292 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003293 new_index = key - KEY_F1;
3294 if (new_index >= shell->workspaces.num)
3295 new_index = shell->workspaces.num - 1;
3296
3297 change_workspace(shell, new_index);
3298}
3299
3300
3301static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003302shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003303{
Tiago Vignattibe143262012-04-16 17:31:41 +03003304 struct desktop_shell *shell =
3305 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003306 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003307
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003308 if (shell->child.client)
3309 wl_client_destroy(shell->child.client);
3310
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003311 wl_list_remove(&shell->lock_listener.link);
3312 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003313 wl_list_remove(&shell->show_input_panel_listener.link);
3314 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003315
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003316 wl_array_for_each(ws, &shell->workspaces.array)
3317 workspace_destroy(*ws);
3318 wl_array_release(&shell->workspaces.array);
3319
Pekka Paalanen3c647232011-12-22 13:43:43 +02003320 free(shell->screensaver.path);
3321 free(shell);
3322}
3323
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003324static void
3325shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3326{
3327 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003328 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003329
3330 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003331 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3332 MODIFIER_CTRL | MODIFIER_ALT,
3333 terminate_binding, ec);
3334 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3335 click_to_activate_binding,
3336 shell);
3337 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3338 MODIFIER_SUPER | MODIFIER_ALT,
3339 surface_opacity_binding, NULL);
3340 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3341 MODIFIER_SUPER, zoom_axis_binding,
3342 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003343
3344 /* configurable bindings */
3345 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003346 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3347 zoom_key_binding, NULL);
3348 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3349 zoom_key_binding, NULL);
3350 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3351 shell);
3352 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3353 resize_binding, shell);
3354 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3355 rotate_binding, NULL);
3356 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3357 shell);
3358 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3359 ec);
3360 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3361 backlight_binding, ec);
3362 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3363 ec);
3364 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3365 backlight_binding, ec);
3366 weston_compositor_add_key_binding(ec, KEY_SPACE, mod,
3367 debug_repaint_binding, shell);
3368 weston_compositor_add_key_binding(ec, KEY_K, mod,
3369 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003370 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3371 workspace_up_binding, shell);
3372 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3373 workspace_down_binding, shell);
3374
3375 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3376 if (shell->workspaces.num > 1) {
3377 num_workspace_bindings = shell->workspaces.num;
3378 if (num_workspace_bindings > 6)
3379 num_workspace_bindings = 6;
3380 for (i = 0; i < num_workspace_bindings; i++)
3381 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3382 workspace_f_binding,
3383 shell);
3384 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003385}
3386
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003387int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003388shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003389
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003390WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003391shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003392{
Tiago Vignattibe143262012-04-16 17:31:41 +03003393 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003394 struct workspace **pws;
3395 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003396
3397 shell = malloc(sizeof *shell);
3398 if (shell == NULL)
3399 return -1;
3400
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003401 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003402 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003403
3404 shell->destroy_listener.notify = shell_destroy;
3405 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3406 shell->lock_listener.notify = lock;
3407 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3408 shell->unlock_listener.notify = unlock;
3409 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003410 shell->show_input_panel_listener.notify = show_input_panels;
3411 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3412 shell->hide_input_panel_listener.notify = hide_input_panels;
3413 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003414 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003415 ec->shell_interface.create_shell_surface = create_shell_surface;
3416 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003417 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003418 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003419 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003420
Pekka Paalanen77346a62011-11-30 16:26:35 +02003421 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003422 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003423
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003424 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3425 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003426 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3427 weston_layer_init(&shell->lock_layer, NULL);
3428
3429 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003430
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003431 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003432
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003433 for (i = 0; i < shell->workspaces.num; i++) {
3434 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3435 if (pws == NULL)
3436 return -1;
3437
3438 *pws = workspace_create();
3439 if (*pws == NULL)
3440 return -1;
3441 }
3442 activate_workspace(shell, 0);
3443
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003444 wl_list_init(&shell->workspaces.animation.link);
3445 shell->workspaces.animation.frame = animate_workspace_change_frame;
3446
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003447 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3448 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003449 return -1;
3450
Kristian Høgsberg75840622011-09-06 13:48:16 -04003451 if (wl_display_add_global(ec->wl_display,
3452 &desktop_shell_interface,
3453 shell, bind_desktop_shell) == NULL)
3454 return -1;
3455
Pekka Paalanen6e168112011-11-24 11:34:05 +02003456 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3457 shell, bind_screensaver) == NULL)
3458 return -1;
3459
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003460 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3461 shell, bind_input_panel) == NULL)
3462 return -1;
3463
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003464 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003465 if (launch_desktop_shell_process(shell) != 0)
3466 return -1;
3467
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003468 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003469 if (ec->seat->seat.pointer)
3470 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3471 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003472
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003473 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003474
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003475 return 0;
3476}