blob: f1a01700a6c02bb00265ca19bb29739a2549f431 [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
Pekka Paalanen068ae942011-11-28 14:11:15 +020095 struct shell_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
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010098 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020099
Pekka Paalanen77346a62011-11-30 16:26:35 +0200100 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200101 struct wl_array array;
102 unsigned int current;
103 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200104
105 struct weston_animation animation;
106 int anim_dir;
107 uint32_t anim_timestamp;
108 double anim_current;
109 struct workspace *anim_from;
110 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200111 } workspaces;
112
113 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200114 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200115 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200116 struct wl_resource *binding;
117 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500118 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200119 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500120
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200121 struct {
122 struct wl_resource *binding;
123 struct wl_list surfaces;
124 } input_panel;
125
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300126 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800127 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500128 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400129};
130
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500131enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200132 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500133
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200134 SHELL_SURFACE_PANEL,
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200135 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200136 SHELL_SURFACE_SCREENSAVER,
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200137 SHELL_SURFACE_INPUT_PANEL,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500138
139 SHELL_SURFACE_TOPLEVEL,
140 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500141 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800142 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500143 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200144};
145
Scott Moreauff1db4a2012-04-17 19:06:18 -0600146struct ping_timer {
147 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600148 uint32_t serial;
149};
150
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200151struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200152 struct wl_resource resource;
153
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500154 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200155 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500156 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300157 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200158
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400159 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400160 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500161 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800162 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800163 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600164 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100165
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500166 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200167 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500168 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200169 } rotation;
170
171 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700172 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500173 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200174 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500175 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100176 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400177 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500178 } popup;
179
Alex Wu4539b082012-03-01 12:57:46 +0800180 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300181 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400182 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300183 } transient;
184
185 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800186 enum wl_shell_surface_fullscreen_method type;
187 struct weston_transform transform; /* matrix from x, y */
188 uint32_t framerate;
189 struct weston_surface *black_surface;
190 } fullscreen;
191
Scott Moreauff1db4a2012-04-17 19:06:18 -0600192 struct ping_timer *ping_timer;
193
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200194 struct weston_transform workspace_transform;
195
Jonas Ådahl04769742012-06-13 00:01:24 +0200196 struct focus_state *focus_state;
197
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500198 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500199 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100200 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400201
202 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200203};
204
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300205struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700206 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300207 struct shell_surface *shsurf;
208 struct wl_listener shsurf_destroy_listener;
209};
210
211struct weston_move_grab {
212 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100213 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500214};
215
Pekka Paalanen460099f2012-01-20 16:48:25 +0200216struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300217 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500218 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200219 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400220 GLfloat x;
221 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200222 } center;
223};
224
Alex Wubd3354b2012-04-17 17:20:49 +0800225static struct shell_surface *
226get_shell_surface(struct weston_surface *surface);
227
228static struct desktop_shell *
229shell_surface_get_shell(struct shell_surface *shsurf);
230
231static bool
232shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
233{
234 struct desktop_shell *shell;
235 struct weston_surface *top_fs_es;
236
237 shell = shell_surface_get_shell(shsurf);
238
239 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
240 return false;
241
242 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
243 struct weston_surface,
244 layer_link);
245 return (shsurf == get_shell_surface(top_fs_es));
246}
247
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500248static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400249destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300250{
251 struct shell_grab *grab;
252
253 grab = container_of(listener, struct shell_grab,
254 shsurf_destroy_listener);
255
256 grab->shsurf = NULL;
257}
258
259static void
260shell_grab_init(struct shell_grab *grab,
261 const struct wl_pointer_grab_interface *interface,
262 struct shell_surface *shsurf)
263{
264 grab->grab.interface = interface;
265 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400266 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
267 wl_signal_add(&shsurf->resource.destroy_signal,
268 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300269
270}
271
272static void
273shell_grab_finish(struct shell_grab *grab)
274{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400275 if (grab->shsurf)
276 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300277}
278
279static void
Alex Wu4539b082012-03-01 12:57:46 +0800280center_on_output(struct weston_surface *surface,
281 struct weston_output *output);
282
Daniel Stone496ca172012-05-30 16:31:42 +0100283static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300284get_modifier(char *modifier)
285{
286 if (!modifier)
287 return MODIFIER_SUPER;
288
289 if (!strcmp("ctrl", modifier))
290 return MODIFIER_CTRL;
291 else if (!strcmp("alt", modifier))
292 return MODIFIER_ALT;
293 else if (!strcmp("super", modifier))
294 return MODIFIER_SUPER;
295 else
296 return MODIFIER_SUPER;
297}
298
Juan Zhaoe10d2792012-04-25 19:09:52 +0800299static enum animation_type
300get_animation_type(char *animation)
301{
302 if (!animation)
303 return ANIMATION_NONE;
304
305 if (!strcmp("zoom", animation))
306 return ANIMATION_ZOOM;
307 else if (!strcmp("fade", animation))
308 return ANIMATION_FADE;
309 else
310 return ANIMATION_NONE;
311}
312
Alex Wu4539b082012-03-01 12:57:46 +0800313static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300314shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200315{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200316 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200317 char *path = NULL;
318 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200319 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300320 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800321 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200322
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400323 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300324 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800325 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200326 { "num-workspaces",
327 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200328 };
329
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400330 struct config_key saver_keys[] = {
331 { "path", CONFIG_KEY_STRING, &path },
332 { "duration", CONFIG_KEY_INTEGER, &duration },
333 };
334
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200335 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400336 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200337 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
338 };
339
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200340 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500341 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200342 free(config_file);
343
Pekka Paalanen7296e792011-12-07 16:22:00 +0200344 shell->screensaver.path = path;
345 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300346 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800347 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200348 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
349}
350
351static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200352focus_state_destroy(struct focus_state *state)
353{
354 wl_list_remove(&state->seat_destroy_listener.link);
355 wl_list_remove(&state->surface_destroy_listener.link);
356 free(state);
357}
358
359static void
360focus_state_seat_destroy(struct wl_listener *listener, void *data)
361{
362 struct focus_state *state = container_of(listener,
363 struct focus_state,
364 seat_destroy_listener);
365
366 wl_list_remove(&state->link);
367 focus_state_destroy(state);
368}
369
370static void
371focus_state_surface_destroy(struct wl_listener *listener, void *data)
372{
373 struct focus_state *state = container_of(listener,
374 struct focus_state,
375 seat_destroy_listener);
376
377 wl_list_remove(&state->link);
378 focus_state_destroy(state);
379}
380
381static struct focus_state *
382focus_state_create(struct weston_seat *seat)
383{
384 struct wl_keyboard *keyboard = seat->seat.keyboard;
385 struct focus_state *state;
386 struct wl_surface *surface;
387 struct shell_surface *shsurf;
388
389 state = malloc(sizeof *state);
390 if (state == NULL)
391 return NULL;
392
393 surface = keyboard->focus;
394 shsurf = get_shell_surface((struct weston_surface *)keyboard->focus);
395 shsurf->focus_state = state;
396
397 state->seat = seat;
398 state->keyboard_focus = shsurf->surface;
399 wl_list_init(&state->link);
400
401 state->seat_destroy_listener.notify = focus_state_seat_destroy;
402 state->surface_destroy_listener.notify = focus_state_surface_destroy;
403 wl_signal_add(&seat->seat.destroy_signal,
404 &state->seat_destroy_listener);
405 wl_signal_add(&surface->resource.destroy_signal,
406 &state->surface_destroy_listener);
407
408 return state;
409}
410
411static void
412pop_focus_state(struct desktop_shell *shell, struct workspace *ws)
413{
414 struct focus_state *state, *next;
415
416 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
417 if (state->keyboard_focus)
418 wl_keyboard_set_focus(state->seat->seat.keyboard,
419 &state->keyboard_focus->surface);
420
421 focus_state_destroy(state);
422 }
423 wl_list_init(&ws->focus_list);
424}
425
426static void
427push_focus_state(struct desktop_shell *shell, struct workspace *ws)
428{
429 struct weston_seat *seat;
430 struct focus_state *state;
431 struct wl_keyboard *keyboard;
432
433 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
434 keyboard = seat->seat.keyboard;
435 if (keyboard && keyboard->focus) {
436 state = focus_state_create(seat);
437 if (state == NULL)
438 return;
439
440 wl_list_insert(&ws->focus_list, &state->link);
441
442 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
443 }
444 }
445}
446
447static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200448workspace_destroy(struct workspace *ws)
449{
Jonas Ådahl04769742012-06-13 00:01:24 +0200450 struct focus_state *state, *next;
451
452 wl_list_for_each_safe(state, next, &ws->focus_list, link)
453 focus_state_destroy(state);
454
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200455 free(ws);
456}
457
Jonas Ådahl04769742012-06-13 00:01:24 +0200458static void
459seat_destroyed(struct wl_listener *listener, void *data)
460{
461 struct weston_seat *seat = data;
462 struct focus_state *state, *next;
463 struct workspace *ws = container_of(listener,
464 struct workspace,
465 seat_destroyed_listener);
466
467 wl_list_for_each_safe(state, next, &ws->focus_list, link)
468 if (state->seat == seat)
469 wl_list_remove(&state->link);
470}
471
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200472static struct workspace *
473workspace_create(void)
474{
475 struct workspace *ws = malloc(sizeof *ws);
476 if (ws == NULL)
477 return NULL;
478
479 weston_layer_init(&ws->layer, NULL);
480
Jonas Ådahl04769742012-06-13 00:01:24 +0200481 wl_list_init(&ws->focus_list);
482 wl_list_init(&ws->seat_destroyed_listener.link);
483 ws->seat_destroyed_listener.notify = seat_destroyed;
484
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200485 return ws;
486}
487
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200488static int
489workspace_is_empty(struct workspace *ws)
490{
491 return wl_list_empty(&ws->layer.surface_list);
492}
493
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200494static struct workspace *
495get_workspace(struct desktop_shell *shell, unsigned int index)
496{
497 struct workspace **pws = shell->workspaces.array.data;
498 pws += index;
499 return *pws;
500}
501
502static struct workspace *
503get_current_workspace(struct desktop_shell *shell)
504{
505 return get_workspace(shell, shell->workspaces.current);
506}
507
508static void
509activate_workspace(struct desktop_shell *shell, unsigned int index)
510{
511 struct workspace *ws;
512
513 ws = get_workspace(shell, index);
514 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
515
516 shell->workspaces.current = index;
517}
518
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200519static unsigned int
520get_output_height(struct weston_output *output)
521{
522 return abs(output->region.extents.y1 - output->region.extents.y2);
523}
524
525static void
526surface_translate(struct weston_surface *surface, double d)
527{
528 struct shell_surface *shsurf = get_shell_surface(surface);
529 struct weston_transform *transform;
530
531 transform = &shsurf->workspace_transform;
532 if (wl_list_empty(&transform->link))
533 wl_list_insert(surface->geometry.transformation_list.prev,
534 &shsurf->workspace_transform.link);
535
536 weston_matrix_init(&shsurf->workspace_transform.matrix);
537 weston_matrix_translate(&shsurf->workspace_transform.matrix,
538 0.0, d, 0.0);
539 surface->geometry.dirty = 1;
540}
541
542static void
543workspace_translate_out(struct workspace *ws, double fraction)
544{
545 struct weston_surface *surface;
546 unsigned int height;
547 double d;
548
549 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
550 height = get_output_height(surface->output);
551 d = height * fraction;
552
553 surface_translate(surface, d);
554 }
555}
556
557static void
558workspace_translate_in(struct workspace *ws, double fraction)
559{
560 struct weston_surface *surface;
561 unsigned int height;
562 double d;
563
564 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
565 height = get_output_height(surface->output);
566
567 if (fraction > 0)
568 d = -(height - height * fraction);
569 else
570 d = height + height * fraction;
571
572 surface_translate(surface, d);
573 }
574}
575
576static void
577workspace_damage_all_surfaces(struct workspace *ws)
578{
579 struct weston_surface *surface;
580
581 wl_list_for_each(surface, &ws->layer.surface_list, layer_link)
582 weston_surface_damage(surface);
583}
584
585static void
586reverse_workspace_change_animation(struct desktop_shell *shell,
587 unsigned int index,
588 struct workspace *from,
589 struct workspace *to)
590{
591 shell->workspaces.current = index;
592
593 shell->workspaces.anim_to = to;
594 shell->workspaces.anim_from = from;
595 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
596 shell->workspaces.anim_timestamp = 0;
597
Jonas Ådahl04769742012-06-13 00:01:24 +0200598 push_focus_state(shell, from);
599 pop_focus_state(shell, to);
600
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200601 workspace_damage_all_surfaces(from);
602 workspace_damage_all_surfaces(to);
603}
604
605static void
606workspace_deactivate_transforms(struct workspace *ws)
607{
608 struct weston_surface *surface;
609 struct shell_surface *shsurf;
610
611 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
612 shsurf = get_shell_surface(surface);
613 wl_list_remove(&shsurf->workspace_transform.link);
614 wl_list_init(&shsurf->workspace_transform.link);
615 shsurf->surface->geometry.dirty = 1;
616 }
617}
618
619static void
620finish_workspace_change_animation(struct desktop_shell *shell,
621 struct workspace *from,
622 struct workspace *to)
623{
624 workspace_damage_all_surfaces(from);
625 workspace_damage_all_surfaces(to);
626
627 wl_list_remove(&shell->workspaces.animation.link);
628 workspace_deactivate_transforms(from);
629 workspace_deactivate_transforms(to);
630 shell->workspaces.anim_to = NULL;
631
632 wl_list_remove(&shell->workspaces.anim_from->layer.link);
633}
634
635static void
636animate_workspace_change_frame(struct weston_animation *animation,
637 struct weston_output *output, uint32_t msecs)
638{
639 struct desktop_shell *shell =
640 container_of(animation, struct desktop_shell,
641 workspaces.animation);
642 struct workspace *from = shell->workspaces.anim_from;
643 struct workspace *to = shell->workspaces.anim_to;
644 uint32_t t;
645 double x, y;
646
647 if (workspace_is_empty(from) && workspace_is_empty(to)) {
648 finish_workspace_change_animation(shell, from, to);
649 return;
650 }
651
652 if (shell->workspaces.anim_timestamp == 0) {
653 if (shell->workspaces.anim_current == 0.0)
654 shell->workspaces.anim_timestamp = msecs;
655 else
656 shell->workspaces.anim_timestamp =
657 msecs -
658 /* Invers of movement function 'y' below. */
659 (asin(1.0 - shell->workspaces.anim_current) *
660 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
661 M_2_PI);
662 }
663
664 t = msecs - shell->workspaces.anim_timestamp;
665
666 /*
667 * x = [0, π/2]
668 * y(x) = sin(x)
669 */
670 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
671 y = sin(x);
672
673 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
674 workspace_damage_all_surfaces(from);
675 workspace_damage_all_surfaces(to);
676
677 workspace_translate_out(from, shell->workspaces.anim_dir * y);
678 workspace_translate_in(to, shell->workspaces.anim_dir * y);
679 shell->workspaces.anim_current = y;
680
681 workspace_damage_all_surfaces(from);
682 workspace_damage_all_surfaces(to);
683 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200684 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200685 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200686}
687
688static void
689animate_workspace_change(struct desktop_shell *shell,
690 unsigned int index,
691 struct workspace *from,
692 struct workspace *to)
693{
694 struct weston_output *output;
695
696 int dir;
697
698 if (index > shell->workspaces.current)
699 dir = -1;
700 else
701 dir = 1;
702
703 shell->workspaces.current = index;
704
705 shell->workspaces.anim_dir = dir;
706 shell->workspaces.anim_from = from;
707 shell->workspaces.anim_to = to;
708 shell->workspaces.anim_current = 0.0;
709 shell->workspaces.anim_timestamp = 0;
710
711 output = container_of(shell->compositor->output_list.next,
712 struct weston_output, link);
713 wl_list_insert(&output->animation_list,
714 &shell->workspaces.animation.link);
715
716 wl_list_insert(&from->layer.link, &to->layer.link);
717
718 workspace_translate_in(to, 0);
719
Jonas Ådahl04769742012-06-13 00:01:24 +0200720 push_focus_state(shell, from);
721 pop_focus_state(shell, to);
722
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200723 workspace_damage_all_surfaces(from);
724 workspace_damage_all_surfaces(to);
725}
726
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200727static void
728change_workspace(struct desktop_shell *shell, unsigned int index)
729{
730 struct workspace *from;
731 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200732
733 if (index == shell->workspaces.current)
734 return;
735
736 /* Don't change workspace when there is any fullscreen surfaces. */
737 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
738 return;
739
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200740 from = get_current_workspace(shell);
741 to = get_workspace(shell, index);
742
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200743 if (shell->workspaces.anim_from == to &&
744 shell->workspaces.anim_to == from) {
745 reverse_workspace_change_animation(shell, index, from, to);
746 return;
747 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200748
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200749 if (shell->workspaces.anim_to != NULL)
750 finish_workspace_change_animation(shell,
751 shell->workspaces.anim_from,
752 shell->workspaces.anim_to);
753
754 if (workspace_is_empty(to) && workspace_is_empty(from)) {
755 shell->workspaces.current = index;
756 wl_list_insert(&from->layer.link, &to->layer.link);
757 wl_list_remove(&from->layer.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200758
759 push_focus_state(shell, from);
760 pop_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200761 }
762 else
763 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200764}
765
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200766static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400767noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100768 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500769{
770 grab->focus = NULL;
771}
772
773static void
Scott Moreau447013d2012-02-18 05:05:29 -0700774move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100775 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500776{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500777 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100778 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300779 struct shell_surface *shsurf = move->base.shsurf;
780 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100781 int dx = wl_fixed_to_int(pointer->x + move->dx);
782 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300783
784 if (!shsurf)
785 return;
786
787 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500788
Daniel Stone103db7f2012-05-08 17:17:55 +0100789 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200790 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -0400791
792 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500793}
794
795static void
Scott Moreau447013d2012-02-18 05:05:29 -0700796move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100797 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500798{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300799 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
800 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100801 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100802 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500803
Daniel Stone4dbadb12012-05-30 16:31:51 +0100804 if (pointer->button_count == 0 &&
805 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300806 shell_grab_finish(shell_grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100807 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500808 free(grab);
809 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500810}
811
Scott Moreau447013d2012-02-18 05:05:29 -0700812static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500813 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500814 move_grab_motion,
815 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500816};
817
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600818static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400819busy_cursor_grab_focus(struct wl_pointer_grab *base,
820 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600821{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400822 struct shell_grab *grab = (struct shell_grab *) base;
823 struct wl_pointer *pointer = base->pointer;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600824
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400825 if (grab->grab.focus != surface) {
826 shell_grab_finish(grab);
827 wl_pointer_end_grab(pointer);
828 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600829 }
830}
831
832static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400833busy_cursor_grab_motion(struct wl_pointer_grab *grab,
834 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600835{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400836}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600837
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400838static void
839busy_cursor_grab_button(struct wl_pointer_grab *grab,
840 uint32_t time, uint32_t button, uint32_t state)
841{
842}
843
844static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
845 busy_cursor_grab_focus,
846 busy_cursor_grab_motion,
847 busy_cursor_grab_button,
848};
849
850static void
851set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
852{
853 struct shell_grab *grab;
854 struct desktop_shell *shell = shsurf->shell;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400855
856 grab = malloc(sizeof *grab);
857 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600858 return;
859
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400860 shell_grab_init(grab, &busy_cursor_grab_interface, shsurf);
861 grab->grab.focus = &shsurf->surface->surface;
862 wl_pointer_start_grab(pointer, &grab->grab);
863 wl_pointer_set_focus(pointer, &shell->busy_surface->surface, 0, 0);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400864}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600865
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400866static void
867end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
868{
869 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
870
871 if (grab->grab.interface == &busy_cursor_grab_interface) {
872 shell_grab_finish(grab);
873 wl_pointer_end_grab(pointer);
874 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600875 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600876}
877
Scott Moreau9521d5e2012-04-19 13:06:17 -0600878static void
879ping_timer_destroy(struct shell_surface *shsurf)
880{
881 if (!shsurf || !shsurf->ping_timer)
882 return;
883
884 if (shsurf->ping_timer->source)
885 wl_event_source_remove(shsurf->ping_timer->source);
886
887 free(shsurf->ping_timer);
888 shsurf->ping_timer = NULL;
889}
890
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400891static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600892ping_timeout_handler(void *data)
893{
894 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400895 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600896
Scott Moreau9521d5e2012-04-19 13:06:17 -0600897 /* Client is not responding */
898 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400899
900 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
901 if (seat->seat.pointer->focus == &shsurf->surface->surface)
902 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600903
904 return 1;
905}
906
907static void
908ping_handler(struct weston_surface *surface, uint32_t serial)
909{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400910 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600911 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400912 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600913
914 if (!shsurf)
915 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400916 if (!shsurf->resource.client)
917 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600918
919 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300920 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600921 if (!shsurf->ping_timer)
922 return;
923
924 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600925 loop = wl_display_get_event_loop(surface->compositor->wl_display);
926 shsurf->ping_timer->source =
927 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
928 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
929
930 wl_shell_surface_send_ping(&shsurf->resource, serial);
931 }
932}
933
934static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400935handle_pointer_focus(struct wl_listener *listener, void *data)
936{
937 struct wl_pointer *pointer = data;
938 struct weston_surface *surface =
939 (struct weston_surface *) pointer->focus;
940 struct weston_compositor *compositor;
941 struct shell_surface *shsurf;
942 uint32_t serial;
943
944 if (!surface)
945 return;
946
947 compositor = surface->compositor;
948 shsurf = get_shell_surface(surface);
949
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +0300950 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400951 set_busy_cursor(shsurf, pointer);
952 } else {
953 serial = wl_display_next_serial(compositor->wl_display);
954 ping_handler(surface, serial);
955 }
956}
957
958static void
Scott Moreauff1db4a2012-04-17 19:06:18 -0600959shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
960 uint32_t serial)
961{
962 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400963 struct desktop_shell *shell = shsurf->shell;
964 struct weston_seat *seat;
965 struct weston_compositor *ec = shsurf->surface->compositor;
966 struct wl_pointer *pointer;
967 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600968
Scott Moreauff1db4a2012-04-17 19:06:18 -0600969 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400970 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600971 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400972 if (was_unresponsive) {
973 /* Received pong from previously unresponsive client */
974 wl_list_for_each(seat, &ec->seat_list, link) {
975 pointer = seat->seat.pointer;
976 if (pointer->focus ==
977 &shell->busy_surface->surface &&
978 pointer->current ==
979 &shsurf->surface->surface)
980 end_busy_cursor(shsurf, pointer);
981 }
982 }
Scott Moreau9521d5e2012-04-19 13:06:17 -0600983 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600984 }
985}
986
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400987static void
988shell_surface_set_title(struct wl_client *client,
989 struct wl_resource *resource, const char *title)
990{
991 struct shell_surface *shsurf = resource->data;
992
993 free(shsurf->title);
994 shsurf->title = strdup(title);
995}
996
997static void
998shell_surface_set_class(struct wl_client *client,
999 struct wl_resource *resource, const char *class)
1000{
1001 struct shell_surface *shsurf = resource->data;
1002
1003 free(shsurf->class);
1004 shsurf->class = strdup(class);
1005}
1006
Scott Moreauff1db4a2012-04-17 19:06:18 -06001007static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001008surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001009{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001010 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001011
1012 if (!shsurf)
1013 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001014
1015 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001016 if (!move)
1017 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001018
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001019 shell_grab_init(&move->base, &move_grab_interface, shsurf);
1020
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001021 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +01001022 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001023 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +01001024 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001025
Daniel Stone37816df2012-05-16 18:45:18 +01001026 wl_pointer_start_grab(ws->seat.pointer, &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001027
Daniel Stone37816df2012-05-16 18:45:18 +01001028 wl_pointer_set_focus(ws->seat.pointer, NULL,
1029 wl_fixed_from_int(0),
1030 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001031
1032 return 0;
1033}
1034
1035static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001036shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001037 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001038{
Daniel Stone37816df2012-05-16 18:45:18 +01001039 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001040 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001041
Daniel Stone37816df2012-05-16 18:45:18 +01001042 if (ws->seat.pointer->button_count == 0 ||
1043 ws->seat.pointer->grab_serial != serial ||
1044 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001045 return;
1046
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001047 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001048 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001049}
1050
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001051struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001052 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001053 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001054 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001055};
1056
1057static void
Scott Moreau447013d2012-02-18 05:05:29 -07001058resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001059 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001060{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001061 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001062 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001063 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001064 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +01001065 wl_fixed_t from_x, from_y;
1066 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001067
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001068 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001069 return;
1070
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001071 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001072 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +01001073 &from_x, &from_y);
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001074 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001075 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001076
Daniel Stone103db7f2012-05-08 17:17:55 +01001077 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001078 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001079 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001080 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001081 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001082 }
1083
Daniel Stone103db7f2012-05-08 17:17:55 +01001084 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001085 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001086 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001087 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001088 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001089 }
1090
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001091 shsurf->client->send_configure(shsurf->surface,
1092 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001093}
1094
1095static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001096send_configure(struct weston_surface *surface,
1097 uint32_t edges, int32_t width, int32_t height)
1098{
1099 struct shell_surface *shsurf = get_shell_surface(surface);
1100
1101 wl_shell_surface_send_configure(&shsurf->resource,
1102 edges, width, height);
1103}
1104
1105static const struct weston_shell_client shell_client = {
1106 send_configure
1107};
1108
1109static void
Scott Moreau447013d2012-02-18 05:05:29 -07001110resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001111 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001112{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001113 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001114 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001115 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001116
Daniel Stone4dbadb12012-05-30 16:31:51 +01001117 if (pointer->button_count == 0 &&
1118 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001119 shell_grab_finish(&resize->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001120 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001121 free(grab);
1122 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001123}
1124
Scott Moreau447013d2012-02-18 05:05:29 -07001125static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001126 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001127 resize_grab_motion,
1128 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001129};
1130
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001131static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001132surface_resize(struct shell_surface *shsurf,
1133 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001134{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001135 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001136
Alex Wu4539b082012-03-01 12:57:46 +08001137 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1138 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001139
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001140 if (edges == 0 || edges > 15 ||
1141 (edges & 3) == 3 || (edges & 12) == 12)
1142 return 0;
1143
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001144 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001145 if (!resize)
1146 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001147
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001148 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
1149
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001150 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001151 resize->width = shsurf->surface->geometry.width;
1152 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001153
Daniel Stone37816df2012-05-16 18:45:18 +01001154 wl_pointer_start_grab(ws->seat.pointer, &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001155
Daniel Stone37816df2012-05-16 18:45:18 +01001156 wl_pointer_set_focus(ws->seat.pointer, NULL,
1157 wl_fixed_from_int(0),
1158 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001159
1160 return 0;
1161}
1162
1163static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001164shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001165 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001166 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001167{
Daniel Stone37816df2012-05-16 18:45:18 +01001168 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001169 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001170
Alex Wu4539b082012-03-01 12:57:46 +08001171 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1172 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001173
Daniel Stone37816df2012-05-16 18:45:18 +01001174 if (ws->seat.pointer->button_count == 0 ||
1175 ws->seat.pointer->grab_serial != serial ||
1176 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001177 return;
1178
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001179 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001180 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001181}
1182
Juan Zhao96879df2012-02-07 08:45:41 +08001183static struct weston_output *
1184get_default_output(struct weston_compositor *compositor)
1185{
1186 return container_of(compositor->output_list.next,
1187 struct weston_output, link);
1188}
1189
Alex Wu4539b082012-03-01 12:57:46 +08001190static void
1191shell_unset_fullscreen(struct shell_surface *shsurf)
1192{
1193 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001194 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1195 shell_surface_is_top_fullscreen(shsurf)) {
1196 weston_output_switch_mode(shsurf->fullscreen_output,
1197 shsurf->fullscreen_output->origin);
1198 }
Alex Wu4539b082012-03-01 12:57:46 +08001199 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1200 shsurf->fullscreen.framerate = 0;
1201 wl_list_remove(&shsurf->fullscreen.transform.link);
1202 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001203 if (shsurf->fullscreen.black_surface)
1204 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001205 shsurf->fullscreen.black_surface = NULL;
1206 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001207 weston_surface_set_position(shsurf->surface,
1208 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001209 if (shsurf->saved_rotation_valid) {
1210 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1211 &shsurf->rotation.transform.link);
1212 shsurf->saved_rotation_valid = false;
1213 }
Alex Wu4539b082012-03-01 12:57:46 +08001214}
1215
Pekka Paalanen98262232011-12-01 10:42:22 +02001216static int
1217reset_shell_surface_type(struct shell_surface *surface)
1218{
1219 switch (surface->type) {
1220 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001221 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001222 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001223 case SHELL_SURFACE_MAXIMIZED:
1224 surface->output = get_default_output(surface->surface->compositor);
1225 weston_surface_set_position(surface->surface,
1226 surface->saved_x,
1227 surface->saved_y);
1228 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001229 case SHELL_SURFACE_PANEL:
Jan Arne Petersen42feced2012-06-21 21:52:17 +02001230 case SHELL_SURFACE_INPUT_PANEL:
Pekka Paalanen98262232011-12-01 10:42:22 +02001231 wl_list_remove(&surface->link);
1232 wl_list_init(&surface->link);
1233 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001234 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +02001235 case SHELL_SURFACE_LOCK:
1236 wl_resource_post_error(&surface->resource,
1237 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +02001238 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +02001239 return -1;
1240 case SHELL_SURFACE_NONE:
1241 case SHELL_SURFACE_TOPLEVEL:
1242 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001243 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001244 break;
1245 }
1246
1247 surface->type = SHELL_SURFACE_NONE;
1248 return 0;
1249}
1250
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001251static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001252set_surface_type(struct shell_surface *shsurf)
1253{
1254 struct weston_surface *surface = shsurf->surface;
1255 struct shell_surface *pshsurf = shsurf->parent;
1256 struct weston_surface *pes;
1257 struct shell_surface *priv;
1258 struct desktop_shell *shell = shsurf->shell;
1259
1260 reset_shell_surface_type(shsurf);
1261
1262 shsurf->type = shsurf->next_type;
1263 shsurf->next_type = SHELL_SURFACE_NONE;
1264
1265 switch (shsurf->type) {
1266 case SHELL_SURFACE_TOPLEVEL:
1267 break;
1268 case SHELL_SURFACE_TRANSIENT:
1269 pes = pshsurf->surface;
1270 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001271 pes->geometry.x + shsurf->transient.x,
1272 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001273 break;
1274
1275 case SHELL_SURFACE_MAXIMIZED:
1276 shsurf->saved_x = surface->geometry.x;
1277 shsurf->saved_y = surface->geometry.y;
1278 shsurf->saved_position_valid = true;
1279 break;
1280
1281 case SHELL_SURFACE_FULLSCREEN:
1282 shsurf->saved_x = surface->geometry.x;
1283 shsurf->saved_y = surface->geometry.y;
1284 shsurf->saved_position_valid = true;
1285
1286 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1287 wl_list_remove(&shsurf->rotation.transform.link);
1288 wl_list_init(&shsurf->rotation.transform.link);
1289 shsurf->surface->geometry.dirty = 1;
1290 shsurf->saved_rotation_valid = true;
1291 }
1292 break;
1293
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001294 case SHELL_SURFACE_PANEL:
1295 wl_list_for_each(priv, &shell->panels, link) {
1296 if (priv->output == shsurf->output) {
1297 priv->surface->output = NULL;
1298 wl_list_remove(&priv->surface->layer_link);
1299 wl_list_remove(&priv->link);
1300 break;
1301 }
1302 }
1303
1304 wl_list_insert(&shell->panels, &shsurf->link);
1305
1306 weston_surface_set_position(surface, shsurf->output->x,
1307 shsurf->output->y);
1308 break;
1309
1310 default:
1311 break;
1312 }
1313}
1314
1315static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001316set_toplevel(struct shell_surface *shsurf)
1317{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001318 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001319}
1320
1321static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001322shell_surface_set_toplevel(struct wl_client *client,
1323 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001324{
Pekka Paalanen98262232011-12-01 10:42:22 +02001325 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001326
Tiago Vignattibc052c92012-04-19 16:18:18 +03001327 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001328}
1329
1330static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001331set_transient(struct shell_surface *shsurf,
1332 struct shell_surface *pshsurf, int x, int y, uint32_t flags)
1333{
1334 /* assign to parents output */
1335 shsurf->parent = pshsurf;
1336 shsurf->transient.x = x;
1337 shsurf->transient.y = y;
1338 shsurf->transient.flags = flags;
1339 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1340}
1341
1342static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001343shell_surface_set_transient(struct wl_client *client,
1344 struct wl_resource *resource,
1345 struct wl_resource *parent_resource,
1346 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001347{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001348 struct shell_surface *shsurf = resource->data;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001349 struct shell_surface *pshsurf = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001350
Tiago Vignatti491bac12012-05-18 16:37:43 -04001351 set_transient(shsurf, pshsurf, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001352}
1353
Tiago Vignattibe143262012-04-16 17:31:41 +03001354static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001355shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001356{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001357 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001358}
1359
1360static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001361get_output_panel_height(struct desktop_shell *shell,
1362 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001363{
1364 struct shell_surface *priv;
1365 int panel_height = 0;
1366
1367 if (!output)
1368 return 0;
1369
Tiago Vignattibe143262012-04-16 17:31:41 +03001370 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +08001371 if (priv->output == output) {
1372 panel_height = priv->surface->geometry.height;
1373 break;
1374 }
1375 }
1376 return panel_height;
1377}
1378
1379static void
1380shell_surface_set_maximized(struct wl_client *client,
1381 struct wl_resource *resource,
1382 struct wl_resource *output_resource )
1383{
1384 struct shell_surface *shsurf = resource->data;
1385 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001386 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001387 uint32_t edges = 0, panel_height = 0;
1388
1389 /* get the default output, if the client set it as NULL
1390 check whether the ouput is available */
1391 if (output_resource)
1392 shsurf->output = output_resource->data;
1393 else
1394 shsurf->output = get_default_output(es->compositor);
1395
Tiago Vignattibe143262012-04-16 17:31:41 +03001396 shell = shell_surface_get_shell(shsurf);
1397 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001398 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001399
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001400 shsurf->client->send_configure(shsurf->surface, edges,
1401 es->output->current->width,
1402 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001403
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001404 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001405}
1406
Alex Wu21858432012-04-01 20:13:08 +08001407static void
1408black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1409
Alex Wu4539b082012-03-01 12:57:46 +08001410static struct weston_surface *
1411create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001412 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001413 GLfloat x, GLfloat y, int w, int h)
1414{
1415 struct weston_surface *surface = NULL;
1416
1417 surface = weston_surface_create(ec);
1418 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001419 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001420 return NULL;
1421 }
1422
Alex Wu21858432012-04-01 20:13:08 +08001423 surface->configure = black_surface_configure;
1424 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001425 weston_surface_configure(surface, x, y, w, h);
1426 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1427 return surface;
1428}
1429
1430/* Create black surface and append it to the associated fullscreen surface.
1431 * Handle size dismatch and positioning according to the method. */
1432static void
1433shell_configure_fullscreen(struct shell_surface *shsurf)
1434{
1435 struct weston_output *output = shsurf->fullscreen_output;
1436 struct weston_surface *surface = shsurf->surface;
1437 struct weston_matrix *matrix;
1438 float scale;
1439
1440 center_on_output(surface, output);
1441
1442 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001443 shsurf->fullscreen.black_surface =
1444 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001445 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001446 output->x, output->y,
1447 output->current->width,
1448 output->current->height);
1449
1450 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1451 wl_list_insert(&surface->layer_link,
1452 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001453 shsurf->fullscreen.black_surface->output = output;
1454
1455 switch (shsurf->fullscreen.type) {
1456 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1457 break;
1458 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1459 matrix = &shsurf->fullscreen.transform.matrix;
1460 weston_matrix_init(matrix);
1461 scale = (float)output->current->width/(float)surface->geometry.width;
1462 weston_matrix_scale(matrix, scale, scale, 1);
1463 wl_list_remove(&shsurf->fullscreen.transform.link);
1464 wl_list_insert(surface->geometry.transformation_list.prev,
1465 &shsurf->fullscreen.transform.link);
1466 weston_surface_set_position(surface, output->x, output->y);
1467 break;
1468 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001469 if (shell_surface_is_top_fullscreen(shsurf)) {
1470 struct weston_mode mode = {0,
1471 surface->geometry.width,
1472 surface->geometry.height,
1473 shsurf->fullscreen.framerate};
1474
1475 if (weston_output_switch_mode(output, &mode) == 0) {
1476 weston_surface_configure(shsurf->fullscreen.black_surface,
1477 output->x, output->y,
1478 output->current->width,
1479 output->current->height);
1480 weston_surface_set_position(surface, output->x, output->y);
1481 break;
1482 }
1483 }
Alex Wu4539b082012-03-01 12:57:46 +08001484 break;
1485 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1486 break;
1487 default:
1488 break;
1489 }
1490}
1491
1492/* make the fullscreen and black surface at the top */
1493static void
1494shell_stack_fullscreen(struct shell_surface *shsurf)
1495{
Alex Wubd3354b2012-04-17 17:20:49 +08001496 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001497 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001498 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001499
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001500 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001501 wl_list_insert(&shell->fullscreen_layer.surface_list,
1502 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001503 weston_surface_damage(surface);
1504
1505 if (!shsurf->fullscreen.black_surface)
1506 shsurf->fullscreen.black_surface =
1507 create_black_surface(surface->compositor,
1508 surface,
1509 output->x, output->y,
1510 output->current->width,
1511 output->current->height);
1512
1513 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001514 wl_list_insert(&surface->layer_link,
1515 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001516 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001517}
1518
1519static void
1520shell_map_fullscreen(struct shell_surface *shsurf)
1521{
Alex Wu4539b082012-03-01 12:57:46 +08001522 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001523 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001524}
1525
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001526static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001527shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001528 struct wl_resource *resource,
1529 uint32_t method,
1530 uint32_t framerate,
1531 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001532{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001533 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001534 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001535
1536 if (output_resource)
1537 shsurf->output = output_resource->data;
1538 else
1539 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001540
Alex Wu4539b082012-03-01 12:57:46 +08001541 shsurf->fullscreen_output = shsurf->output;
1542 shsurf->fullscreen.type = method;
1543 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001544 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001545
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001546 shsurf->client->send_configure(shsurf->surface, 0,
1547 shsurf->output->current->width,
1548 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001549}
1550
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001551static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001552popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001553 struct wl_surface *surface,
1554 wl_fixed_t x,
1555 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001556{
Daniel Stone37816df2012-05-16 18:45:18 +01001557 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001558 struct shell_surface *priv =
1559 container_of(grab, struct shell_surface, popup.grab);
1560 struct wl_client *client = priv->surface->surface.resource.client;
1561
Pekka Paalanencb108432012-01-19 16:25:40 +02001562 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001563 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001564 grab->focus = surface;
1565 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001566 wl_pointer_set_focus(pointer, NULL,
1567 wl_fixed_from_int(0),
1568 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001569 grab->focus = NULL;
1570 }
1571}
1572
1573static void
Scott Moreau447013d2012-02-18 05:05:29 -07001574popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001575 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001576{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001577 struct wl_resource *resource;
1578
Daniel Stone37816df2012-05-16 18:45:18 +01001579 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001580 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001581 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001582}
1583
1584static void
Scott Moreau447013d2012-02-18 05:05:29 -07001585popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001586 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001587{
1588 struct wl_resource *resource;
1589 struct shell_surface *shsurf =
1590 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001591 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001592 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001593 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001594
Daniel Stone37816df2012-05-16 18:45:18 +01001595 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001596 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001597 display = wl_client_get_display(resource->client);
1598 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001599 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001600 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001601 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001602 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001603 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001604 wl_pointer_end_grab(grab->pointer);
1605 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001606 }
1607
Daniel Stone4dbadb12012-05-30 16:31:51 +01001608 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001609 shsurf->popup.initial_up = 1;
1610}
1611
Scott Moreau447013d2012-02-18 05:05:29 -07001612static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001613 popup_grab_focus,
1614 popup_grab_motion,
1615 popup_grab_button,
1616};
1617
1618static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001619shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001620{
Daniel Stone37816df2012-05-16 18:45:18 +01001621 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001622 struct weston_surface *es = shsurf->surface;
1623 struct weston_surface *parent = shsurf->parent->surface;
1624
1625 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001626 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001627
Pekka Paalanen938269a2012-02-07 14:19:01 +02001628 weston_surface_update_transform(parent);
1629 if (parent->transform.enabled) {
1630 shsurf->popup.parent_transform.matrix =
1631 parent->transform.matrix;
1632 } else {
1633 /* construct x, y translation matrix */
1634 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1635 shsurf->popup.parent_transform.matrix.d[12] =
1636 parent->geometry.x;
1637 shsurf->popup.parent_transform.matrix.d[13] =
1638 parent->geometry.y;
1639 }
1640 wl_list_insert(es->geometry.transformation_list.prev,
1641 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001642 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001643
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001644 shsurf->popup.initial_up = 0;
1645
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001646 /* We don't require the grab to still be active, but if another
1647 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001648 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1649 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001650 } else {
1651 wl_shell_surface_send_popup_done(&shsurf->resource);
1652 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001653}
1654
1655static void
1656shell_surface_set_popup(struct wl_client *client,
1657 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001658 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001659 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001660 struct wl_resource *parent_resource,
1661 int32_t x, int32_t y, uint32_t flags)
1662{
1663 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001664
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001665 shsurf->type = SHELL_SURFACE_POPUP;
1666 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001667 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001668 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001669 shsurf->popup.x = x;
1670 shsurf->popup.y = y;
1671}
1672
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001673static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001674 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001675 shell_surface_move,
1676 shell_surface_resize,
1677 shell_surface_set_toplevel,
1678 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001679 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001680 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001681 shell_surface_set_maximized,
1682 shell_surface_set_title,
1683 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001684};
1685
1686static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001687destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001688{
Daniel Stone37816df2012-05-16 18:45:18 +01001689 if (shsurf->popup.grab.pointer)
1690 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001691
Alex Wubd3354b2012-04-17 17:20:49 +08001692 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1693 shell_surface_is_top_fullscreen(shsurf)) {
1694 weston_output_switch_mode(shsurf->fullscreen_output,
1695 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001696 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001697
Alex Wuaa08e2d2012-03-05 11:01:40 +08001698 if (shsurf->fullscreen.black_surface)
1699 weston_surface_destroy(shsurf->fullscreen.black_surface);
1700
Alex Wubd3354b2012-04-17 17:20:49 +08001701 /* As destroy_resource() use wl_list_for_each_safe(),
1702 * we can always remove the listener.
1703 */
1704 wl_list_remove(&shsurf->surface_destroy_listener.link);
1705 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001706 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001707
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001708 wl_list_remove(&shsurf->link);
1709 free(shsurf);
1710}
1711
1712static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001713shell_destroy_shell_surface(struct wl_resource *resource)
1714{
1715 struct shell_surface *shsurf = resource->data;
1716
1717 destroy_shell_surface(shsurf);
1718}
1719
1720static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001721shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001722{
1723 struct shell_surface *shsurf = container_of(listener,
1724 struct shell_surface,
1725 surface_destroy_listener);
1726
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001727 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001728 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001729 } else {
1730 wl_signal_emit(&shsurf->resource.destroy_signal,
1731 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001732 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001733 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001734}
1735
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001736static void
1737shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1738
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001739static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001740get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001741{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001742 if (surface->configure == shell_surface_configure)
1743 return surface->private;
1744 else
1745 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001746}
1747
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001748static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001749create_shell_surface(void *shell, struct weston_surface *surface,
1750 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001751{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001752 struct shell_surface *shsurf;
1753
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001754 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001755 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001756 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001757 }
1758
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001759 shsurf = calloc(1, sizeof *shsurf);
1760 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001761 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001762 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001763 }
1764
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001765 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001766 surface->private = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001767 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001768
Tiago Vignattibc052c92012-04-19 16:18:18 +03001769 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001770 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001771 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001772 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001773 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001774 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1775 shsurf->fullscreen.framerate = 0;
1776 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001777 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001778 wl_list_init(&shsurf->fullscreen.transform.link);
1779
Tiago Vignattibc052c92012-04-19 16:18:18 +03001780 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001781 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1782 wl_signal_add(&surface->surface.resource.destroy_signal,
1783 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001784
1785 /* init link so its safe to always remove it in destroy_shell_surface */
1786 wl_list_init(&shsurf->link);
1787
Pekka Paalanen460099f2012-01-20 16:48:25 +02001788 /* empty when not in use */
1789 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001790 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001791
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001792 wl_list_init(&shsurf->workspace_transform.link);
1793
Pekka Paalanen98262232011-12-01 10:42:22 +02001794 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001795 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001796
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001797 shsurf->client = client;
1798
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001799 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001800}
1801
1802static void
1803shell_get_shell_surface(struct wl_client *client,
1804 struct wl_resource *resource,
1805 uint32_t id,
1806 struct wl_resource *surface_resource)
1807{
1808 struct weston_surface *surface = surface_resource->data;
1809 struct desktop_shell *shell = resource->data;
1810 struct shell_surface *shsurf;
1811
1812 if (get_shell_surface(surface)) {
1813 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001814 WL_DISPLAY_ERROR_INVALID_OBJECT,
1815 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001816 return;
1817 }
1818
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001819 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001820 if (!shsurf) {
1821 wl_resource_post_error(surface_resource,
1822 WL_DISPLAY_ERROR_INVALID_OBJECT,
1823 "surface->configure already set");
1824 return;
1825 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001826
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001827 shsurf->resource.destroy = shell_destroy_shell_surface;
1828 shsurf->resource.object.id = id;
1829 shsurf->resource.object.interface = &wl_shell_surface_interface;
1830 shsurf->resource.object.implementation =
1831 (void (**)(void)) &shell_surface_implementation;
1832 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001833
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001834 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001835}
1836
1837static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001838 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001839};
1840
Kristian Høgsberg07937562011-04-12 17:25:42 -04001841static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001842handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001843{
1844 proc->pid = 0;
1845}
1846
1847static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001848launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001849{
1850 if (shell->screensaver.binding)
1851 return;
1852
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001853 if (!shell->screensaver.path)
1854 return;
1855
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001856 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001857 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001858 return;
1859 }
1860
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001861 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001862 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001863 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001864 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001865}
1866
1867static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001868terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001869{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001870 if (shell->screensaver.process.pid == 0)
1871 return;
1872
1873 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001874}
1875
1876static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001877show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001878{
1879 struct wl_list *list;
1880
1881 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001882 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001883 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001884 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001885
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001886 wl_list_remove(&surface->surface->layer_link);
1887 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001888 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001889 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001890}
1891
1892static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001893hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001894{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001895 wl_list_remove(&surface->surface->layer_link);
1896 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001897 surface->surface->output = NULL;
1898}
1899
1900static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02001901show_input_panel(struct desktop_shell *shell, struct shell_surface *surface)
1902{
Kristian Høgsberg1ce6a2a2012-06-21 22:34:39 -04001903 if (weston_surface_is_mapped(surface->surface))
1904 return;
1905
Jan Arne Petersen42feced2012-06-21 21:52:17 +02001906 wl_list_remove(&surface->surface->layer_link);
1907 wl_list_insert(&shell->panel_layer.surface_list, &surface->surface->layer_link);
1908 surface->surface->output = surface->output;
1909 weston_surface_damage(surface->surface);
Kristian Høgsberg414bd422012-06-21 22:07:30 -04001910
Kristian Høgsberg1ce6a2a2012-06-21 22:34:39 -04001911 weston_slide_run(surface->surface,
1912 surface->surface->geometry.height, 0,
1913 NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02001914}
1915
1916static void
1917hide_input_panel(struct desktop_shell *shell, struct shell_surface *surface)
1918{
1919 weston_surface_damage_below(surface->surface);
1920 wl_list_remove(&surface->surface->layer_link);
1921 wl_list_init(&surface->surface->layer_link);
1922 surface->surface->output = NULL;
1923
1924 weston_compositor_schedule_repaint(surface->surface->compositor);
1925}
1926
1927static void
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001928background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1929{
1930 struct desktop_shell *shell = es->private;
1931 struct weston_surface *s;
1932
1933 wl_list_for_each(s, &shell->background_layer.surface_list, layer_link) {
1934 if (s->output == es->output) {
1935 s->output = NULL;
1936 wl_list_remove(&s->layer_link);
1937 s->configure = NULL;
1938 break;
1939 }
1940 }
1941
1942 weston_surface_configure(es, es->output->x, es->output->y,
1943 es->buffer->width, es->buffer->height);
1944
1945 if (wl_list_empty(&es->layer_link)) {
1946 wl_list_insert(&shell->background_layer.surface_list,
1947 &es->layer_link);
1948 weston_surface_assign_output(es);
1949 }
1950}
1951
1952static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001953desktop_shell_set_background(struct wl_client *client,
1954 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001955 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001956 struct wl_resource *surface_resource)
1957{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001958 struct desktop_shell *shell = resource->data;
1959 struct weston_surface *surface = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001960
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001961 if (surface->configure) {
1962 wl_resource_post_error(surface_resource,
1963 WL_DISPLAY_ERROR_INVALID_OBJECT,
1964 "surface role already assigned");
1965 return;
1966 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001967
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001968 surface->configure = background_configure;
1969 surface->private = shell;
1970 surface->output = output_resource->data;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001971 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001972 surface_resource,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001973 surface->output->current->width,
1974 surface->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001975}
1976
1977static void
1978desktop_shell_set_panel(struct wl_client *client,
1979 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001980 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001981 struct wl_resource *surface_resource)
1982{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001983 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001984
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001985 shsurf->next_type = SHELL_SURFACE_PANEL;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001986 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001987
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001988 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001989 &shsurf->surface->surface.resource,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001990 shsurf->output->current->width,
1991 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001992}
1993
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001994static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001995handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001996{
Tiago Vignattibe143262012-04-16 17:31:41 +03001997 struct desktop_shell *shell =
1998 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001999
Martin Minarik6d118362012-06-07 18:01:59 +02002000 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002001 shell->lock_surface = NULL;
2002}
2003
2004static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002005desktop_shell_set_lock_surface(struct wl_client *client,
2006 struct wl_resource *resource,
2007 struct wl_resource *surface_resource)
2008{
Tiago Vignattibe143262012-04-16 17:31:41 +03002009 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02002010 struct shell_surface *surface = surface_resource->data;
2011
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002012 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002013
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002014 if (!shell->locked)
2015 return;
2016
Pekka Paalanen98262232011-12-01 10:42:22 +02002017 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002018
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002019 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2020 wl_signal_add(&surface_resource->destroy_signal,
2021 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002022
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002023 shell->lock_surface->next_type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002024}
2025
2026static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002027resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002028{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002029 struct shell_surface *tmp;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002030 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002031
2032 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
2033 hide_screensaver(shell, tmp);
2034
2035 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002036
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002037 wl_list_remove(&shell->lock_layer.link);
2038 wl_list_insert(&shell->compositor->cursor_layer.link,
2039 &shell->fullscreen_layer.link);
2040 wl_list_insert(&shell->fullscreen_layer.link,
2041 &shell->panel_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002042 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002043
Jonas Ådahl04769742012-06-13 00:01:24 +02002044 pop_focus_state(shell, get_current_workspace(shell));
2045
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002046 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002047 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002048 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002049 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002050}
2051
2052static void
2053desktop_shell_unlock(struct wl_client *client,
2054 struct wl_resource *resource)
2055{
Tiago Vignattibe143262012-04-16 17:31:41 +03002056 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002057
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002058 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002059
2060 if (shell->locked)
2061 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002062}
2063
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002064static void
2065desktop_shell_set_busy_surface(struct wl_client *client,
2066 struct wl_resource *resource,
2067 struct wl_resource *surface_resource)
2068{
2069 struct desktop_shell *shell = resource->data;
2070
2071 shell->busy_surface = surface_resource->data;
2072}
2073
Kristian Høgsberg75840622011-09-06 13:48:16 -04002074static const struct desktop_shell_interface desktop_shell_implementation = {
2075 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002076 desktop_shell_set_panel,
2077 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002078 desktop_shell_unlock,
2079 desktop_shell_set_busy_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002080};
2081
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002082static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002083get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002084{
2085 struct shell_surface *shsurf;
2086
2087 shsurf = get_shell_surface(surface);
2088 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002089 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002090 return shsurf->type;
2091}
2092
Kristian Høgsberg75840622011-09-06 13:48:16 -04002093static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002094move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002095{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002096 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002097 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002098 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002099
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002100 if (surface == NULL)
2101 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002102
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002103 shsurf = get_shell_surface(surface);
2104 if (shsurf == NULL)
2105 return;
2106
2107 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002108 case SHELL_SURFACE_PANEL:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002109 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02002110 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberga4a4de42012-06-21 16:46:14 -04002111 case SHELL_SURFACE_INPUT_PANEL:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002112 return;
2113 default:
2114 break;
2115 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04002116
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002117 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002118}
2119
2120static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002121resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002122{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002123 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002124 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002125 uint32_t edges = 0;
2126 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002127 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002128
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002129 if (surface == NULL)
2130 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002131
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002132 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002133 if (!shsurf)
2134 return;
2135
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002136 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002137 case SHELL_SURFACE_PANEL:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002138 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02002139 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberga4a4de42012-06-21 16:46:14 -04002140 case SHELL_SURFACE_INPUT_PANEL:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002141 return;
2142 default:
2143 break;
2144 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04002145
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002146 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002147 wl_fixed_to_int(seat->pointer->grab_x),
2148 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002149 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002150
Pekka Paalanen60921e52012-01-25 15:55:43 +02002151 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002152 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002153 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002154 edges |= 0;
2155 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002156 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002157
Pekka Paalanen60921e52012-01-25 15:55:43 +02002158 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002159 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002160 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002161 edges |= 0;
2162 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002163 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002164
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002165 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002166}
2167
2168static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002169surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002170 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002171{
Scott Moreau02709af2012-05-22 01:54:10 -06002172 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002173 struct shell_surface *shsurf;
2174 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002175 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002176
2177 if (surface == NULL)
2178 return;
2179
2180 shsurf = get_shell_surface(surface);
2181 if (!shsurf)
2182 return;
2183
2184 switch (shsurf->type) {
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002185 case SHELL_SURFACE_SCREENSAVER:
2186 return;
2187 default:
2188 break;
2189 }
2190
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002191 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002192
Scott Moreau02709af2012-05-22 01:54:10 -06002193 if (surface->alpha > 1.0)
2194 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002195 if (surface->alpha < step)
2196 surface->alpha = step;
2197
2198 surface->geometry.dirty = 1;
2199 weston_surface_damage(surface);
2200}
2201
2202static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002203do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002204 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002205{
Daniel Stone37816df2012-05-16 18:45:18 +01002206 struct weston_seat *ws = (struct weston_seat *) seat;
2207 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002208 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002209 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002210
2211 wl_list_for_each(output, &compositor->output_list, link) {
2212 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002213 wl_fixed_to_double(seat->pointer->x),
2214 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002215 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002216 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002217 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002218 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002219 increment = -output->zoom.increment;
2220 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002221 increment = output->zoom.increment *
2222 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002223 else
2224 increment = 0;
2225
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002226 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002227
Scott Moreaue6603982012-06-11 13:07:51 -06002228 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002229 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002230 else if (output->zoom.level > output->zoom.max_level)
2231 output->zoom.level = output->zoom.max_level;
2232 else
2233 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002234
Scott Moreaue6603982012-06-11 13:07:51 -06002235 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002236
Scott Moreau8dacaab2012-06-17 18:10:58 -06002237 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002238 }
2239 }
2240}
2241
Scott Moreauccbf29d2012-02-22 14:21:41 -07002242static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002243zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002244 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002245{
2246 do_zoom(seat, time, 0, axis, value);
2247}
2248
2249static void
2250zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2251 void *data)
2252{
2253 do_zoom(seat, time, key, 0, 0);
2254}
2255
2256static void
2257terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2258 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002259{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002260 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002261
Daniel Stone325fc2d2012-05-30 16:31:58 +01002262 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002263}
2264
2265static void
Scott Moreau447013d2012-02-18 05:05:29 -07002266rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002267 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002268{
2269 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002270 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002271 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002272 struct shell_surface *shsurf = rotate->base.shsurf;
2273 struct weston_surface *surface;
2274 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2275
2276 if (!shsurf)
2277 return;
2278
2279 surface = shsurf->surface;
2280
2281 cx = 0.5f * surface->geometry.width;
2282 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002283
Daniel Stone37816df2012-05-16 18:45:18 +01002284 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2285 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002286 r = sqrtf(dx * dx + dy * dy);
2287
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002288 wl_list_remove(&shsurf->rotation.transform.link);
2289 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002290
2291 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002292 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002293 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002294
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002295 weston_matrix_init(&rotate->rotation);
2296 rotate->rotation.d[0] = dx / r;
2297 rotate->rotation.d[4] = -dy / r;
2298 rotate->rotation.d[1] = -rotate->rotation.d[4];
2299 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002300
2301 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002302 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002303 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002304 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002305 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002306
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002307 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002308 &shsurf->surface->geometry.transformation_list,
2309 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002310 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002311 wl_list_init(&shsurf->rotation.transform.link);
2312 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002313 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002314 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002315
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002316 /* We need to adjust the position of the surface
2317 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002318 cposx = surface->geometry.x + cx;
2319 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002320 dposx = rotate->center.x - cposx;
2321 dposy = rotate->center.y - cposy;
2322 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002323 weston_surface_set_position(surface,
2324 surface->geometry.x + dposx,
2325 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002326 }
2327
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002328 /* Repaint implies weston_surface_update_transform(), which
2329 * lazily applies the damage due to rotation update.
2330 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002331 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002332}
2333
2334static void
Scott Moreau447013d2012-02-18 05:05:29 -07002335rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002336 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002337{
2338 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002339 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002340 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002341 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002342 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002343
Daniel Stone4dbadb12012-05-30 16:31:51 +01002344 if (pointer->button_count == 0 &&
2345 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002346 if (shsurf)
2347 weston_matrix_multiply(&shsurf->rotation.rotation,
2348 &rotate->rotation);
2349 shell_grab_finish(&rotate->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002350 wl_pointer_end_grab(pointer);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002351 free(rotate);
2352 }
2353}
2354
Scott Moreau447013d2012-02-18 05:05:29 -07002355static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002356 noop_grab_focus,
2357 rotate_grab_motion,
2358 rotate_grab_button,
2359};
2360
2361static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002362rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002363 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002364{
2365 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002366 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002367 struct shell_surface *surface;
2368 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002369 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002370 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002371
2372 if (base_surface == NULL)
2373 return;
2374
2375 surface = get_shell_surface(base_surface);
2376 if (!surface)
2377 return;
2378
2379 switch (surface->type) {
2380 case SHELL_SURFACE_PANEL:
Pekka Paalanen460099f2012-01-20 16:48:25 +02002381 case SHELL_SURFACE_FULLSCREEN:
2382 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberga4a4de42012-06-21 16:46:14 -04002383 case SHELL_SURFACE_INPUT_PANEL:
Pekka Paalanen460099f2012-01-20 16:48:25 +02002384 return;
2385 default:
2386 break;
2387 }
2388
Pekka Paalanen460099f2012-01-20 16:48:25 +02002389 rotate = malloc(sizeof *rotate);
2390 if (!rotate)
2391 return;
2392
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002393 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02002394
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002395 weston_surface_to_global_float(surface->surface,
2396 surface->surface->geometry.width / 2,
2397 surface->surface->geometry.height / 2,
2398 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002399
Daniel Stone37816df2012-05-16 18:45:18 +01002400 wl_pointer_start_grab(seat->pointer, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002401
Daniel Stone37816df2012-05-16 18:45:18 +01002402 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2403 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002404 r = sqrtf(dx * dx + dy * dy);
2405 if (r > 20.0f) {
2406 struct weston_matrix inverse;
2407
2408 weston_matrix_init(&inverse);
2409 inverse.d[0] = dx / r;
2410 inverse.d[4] = dy / r;
2411 inverse.d[1] = -inverse.d[4];
2412 inverse.d[5] = inverse.d[0];
2413 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002414
2415 weston_matrix_init(&rotate->rotation);
2416 rotate->rotation.d[0] = dx / r;
2417 rotate->rotation.d[4] = -dy / r;
2418 rotate->rotation.d[1] = -rotate->rotation.d[4];
2419 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002420 } else {
2421 weston_matrix_init(&surface->rotation.rotation);
2422 weston_matrix_init(&rotate->rotation);
2423 }
2424
Daniel Stone37816df2012-05-16 18:45:18 +01002425 wl_pointer_set_focus(seat->pointer, NULL,
2426 wl_fixed_from_int(0),
2427 wl_fixed_from_int(0));
Pekka Paalanen460099f2012-01-20 16:48:25 +02002428}
2429
2430static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002431lower_fullscreen_layer(struct desktop_shell *shell)
2432{
2433 struct workspace *ws;
2434 struct weston_surface *surface, *prev;
2435
2436 ws = get_current_workspace(shell);
2437 wl_list_for_each_reverse_safe(surface, prev,
2438 &shell->fullscreen_layer.surface_list,
2439 layer_link)
2440 weston_surface_restack(surface, &ws->layer.surface_list);
2441}
2442
2443static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002444activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002445 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002446{
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002447 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002448
Daniel Stone37816df2012-05-16 18:45:18 +01002449 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002450
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002451 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002452 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002453 case SHELL_SURFACE_LOCK:
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002454 case SHELL_SURFACE_INPUT_PANEL:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002455 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002456
Pekka Paalanen77346a62011-11-30 16:26:35 +02002457 case SHELL_SURFACE_SCREENSAVER:
2458 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002459 if (shell->lock_surface)
2460 weston_surface_restack(es,
2461 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002462 break;
Alex Wu4539b082012-03-01 12:57:46 +08002463 case SHELL_SURFACE_FULLSCREEN:
2464 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002465 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002466 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002467 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002468 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002469 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002470 lower_fullscreen_layer(shell);
2471 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002472 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002473 }
2474}
2475
Alex Wu21858432012-04-01 20:13:08 +08002476/* no-op func for checking black surface */
2477static void
2478black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2479{
2480}
2481
2482static bool
2483is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2484{
2485 if (es->configure == black_surface_configure) {
2486 if (fs_surface)
2487 *fs_surface = (struct weston_surface *)es->private;
2488 return true;
2489 }
2490 return false;
2491}
2492
Kristian Høgsberg75840622011-09-06 13:48:16 -04002493static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002494click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002495 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002496{
Daniel Stone37816df2012-05-16 18:45:18 +01002497 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002498 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002499 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002500 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002501
Daniel Stone37816df2012-05-16 18:45:18 +01002502 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002503 if (!focus)
2504 return;
2505
Alex Wu21858432012-04-01 20:13:08 +08002506 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002507 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002508
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002509 switch (get_shell_surface_type(focus)) {
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002510 case SHELL_SURFACE_SCREENSAVER:
2511 case SHELL_SURFACE_INPUT_PANEL:
2512 return;
2513 default:
2514 break;
2515 }
2516
Daniel Stone325fc2d2012-05-30 16:31:58 +01002517 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002518 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002519}
2520
2521static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002522lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002523{
Tiago Vignattibe143262012-04-16 17:31:41 +03002524 struct desktop_shell *shell =
2525 container_of(listener, struct desktop_shell, lock_listener);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002526 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002527 struct weston_output *output;
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002528 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002529
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002530 if (shell->locked) {
2531 wl_list_for_each(output, &shell->compositor->output_list, link)
2532 /* TODO: find a way to jump to other DPMS levels */
2533 if (output->set_dpms)
2534 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002535 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002536 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002537
2538 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002539
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002540 /* Hide all surfaces by removing the fullscreen, panel and
2541 * toplevel layers. This way nothing else can show or receive
2542 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002543
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002544 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002545 wl_list_remove(&shell->fullscreen_layer.link);
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04002546 wl_list_remove(&ws->layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002547 wl_list_insert(&shell->compositor->cursor_layer.link,
2548 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002549
Pekka Paalanen77346a62011-11-30 16:26:35 +02002550 launch_screensaver(shell);
2551
2552 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
2553 show_screensaver(shell, shsurf);
2554
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002555 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02002556 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002557 weston_compositor_wake(shell->compositor);
2558 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002559 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002560
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002561 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002562 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002563
Jonas Ådahl04769742012-06-13 00:01:24 +02002564 /* stash keyboard foci in current workspace */
2565 push_focus_state(shell, get_current_workspace(shell));
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002566
2567 /* TODO: disable bindings that should not work while locked. */
2568
2569 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002570}
2571
2572static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002573unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002574{
Tiago Vignattibe143262012-04-16 17:31:41 +03002575 struct desktop_shell *shell =
2576 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002577
Pekka Paalanend81c2162011-11-16 13:47:34 +02002578 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002579 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002580 return;
2581 }
2582
2583 /* If desktop-shell client has gone away, unlock immediately. */
2584 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002585 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002586 return;
2587 }
2588
2589 if (shell->prepare_event_sent)
2590 return;
2591
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002592 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002593 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002594}
2595
2596static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002597show_input_panels(struct wl_listener *listener, void *data)
2598{
2599 struct desktop_shell *shell =
2600 container_of(listener, struct desktop_shell, show_input_panel_listener);
2601 struct shell_surface *priv;
2602
2603 wl_list_for_each(priv, &shell->input_panel.surfaces, link) {
2604 show_input_panel(shell, priv);
2605 }
2606}
2607
2608static void
2609hide_input_panels(struct wl_listener *listener, void *data)
2610{
2611 struct desktop_shell *shell =
2612 container_of(listener, struct desktop_shell, hide_input_panel_listener);
2613 struct shell_surface *priv;
2614
2615 wl_list_for_each(priv, &shell->input_panel.surfaces, link) {
2616 hide_input_panel(shell, priv);
2617 }
2618}
2619
2620static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002621center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002622{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002623 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002624 GLfloat x = (mode->width - surface->geometry.width) / 2;
2625 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002626
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002627 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002628}
2629
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002630
2631static void
2632bottom_center_on_output(struct weston_surface *surface, struct weston_output *output)
2633{
2634 struct weston_mode *mode = output->current;
2635 GLfloat x = (mode->width - surface->geometry.width) / 2;
2636 GLfloat y = mode->height - surface->geometry.height;
2637
2638 weston_surface_set_position(surface, output->x + x, output->y + y);
2639}
2640
Pekka Paalanen77346a62011-11-30 16:26:35 +02002641static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002642map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002643 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002644{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002645 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002646 struct shell_surface *shsurf = get_shell_surface(surface);
2647 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002648 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002649 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002650 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002651 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002652
Pekka Paalanen60921e52012-01-25 15:55:43 +02002653 surface->geometry.width = width;
2654 surface->geometry.height = height;
2655 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002656
2657 /* initial positioning, see also configure() */
2658 switch (surface_type) {
2659 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002660 weston_surface_set_position(surface, 10 + random() % 400,
2661 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002662 break;
2663 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002664 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002665 break;
Alex Wu4539b082012-03-01 12:57:46 +08002666 case SHELL_SURFACE_FULLSCREEN:
2667 shell_map_fullscreen(shsurf);
2668 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002669 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002670 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002671 panel_height = get_output_panel_height(shell,surface->output);
2672 weston_surface_set_position(surface, surface->output->x,
2673 surface->output->y + panel_height);
2674 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002675 case SHELL_SURFACE_LOCK:
2676 center_on_output(surface, get_default_output(compositor));
2677 break;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002678 case SHELL_SURFACE_INPUT_PANEL:
2679 bottom_center_on_output(surface, get_default_output(compositor));
Kristian Høgsberg1ce6a2a2012-06-21 22:34:39 -04002680 /* Don't map the input panel here, wait for
2681 * show_input_panels signal. */
2682 return;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002683 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002684 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002685 case SHELL_SURFACE_NONE:
2686 weston_surface_set_position(surface,
2687 surface->geometry.x + sx,
2688 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002689 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002690 default:
2691 ;
2692 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002693
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002694 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002695 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002696 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002697 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002698 wl_list_insert(&shell->panel_layer.surface_list,
2699 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002700 break;
2701 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002702 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002703 wl_list_insert(&shell->lock_layer.surface_list,
2704 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002705 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002706 break;
2707 case SHELL_SURFACE_SCREENSAVER:
2708 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002709 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002710 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002711 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002712 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002713 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002714 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002715 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002716 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002717 case SHELL_SURFACE_POPUP:
2718 case SHELL_SURFACE_TRANSIENT:
2719 parent = shsurf->parent->surface;
2720 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2721 break;
Alex Wu4539b082012-03-01 12:57:46 +08002722 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002723 case SHELL_SURFACE_NONE:
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002724 case SHELL_SURFACE_INPUT_PANEL:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002725 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002726 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002727 ws = get_current_workspace(shell);
2728 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002729 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002730 }
2731
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002732 if (surface_type != SHELL_SURFACE_NONE) {
2733 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002734 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2735 surface->output = shsurf->output;
2736 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002737
Juan Zhao7bb92f02011-12-15 11:31:51 -05002738 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002739 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002740 if (shsurf->transient.flags ==
2741 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2742 break;
2743 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002744 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002745 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002746 if (!shell->locked) {
2747 wl_list_for_each(seat, &compositor->seat_list, link)
2748 activate(shell, surface, seat);
2749 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002750 break;
2751 default:
2752 break;
2753 }
2754
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002755 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002756 {
2757 switch (shell->win_animation_type) {
2758 case ANIMATION_FADE:
2759 weston_fade_run(surface, NULL, NULL);
2760 break;
2761 case ANIMATION_ZOOM:
2762 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2763 break;
2764 default:
2765 break;
2766 }
2767 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002768}
2769
2770static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002771configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002772 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002773{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002774 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2775 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002776
Pekka Paalanen77346a62011-11-30 16:26:35 +02002777 shsurf = get_shell_surface(surface);
2778 if (shsurf)
2779 surface_type = shsurf->type;
2780
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002781 surface->geometry.x = x;
2782 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002783 surface->geometry.width = width;
2784 surface->geometry.height = height;
2785 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002786
2787 switch (surface_type) {
2788 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002789 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002790 break;
Alex Wu4539b082012-03-01 12:57:46 +08002791 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002792 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002793 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002794 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002795 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002796 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002797 surface->geometry.x = surface->output->x;
2798 surface->geometry.y = surface->output->y +
2799 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002800 break;
Alex Wu4539b082012-03-01 12:57:46 +08002801 case SHELL_SURFACE_TOPLEVEL:
2802 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002803 default:
2804 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002805 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002806
Alex Wu4539b082012-03-01 12:57:46 +08002807 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002808 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002809 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002810
2811 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2812 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002813 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2814 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002815 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002816}
2817
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002818static void
2819shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2820{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002821 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002822 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002823 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002824
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002825 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002826 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002827 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002828 type_changed = 1;
2829 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002830
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002831 if (!weston_surface_is_mapped(es)) {
2832 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002833 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002834 es->geometry.width != es->buffer->width ||
2835 es->geometry.height != es->buffer->height) {
2836 GLfloat from_x, from_y;
2837 GLfloat to_x, to_y;
2838
2839 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2840 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2841 configure(shell, es,
2842 es->geometry.x + to_x - from_x,
2843 es->geometry.y + to_y - from_y,
2844 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002845 }
2846}
2847
Tiago Vignattibe143262012-04-16 17:31:41 +03002848static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002849
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002850static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002851desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002852{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002853 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002854 struct desktop_shell *shell =
2855 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002856
2857 shell->child.process.pid = 0;
2858 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002859
2860 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2861 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002862 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002863 shell->child.deathstamp = time;
2864 shell->child.deathcount = 0;
2865 }
2866
2867 shell->child.deathcount++;
2868 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002869 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002870 return;
2871 }
2872
Martin Minarik6d118362012-06-07 18:01:59 +02002873 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002874 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002875}
2876
2877static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002878launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002879{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002880 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002881
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002882 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002883 &shell->child.process,
2884 shell_exe,
2885 desktop_shell_sigchld);
2886
2887 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002888 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002889 return 0;
2890}
2891
2892static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002893bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2894{
Tiago Vignattibe143262012-04-16 17:31:41 +03002895 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002896
2897 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002898 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002899}
2900
Kristian Høgsberg75840622011-09-06 13:48:16 -04002901static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002902unbind_desktop_shell(struct wl_resource *resource)
2903{
Tiago Vignattibe143262012-04-16 17:31:41 +03002904 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002905
2906 if (shell->locked)
2907 resume_desktop(shell);
2908
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002909 shell->child.desktop_shell = NULL;
2910 shell->prepare_event_sent = false;
2911 free(resource);
2912}
2913
2914static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002915bind_desktop_shell(struct wl_client *client,
2916 void *data, uint32_t version, uint32_t id)
2917{
Tiago Vignattibe143262012-04-16 17:31:41 +03002918 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002919 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002920
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002921 resource = wl_client_add_object(client, &desktop_shell_interface,
2922 &desktop_shell_implementation,
2923 id, shell);
2924
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002925 if (client == shell->child.client) {
2926 resource->destroy = unbind_desktop_shell;
2927 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002928 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002929 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002930
2931 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2932 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002933 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002934}
2935
Pekka Paalanen6e168112011-11-24 11:34:05 +02002936static void
2937screensaver_set_surface(struct wl_client *client,
2938 struct wl_resource *resource,
2939 struct wl_resource *shell_surface_resource,
2940 struct wl_resource *output_resource)
2941{
Tiago Vignattibe143262012-04-16 17:31:41 +03002942 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002943 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002944 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002945
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002946 surface->next_type = SHELL_SURFACE_SCREENSAVER;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002947
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002948 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002949 surface->output = output;
2950 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002951}
2952
2953static const struct screensaver_interface screensaver_implementation = {
2954 screensaver_set_surface
2955};
2956
2957static void
2958unbind_screensaver(struct wl_resource *resource)
2959{
Tiago Vignattibe143262012-04-16 17:31:41 +03002960 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002961
Pekka Paalanen77346a62011-11-30 16:26:35 +02002962 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002963 free(resource);
2964}
2965
2966static void
2967bind_screensaver(struct wl_client *client,
2968 void *data, uint32_t version, uint32_t id)
2969{
Tiago Vignattibe143262012-04-16 17:31:41 +03002970 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002971 struct wl_resource *resource;
2972
2973 resource = wl_client_add_object(client, &screensaver_interface,
2974 &screensaver_implementation,
2975 id, shell);
2976
Pekka Paalanen77346a62011-11-30 16:26:35 +02002977 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002978 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002979 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002980 return;
2981 }
2982
2983 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2984 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002985 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002986}
2987
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002988static void
2989input_panel_set_surface(struct wl_client *client,
2990 struct wl_resource *resource,
2991 struct wl_resource *shell_surface_resource,
2992 struct wl_resource *output_resource)
2993{
2994 struct desktop_shell *shell = resource->data;
2995 struct shell_surface *surface = shell_surface_resource->data;
2996 struct weston_output *output = output_resource->data;
2997
2998 surface->next_type = SHELL_SURFACE_INPUT_PANEL;
2999
3000 surface->fullscreen_output = output;
3001 surface->output = output;
3002 wl_list_insert(shell->input_panel.surfaces.prev, &surface->link);
3003}
3004
3005static const struct input_panel_interface input_panel_implementation = {
3006 input_panel_set_surface
3007};
3008
3009static void
3010unbind_input_panel(struct wl_resource *resource)
3011{
3012 struct desktop_shell *shell = resource->data;
3013
3014 shell->input_panel.binding = NULL;
3015 free(resource);
3016}
3017
3018static void
3019bind_input_panel(struct wl_client *client,
3020 void *data, uint32_t version, uint32_t id)
3021{
3022 struct desktop_shell *shell = data;
3023 struct wl_resource *resource;
3024
3025 resource = wl_client_add_object(client, &input_panel_interface,
3026 &input_panel_implementation,
3027 id, shell);
3028
3029 if (shell->input_panel.binding == NULL) {
3030 resource->destroy = unbind_input_panel;
3031 shell->input_panel.binding = resource;
3032 return;
3033 }
3034
3035 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3036 "interface object already bound");
3037 wl_resource_destroy(resource);
3038}
3039
Kristian Høgsberg07045392012-02-19 18:52:44 -05003040struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003041 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003042 struct weston_surface *current;
3043 struct wl_listener listener;
3044 struct wl_keyboard_grab grab;
3045};
3046
3047static void
3048switcher_next(struct switcher *switcher)
3049{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003050 struct weston_surface *surface;
3051 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003052 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003053 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003054
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003055 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003056 switch (get_shell_surface_type(surface)) {
3057 case SHELL_SURFACE_TOPLEVEL:
3058 case SHELL_SURFACE_FULLSCREEN:
3059 case SHELL_SURFACE_MAXIMIZED:
3060 if (first == NULL)
3061 first = surface;
3062 if (prev == switcher->current)
3063 next = surface;
3064 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003065 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003066 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003067 weston_surface_damage(surface);
3068 break;
3069 default:
3070 break;
3071 }
Alex Wu1659daa2012-04-01 20:13:09 +08003072
3073 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003074 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003075 surface->geometry.dirty = 1;
3076 weston_surface_damage(surface);
3077 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003078 }
3079
3080 if (next == NULL)
3081 next = first;
3082
Alex Wu07b26062012-03-12 16:06:01 +08003083 if (next == NULL)
3084 return;
3085
Kristian Høgsberg07045392012-02-19 18:52:44 -05003086 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003087 wl_signal_add(&next->surface.resource.destroy_signal,
3088 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003089
3090 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003091 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003092
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003093 shsurf = get_shell_surface(switcher->current);
3094 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003095 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003096}
3097
3098static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003099switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003100{
3101 struct switcher *switcher =
3102 container_of(listener, struct switcher, listener);
3103
3104 switcher_next(switcher);
3105}
3106
3107static void
Daniel Stone351eb612012-05-31 15:27:47 -04003108switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003109{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003110 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003111 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003112 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003113
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003114 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003115 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003116 weston_surface_damage(surface);
3117 }
3118
Alex Wu07b26062012-03-12 16:06:01 +08003119 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003120 activate(switcher->shell, switcher->current,
3121 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003122 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003123 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003124 free(switcher);
3125}
3126
3127static void
3128switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003129 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003130{
3131 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003132 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003133
Daniel Stonec9785ea2012-05-30 16:31:52 +01003134 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003135 switcher_next(switcher);
3136}
3137
3138static void
3139switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3140 uint32_t mods_depressed, uint32_t mods_latched,
3141 uint32_t mods_locked, uint32_t group)
3142{
3143 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003144 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003145
Daniel Stone351eb612012-05-31 15:27:47 -04003146 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3147 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003148}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003149
3150static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003151 switcher_key,
3152 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003153};
3154
3155static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003156switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3157 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003158{
Tiago Vignattibe143262012-04-16 17:31:41 +03003159 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003160 struct switcher *switcher;
3161
3162 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003163 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003164 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003165 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003166 wl_list_init(&switcher->listener.link);
3167
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003168 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003169 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003170 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3171 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003172 switcher_next(switcher);
3173}
3174
Pekka Paalanen3c647232011-12-22 13:43:43 +02003175static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003176backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3177 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003178{
3179 struct weston_compositor *compositor = data;
3180 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003181 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003182
3183 /* TODO: we're limiting to simple use cases, where we assume just
3184 * control on the primary display. We'd have to extend later if we
3185 * ever get support for setting backlights on random desktop LCD
3186 * panels though */
3187 output = get_default_output(compositor);
3188 if (!output)
3189 return;
3190
3191 if (!output->set_backlight)
3192 return;
3193
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003194 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3195 backlight_new = output->backlight_current - 25;
3196 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3197 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003198
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003199 if (backlight_new < 5)
3200 backlight_new = 5;
3201 if (backlight_new > 255)
3202 backlight_new = 255;
3203
3204 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003205 output->set_backlight(output, output->backlight_current);
3206}
3207
3208static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003209debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3210 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003211{
Tiago Vignattibe143262012-04-16 17:31:41 +03003212 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003213 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003214 struct weston_surface *surface;
3215
3216 if (shell->debug_repaint_surface) {
3217 weston_surface_destroy(shell->debug_repaint_surface);
3218 shell->debug_repaint_surface = NULL;
3219 } else {
3220 surface = weston_surface_create(compositor);
3221 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3222 weston_surface_configure(surface, 0, 0, 8192, 8192);
3223 wl_list_insert(&compositor->fade_layer.surface_list,
3224 &surface->layer_link);
3225 weston_surface_assign_output(surface);
3226 pixman_region32_init(&surface->input);
3227
3228 /* Here's the dirty little trick that makes the
3229 * repaint debugging work: we force an
3230 * update_transform first to update dependent state
3231 * and clear the geometry.dirty bit. Then we clear
3232 * the surface damage so it only gets repainted
3233 * piecewise as we repaint other things. */
3234
3235 weston_surface_update_transform(surface);
3236 pixman_region32_fini(&surface->damage);
3237 pixman_region32_init(&surface->damage);
3238 shell->debug_repaint_surface = surface;
3239 }
3240}
3241
3242static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003243force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3244 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003245{
3246 struct wl_client *client;
3247 pid_t pid;
3248 uid_t uid;
3249 gid_t gid;
3250
Daniel Stone325fc2d2012-05-30 16:31:58 +01003251 client = seat->keyboard->focus->resource.client;
3252 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003253
Daniel Stone325fc2d2012-05-30 16:31:58 +01003254 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003255}
3256
3257static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003258workspace_up_binding(struct wl_seat *seat, uint32_t time,
3259 uint32_t key, void *data)
3260{
3261 struct desktop_shell *shell = data;
3262 unsigned int new_index = shell->workspaces.current;
3263
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003264 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003265 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003266 if (new_index != 0)
3267 new_index--;
3268
3269 change_workspace(shell, new_index);
3270}
3271
3272static void
3273workspace_down_binding(struct wl_seat *seat, uint32_t time,
3274 uint32_t key, void *data)
3275{
3276 struct desktop_shell *shell = data;
3277 unsigned int new_index = shell->workspaces.current;
3278
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003279 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003280 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003281 if (new_index < shell->workspaces.num - 1)
3282 new_index++;
3283
3284 change_workspace(shell, new_index);
3285}
3286
3287static void
3288workspace_f_binding(struct wl_seat *seat, uint32_t time,
3289 uint32_t key, void *data)
3290{
3291 struct desktop_shell *shell = data;
3292 unsigned int new_index;
3293
Kristian Høgsbergce345b02012-06-25 21:35:29 -04003294 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003295 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003296 new_index = key - KEY_F1;
3297 if (new_index >= shell->workspaces.num)
3298 new_index = shell->workspaces.num - 1;
3299
3300 change_workspace(shell, new_index);
3301}
3302
3303
3304static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003305shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003306{
Tiago Vignattibe143262012-04-16 17:31:41 +03003307 struct desktop_shell *shell =
3308 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003309 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003310
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003311 if (shell->child.client)
3312 wl_client_destroy(shell->child.client);
3313
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003314 wl_list_remove(&shell->lock_listener.link);
3315 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003316 wl_list_remove(&shell->show_input_panel_listener.link);
3317 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003318
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003319 wl_array_for_each(ws, &shell->workspaces.array)
3320 workspace_destroy(*ws);
3321 wl_array_release(&shell->workspaces.array);
3322
Pekka Paalanen3c647232011-12-22 13:43:43 +02003323 free(shell->screensaver.path);
3324 free(shell);
3325}
3326
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003327static void
3328shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3329{
3330 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003331 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003332
3333 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003334 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3335 MODIFIER_CTRL | MODIFIER_ALT,
3336 terminate_binding, ec);
3337 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3338 click_to_activate_binding,
3339 shell);
3340 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3341 MODIFIER_SUPER | MODIFIER_ALT,
3342 surface_opacity_binding, NULL);
3343 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3344 MODIFIER_SUPER, zoom_axis_binding,
3345 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003346
3347 /* configurable bindings */
3348 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003349 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3350 zoom_key_binding, NULL);
3351 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3352 zoom_key_binding, NULL);
3353 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3354 shell);
3355 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3356 resize_binding, shell);
3357 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3358 rotate_binding, NULL);
3359 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3360 shell);
3361 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3362 ec);
3363 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3364 backlight_binding, ec);
3365 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3366 ec);
3367 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3368 backlight_binding, ec);
3369 weston_compositor_add_key_binding(ec, KEY_SPACE, mod,
3370 debug_repaint_binding, shell);
3371 weston_compositor_add_key_binding(ec, KEY_K, mod,
3372 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003373 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3374 workspace_up_binding, shell);
3375 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3376 workspace_down_binding, shell);
3377
3378 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3379 if (shell->workspaces.num > 1) {
3380 num_workspace_bindings = shell->workspaces.num;
3381 if (num_workspace_bindings > 6)
3382 num_workspace_bindings = 6;
3383 for (i = 0; i < num_workspace_bindings; i++)
3384 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3385 workspace_f_binding,
3386 shell);
3387 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003388}
3389
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003390int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003391shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003392
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003393WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003394shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003395{
Tiago Vignattibe143262012-04-16 17:31:41 +03003396 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003397 struct workspace **pws;
3398 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003399
3400 shell = malloc(sizeof *shell);
3401 if (shell == NULL)
3402 return -1;
3403
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003404 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003405 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003406
3407 shell->destroy_listener.notify = shell_destroy;
3408 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3409 shell->lock_listener.notify = lock;
3410 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3411 shell->unlock_listener.notify = unlock;
3412 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003413 shell->show_input_panel_listener.notify = show_input_panels;
3414 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3415 shell->hide_input_panel_listener.notify = hide_input_panels;
3416 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003417 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003418 ec->shell_interface.create_shell_surface = create_shell_surface;
3419 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003420 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003421 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003422 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003423
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003424 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003425 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003426 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003427
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003428 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3429 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003430 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3431 weston_layer_init(&shell->lock_layer, NULL);
3432
3433 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003434
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003435 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003436
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003437 for (i = 0; i < shell->workspaces.num; i++) {
3438 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3439 if (pws == NULL)
3440 return -1;
3441
3442 *pws = workspace_create();
3443 if (*pws == NULL)
3444 return -1;
3445 }
3446 activate_workspace(shell, 0);
3447
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003448 wl_list_init(&shell->workspaces.animation.link);
3449 shell->workspaces.animation.frame = animate_workspace_change_frame;
3450
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003451 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3452 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003453 return -1;
3454
Kristian Høgsberg75840622011-09-06 13:48:16 -04003455 if (wl_display_add_global(ec->wl_display,
3456 &desktop_shell_interface,
3457 shell, bind_desktop_shell) == NULL)
3458 return -1;
3459
Pekka Paalanen6e168112011-11-24 11:34:05 +02003460 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3461 shell, bind_screensaver) == NULL)
3462 return -1;
3463
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003464 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3465 shell, bind_input_panel) == NULL)
3466 return -1;
3467
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003468 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003469 if (launch_desktop_shell_process(shell) != 0)
3470 return -1;
3471
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003472 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003473 if (ec->seat->seat.pointer)
3474 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3475 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003476
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003477 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003478
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003479 return 0;
3480}