blob: cdd4e125fea73fa93a391a0caa2f2fef4b7f70cc [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 Ådahle3cddce2012-06-13 00:01:22 +020051struct workspace {
52 struct weston_layer layer;
53};
54
Tiago Vignattibe143262012-04-16 17:31:41 +030055struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050056 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040057
58 struct wl_listener lock_listener;
59 struct wl_listener unlock_listener;
60 struct wl_listener destroy_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020061
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050062 struct weston_layer fullscreen_layer;
63 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050064 struct weston_layer background_layer;
65 struct weston_layer lock_layer;
66
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040067 struct wl_listener pointer_focus_listener;
68 struct weston_surface *busy_surface;
69
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020070 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050071 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020072 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020073 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020074
75 unsigned deathcount;
76 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020077 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020078
79 bool locked;
80 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020081
Pekka Paalanen068ae942011-11-28 14:11:15 +020082 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050083 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010084
85 struct wl_list backgrounds;
86 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020087
Pekka Paalanen77346a62011-11-30 16:26:35 +020088 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +020089 struct wl_array array;
90 unsigned int current;
91 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +020092
93 struct weston_animation animation;
94 int anim_dir;
95 uint32_t anim_timestamp;
96 double anim_current;
97 struct workspace *anim_from;
98 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020099 } workspaces;
100
101 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200102 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200103 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200104 struct wl_resource *binding;
105 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500106 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200107 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500108
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300109 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800110 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500111 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400112};
113
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500114enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200115 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500116
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200117 SHELL_SURFACE_PANEL,
118 SHELL_SURFACE_BACKGROUND,
119 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200120 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500121
122 SHELL_SURFACE_TOPLEVEL,
123 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500124 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800125 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500126 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200127};
128
Scott Moreauff1db4a2012-04-17 19:06:18 -0600129struct ping_timer {
130 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600131 uint32_t serial;
132};
133
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200134struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200135 struct wl_resource resource;
136
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500137 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200138 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500139 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300140 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200141
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400142 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400143 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500144 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800145 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800146 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600147 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100148
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500149 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200150 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500151 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200152 } rotation;
153
154 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700155 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500156 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200157 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500158 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100159 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400160 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500161 } popup;
162
Alex Wu4539b082012-03-01 12:57:46 +0800163 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300164 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400165 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300166 } transient;
167
168 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800169 enum wl_shell_surface_fullscreen_method type;
170 struct weston_transform transform; /* matrix from x, y */
171 uint32_t framerate;
172 struct weston_surface *black_surface;
173 } fullscreen;
174
Scott Moreauff1db4a2012-04-17 19:06:18 -0600175 struct ping_timer *ping_timer;
176
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200177 struct weston_transform workspace_transform;
178
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500179 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500180 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100181 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400182
183 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200184};
185
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300186struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700187 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300188 struct shell_surface *shsurf;
189 struct wl_listener shsurf_destroy_listener;
190};
191
192struct weston_move_grab {
193 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100194 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500195};
196
Pekka Paalanen460099f2012-01-20 16:48:25 +0200197struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300198 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500199 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200200 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400201 GLfloat x;
202 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200203 } center;
204};
205
Alex Wubd3354b2012-04-17 17:20:49 +0800206static struct shell_surface *
207get_shell_surface(struct weston_surface *surface);
208
209static struct desktop_shell *
210shell_surface_get_shell(struct shell_surface *shsurf);
211
212static bool
213shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
214{
215 struct desktop_shell *shell;
216 struct weston_surface *top_fs_es;
217
218 shell = shell_surface_get_shell(shsurf);
219
220 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
221 return false;
222
223 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
224 struct weston_surface,
225 layer_link);
226 return (shsurf == get_shell_surface(top_fs_es));
227}
228
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500229static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400230destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300231{
232 struct shell_grab *grab;
233
234 grab = container_of(listener, struct shell_grab,
235 shsurf_destroy_listener);
236
237 grab->shsurf = NULL;
238}
239
240static void
241shell_grab_init(struct shell_grab *grab,
242 const struct wl_pointer_grab_interface *interface,
243 struct shell_surface *shsurf)
244{
245 grab->grab.interface = interface;
246 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400247 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
248 wl_signal_add(&shsurf->resource.destroy_signal,
249 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300250
251}
252
253static void
254shell_grab_finish(struct shell_grab *grab)
255{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400256 if (grab->shsurf)
257 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300258}
259
260static void
Alex Wu4539b082012-03-01 12:57:46 +0800261center_on_output(struct weston_surface *surface,
262 struct weston_output *output);
263
Daniel Stone496ca172012-05-30 16:31:42 +0100264static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300265get_modifier(char *modifier)
266{
267 if (!modifier)
268 return MODIFIER_SUPER;
269
270 if (!strcmp("ctrl", modifier))
271 return MODIFIER_CTRL;
272 else if (!strcmp("alt", modifier))
273 return MODIFIER_ALT;
274 else if (!strcmp("super", modifier))
275 return MODIFIER_SUPER;
276 else
277 return MODIFIER_SUPER;
278}
279
Juan Zhaoe10d2792012-04-25 19:09:52 +0800280static enum animation_type
281get_animation_type(char *animation)
282{
283 if (!animation)
284 return ANIMATION_NONE;
285
286 if (!strcmp("zoom", animation))
287 return ANIMATION_ZOOM;
288 else if (!strcmp("fade", animation))
289 return ANIMATION_FADE;
290 else
291 return ANIMATION_NONE;
292}
293
Alex Wu4539b082012-03-01 12:57:46 +0800294static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300295shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200296{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200297 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200298 char *path = NULL;
299 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200300 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300301 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800302 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200303
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400304 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300305 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800306 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200307 { "num-workspaces",
308 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200309 };
310
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400311 struct config_key saver_keys[] = {
312 { "path", CONFIG_KEY_STRING, &path },
313 { "duration", CONFIG_KEY_INTEGER, &duration },
314 };
315
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200316 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400317 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200318 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
319 };
320
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200321 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500322 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200323 free(config_file);
324
Pekka Paalanen7296e792011-12-07 16:22:00 +0200325 shell->screensaver.path = path;
326 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300327 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800328 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200329 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
330}
331
332static void
333workspace_destroy(struct workspace *ws)
334{
335 free(ws);
336}
337
338static struct workspace *
339workspace_create(void)
340{
341 struct workspace *ws = malloc(sizeof *ws);
342 if (ws == NULL)
343 return NULL;
344
345 weston_layer_init(&ws->layer, NULL);
346
347 return ws;
348}
349
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200350static int
351workspace_is_empty(struct workspace *ws)
352{
353 return wl_list_empty(&ws->layer.surface_list);
354}
355
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200356static struct workspace *
357get_workspace(struct desktop_shell *shell, unsigned int index)
358{
359 struct workspace **pws = shell->workspaces.array.data;
360 pws += index;
361 return *pws;
362}
363
364static struct workspace *
365get_current_workspace(struct desktop_shell *shell)
366{
367 return get_workspace(shell, shell->workspaces.current);
368}
369
370static void
371activate_workspace(struct desktop_shell *shell, unsigned int index)
372{
373 struct workspace *ws;
374
375 ws = get_workspace(shell, index);
376 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
377
378 shell->workspaces.current = index;
379}
380
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200381static unsigned int
382get_output_height(struct weston_output *output)
383{
384 return abs(output->region.extents.y1 - output->region.extents.y2);
385}
386
387static void
388surface_translate(struct weston_surface *surface, double d)
389{
390 struct shell_surface *shsurf = get_shell_surface(surface);
391 struct weston_transform *transform;
392
393 transform = &shsurf->workspace_transform;
394 if (wl_list_empty(&transform->link))
395 wl_list_insert(surface->geometry.transformation_list.prev,
396 &shsurf->workspace_transform.link);
397
398 weston_matrix_init(&shsurf->workspace_transform.matrix);
399 weston_matrix_translate(&shsurf->workspace_transform.matrix,
400 0.0, d, 0.0);
401 surface->geometry.dirty = 1;
402}
403
404static void
405workspace_translate_out(struct workspace *ws, double fraction)
406{
407 struct weston_surface *surface;
408 unsigned int height;
409 double d;
410
411 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
412 height = get_output_height(surface->output);
413 d = height * fraction;
414
415 surface_translate(surface, d);
416 }
417}
418
419static void
420workspace_translate_in(struct workspace *ws, double fraction)
421{
422 struct weston_surface *surface;
423 unsigned int height;
424 double d;
425
426 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
427 height = get_output_height(surface->output);
428
429 if (fraction > 0)
430 d = -(height - height * fraction);
431 else
432 d = height + height * fraction;
433
434 surface_translate(surface, d);
435 }
436}
437
438static void
439workspace_damage_all_surfaces(struct workspace *ws)
440{
441 struct weston_surface *surface;
442
443 wl_list_for_each(surface, &ws->layer.surface_list, layer_link)
444 weston_surface_damage(surface);
445}
446
447static void
448reverse_workspace_change_animation(struct desktop_shell *shell,
449 unsigned int index,
450 struct workspace *from,
451 struct workspace *to)
452{
453 shell->workspaces.current = index;
454
455 shell->workspaces.anim_to = to;
456 shell->workspaces.anim_from = from;
457 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
458 shell->workspaces.anim_timestamp = 0;
459
460 workspace_damage_all_surfaces(from);
461 workspace_damage_all_surfaces(to);
462}
463
464static void
465workspace_deactivate_transforms(struct workspace *ws)
466{
467 struct weston_surface *surface;
468 struct shell_surface *shsurf;
469
470 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
471 shsurf = get_shell_surface(surface);
472 wl_list_remove(&shsurf->workspace_transform.link);
473 wl_list_init(&shsurf->workspace_transform.link);
474 shsurf->surface->geometry.dirty = 1;
475 }
476}
477
478static void
479finish_workspace_change_animation(struct desktop_shell *shell,
480 struct workspace *from,
481 struct workspace *to)
482{
483 workspace_damage_all_surfaces(from);
484 workspace_damage_all_surfaces(to);
485
486 wl_list_remove(&shell->workspaces.animation.link);
487 workspace_deactivate_transforms(from);
488 workspace_deactivate_transforms(to);
489 shell->workspaces.anim_to = NULL;
490
491 wl_list_remove(&shell->workspaces.anim_from->layer.link);
492}
493
494static void
495animate_workspace_change_frame(struct weston_animation *animation,
496 struct weston_output *output, uint32_t msecs)
497{
498 struct desktop_shell *shell =
499 container_of(animation, struct desktop_shell,
500 workspaces.animation);
501 struct workspace *from = shell->workspaces.anim_from;
502 struct workspace *to = shell->workspaces.anim_to;
503 uint32_t t;
504 double x, y;
505
506 if (workspace_is_empty(from) && workspace_is_empty(to)) {
507 finish_workspace_change_animation(shell, from, to);
508 return;
509 }
510
511 if (shell->workspaces.anim_timestamp == 0) {
512 if (shell->workspaces.anim_current == 0.0)
513 shell->workspaces.anim_timestamp = msecs;
514 else
515 shell->workspaces.anim_timestamp =
516 msecs -
517 /* Invers of movement function 'y' below. */
518 (asin(1.0 - shell->workspaces.anim_current) *
519 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
520 M_2_PI);
521 }
522
523 t = msecs - shell->workspaces.anim_timestamp;
524
525 /*
526 * x = [0, π/2]
527 * y(x) = sin(x)
528 */
529 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
530 y = sin(x);
531
532 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
533 workspace_damage_all_surfaces(from);
534 workspace_damage_all_surfaces(to);
535
536 workspace_translate_out(from, shell->workspaces.anim_dir * y);
537 workspace_translate_in(to, shell->workspaces.anim_dir * y);
538 shell->workspaces.anim_current = y;
539
540 workspace_damage_all_surfaces(from);
541 workspace_damage_all_surfaces(to);
542 }
543 else {
544 finish_workspace_change_animation(shell, from, to);
545 }
546}
547
548static void
549animate_workspace_change(struct desktop_shell *shell,
550 unsigned int index,
551 struct workspace *from,
552 struct workspace *to)
553{
554 struct weston_output *output;
555
556 int dir;
557
558 if (index > shell->workspaces.current)
559 dir = -1;
560 else
561 dir = 1;
562
563 shell->workspaces.current = index;
564
565 shell->workspaces.anim_dir = dir;
566 shell->workspaces.anim_from = from;
567 shell->workspaces.anim_to = to;
568 shell->workspaces.anim_current = 0.0;
569 shell->workspaces.anim_timestamp = 0;
570
571 output = container_of(shell->compositor->output_list.next,
572 struct weston_output, link);
573 wl_list_insert(&output->animation_list,
574 &shell->workspaces.animation.link);
575
576 wl_list_insert(&from->layer.link, &to->layer.link);
577
578 workspace_translate_in(to, 0);
579
580 workspace_damage_all_surfaces(from);
581 workspace_damage_all_surfaces(to);
582}
583
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200584static void
585change_workspace(struct desktop_shell *shell, unsigned int index)
586{
587 struct workspace *from;
588 struct workspace *to;
589 struct weston_seat *seat;
590
591 if (index == shell->workspaces.current)
592 return;
593
594 /* Don't change workspace when there is any fullscreen surfaces. */
595 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
596 return;
597
598 /* Clear keyboard focus so that no hidden surfaces will keep it. */
599 wl_list_for_each(seat, &shell->compositor->seat_list, link)
600 if (seat->seat.keyboard)
601 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
602
603 from = get_current_workspace(shell);
604 to = get_workspace(shell, index);
605
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200606 if (shell->workspaces.anim_from == to &&
607 shell->workspaces.anim_to == from) {
608 reverse_workspace_change_animation(shell, index, from, to);
609 return;
610 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200611
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200612 if (shell->workspaces.anim_to != NULL)
613 finish_workspace_change_animation(shell,
614 shell->workspaces.anim_from,
615 shell->workspaces.anim_to);
616
617 if (workspace_is_empty(to) && workspace_is_empty(from)) {
618 shell->workspaces.current = index;
619 wl_list_insert(&from->layer.link, &to->layer.link);
620 wl_list_remove(&from->layer.link);
621 }
622 else
623 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200624}
625
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200626static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400627noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100628 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500629{
630 grab->focus = NULL;
631}
632
633static void
Scott Moreau447013d2012-02-18 05:05:29 -0700634move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100635 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500636{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500637 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100638 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300639 struct shell_surface *shsurf = move->base.shsurf;
640 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100641 int dx = wl_fixed_to_int(pointer->x + move->dx);
642 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300643
644 if (!shsurf)
645 return;
646
647 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500648
Daniel Stone103db7f2012-05-08 17:17:55 +0100649 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200650 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500651}
652
653static void
Scott Moreau447013d2012-02-18 05:05:29 -0700654move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100655 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500656{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300657 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
658 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100659 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100660 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500661
Daniel Stone4dbadb12012-05-30 16:31:51 +0100662 if (pointer->button_count == 0 &&
663 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300664 shell_grab_finish(shell_grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100665 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500666 free(grab);
667 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500668}
669
Scott Moreau447013d2012-02-18 05:05:29 -0700670static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500671 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500672 move_grab_motion,
673 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500674};
675
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600676static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400677busy_cursor_grab_focus(struct wl_pointer_grab *base,
678 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600679{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400680 struct shell_grab *grab = (struct shell_grab *) base;
681 struct wl_pointer *pointer = base->pointer;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600682
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400683 if (grab->grab.focus != surface) {
684 shell_grab_finish(grab);
685 wl_pointer_end_grab(pointer);
686 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600687 }
688}
689
690static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400691busy_cursor_grab_motion(struct wl_pointer_grab *grab,
692 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600693{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400694}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600695
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400696static void
697busy_cursor_grab_button(struct wl_pointer_grab *grab,
698 uint32_t time, uint32_t button, uint32_t state)
699{
700}
701
702static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
703 busy_cursor_grab_focus,
704 busy_cursor_grab_motion,
705 busy_cursor_grab_button,
706};
707
708static void
709set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
710{
711 struct shell_grab *grab;
712 struct desktop_shell *shell = shsurf->shell;
713 struct weston_seat *seat = (struct weston_seat *) pointer->seat;
714 struct weston_surface *sprite;
715
716 grab = malloc(sizeof *grab);
717 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600718 return;
719
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400720 shell_grab_init(grab, &busy_cursor_grab_interface, shsurf);
721 grab->grab.focus = &shsurf->surface->surface;
722 wl_pointer_start_grab(pointer, &grab->grab);
723 wl_pointer_set_focus(pointer, &shell->busy_surface->surface, 0, 0);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600724
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400725 sprite = (struct weston_surface *) seat->sprite;
726 shell->busy_surface->output = sprite->output;
727}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600728
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400729static void
730end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
731{
732 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
733
734 if (grab->grab.interface == &busy_cursor_grab_interface) {
735 shell_grab_finish(grab);
736 wl_pointer_end_grab(pointer);
737 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600738 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600739}
740
Scott Moreau9521d5e2012-04-19 13:06:17 -0600741static void
742ping_timer_destroy(struct shell_surface *shsurf)
743{
744 if (!shsurf || !shsurf->ping_timer)
745 return;
746
747 if (shsurf->ping_timer->source)
748 wl_event_source_remove(shsurf->ping_timer->source);
749
750 free(shsurf->ping_timer);
751 shsurf->ping_timer = NULL;
752}
753
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400754static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600755ping_timeout_handler(void *data)
756{
757 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400758 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600759
Scott Moreau9521d5e2012-04-19 13:06:17 -0600760 /* Client is not responding */
761 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400762
763 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
764 if (seat->seat.pointer->focus == &shsurf->surface->surface)
765 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600766
767 return 1;
768}
769
770static void
771ping_handler(struct weston_surface *surface, uint32_t serial)
772{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400773 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600774 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400775 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600776
777 if (!shsurf)
778 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400779 if (!shsurf->resource.client)
780 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600781
782 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300783 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600784 if (!shsurf->ping_timer)
785 return;
786
787 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600788 loop = wl_display_get_event_loop(surface->compositor->wl_display);
789 shsurf->ping_timer->source =
790 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
791 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
792
793 wl_shell_surface_send_ping(&shsurf->resource, serial);
794 }
795}
796
797static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400798handle_pointer_focus(struct wl_listener *listener, void *data)
799{
800 struct wl_pointer *pointer = data;
801 struct weston_surface *surface =
802 (struct weston_surface *) pointer->focus;
803 struct weston_compositor *compositor;
804 struct shell_surface *shsurf;
805 uint32_t serial;
806
807 if (!surface)
808 return;
809
810 compositor = surface->compositor;
811 shsurf = get_shell_surface(surface);
812
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +0300813 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400814 set_busy_cursor(shsurf, pointer);
815 } else {
816 serial = wl_display_next_serial(compositor->wl_display);
817 ping_handler(surface, serial);
818 }
819}
820
821static void
Scott Moreauff1db4a2012-04-17 19:06:18 -0600822shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
823 uint32_t serial)
824{
825 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400826 struct desktop_shell *shell = shsurf->shell;
827 struct weston_seat *seat;
828 struct weston_compositor *ec = shsurf->surface->compositor;
829 struct wl_pointer *pointer;
830 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600831
Scott Moreauff1db4a2012-04-17 19:06:18 -0600832 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400833 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600834 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400835 if (was_unresponsive) {
836 /* Received pong from previously unresponsive client */
837 wl_list_for_each(seat, &ec->seat_list, link) {
838 pointer = seat->seat.pointer;
839 if (pointer->focus ==
840 &shell->busy_surface->surface &&
841 pointer->current ==
842 &shsurf->surface->surface)
843 end_busy_cursor(shsurf, pointer);
844 }
845 }
Scott Moreau9521d5e2012-04-19 13:06:17 -0600846 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600847 }
848}
849
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400850static void
851shell_surface_set_title(struct wl_client *client,
852 struct wl_resource *resource, const char *title)
853{
854 struct shell_surface *shsurf = resource->data;
855
856 free(shsurf->title);
857 shsurf->title = strdup(title);
858}
859
860static void
861shell_surface_set_class(struct wl_client *client,
862 struct wl_resource *resource, const char *class)
863{
864 struct shell_surface *shsurf = resource->data;
865
866 free(shsurf->class);
867 shsurf->class = strdup(class);
868}
869
Scott Moreauff1db4a2012-04-17 19:06:18 -0600870static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400871surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500872{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500873 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300874
875 if (!shsurf)
876 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500877
878 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400879 if (!move)
880 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500881
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300882 shell_grab_init(&move->base, &move_grab_interface, shsurf);
883
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400884 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +0100885 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400886 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +0100887 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500888
Daniel Stone37816df2012-05-16 18:45:18 +0100889 wl_pointer_start_grab(ws->seat.pointer, &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500890
Daniel Stone37816df2012-05-16 18:45:18 +0100891 wl_pointer_set_focus(ws->seat.pointer, NULL,
892 wl_fixed_from_int(0),
893 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400894
895 return 0;
896}
897
898static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200899shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +0100900 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400901{
Daniel Stone37816df2012-05-16 18:45:18 +0100902 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200903 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400904
Daniel Stone37816df2012-05-16 18:45:18 +0100905 if (ws->seat.pointer->button_count == 0 ||
906 ws->seat.pointer->grab_serial != serial ||
907 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500908 return;
909
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -0400910 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400911 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500912}
913
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500914struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300915 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500916 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200917 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500918};
919
920static void
Scott Moreau447013d2012-02-18 05:05:29 -0700921resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100922 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500923{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500924 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100925 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400926 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500927 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +0100928 wl_fixed_t from_x, from_y;
929 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200930
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400931 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300932 return;
933
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400934 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +0100935 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +0100936 &from_x, &from_y);
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400937 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +0100938 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500939
Daniel Stone103db7f2012-05-08 17:17:55 +0100940 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200941 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100942 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200943 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100944 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500945 }
946
Daniel Stone103db7f2012-05-08 17:17:55 +0100947 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200948 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100949 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200950 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100951 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500952 }
953
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400954 shsurf->client->send_configure(shsurf->surface,
955 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500956}
957
958static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400959send_configure(struct weston_surface *surface,
960 uint32_t edges, int32_t width, int32_t height)
961{
962 struct shell_surface *shsurf = get_shell_surface(surface);
963
964 wl_shell_surface_send_configure(&shsurf->resource,
965 edges, width, height);
966}
967
968static const struct weston_shell_client shell_client = {
969 send_configure
970};
971
972static void
Scott Moreau447013d2012-02-18 05:05:29 -0700973resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100974 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500975{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300976 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100977 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100978 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500979
Daniel Stone4dbadb12012-05-30 16:31:51 +0100980 if (pointer->button_count == 0 &&
981 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300982 shell_grab_finish(&resize->base);
Daniel Stone37816df2012-05-16 18:45:18 +0100983 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500984 free(grab);
985 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500986}
987
Scott Moreau447013d2012-02-18 05:05:29 -0700988static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500989 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500990 resize_grab_motion,
991 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500992};
993
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400994static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400995surface_resize(struct shell_surface *shsurf,
996 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500997{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500998 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500999
Alex Wu4539b082012-03-01 12:57:46 +08001000 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1001 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001002
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001003 if (edges == 0 || edges > 15 ||
1004 (edges & 3) == 3 || (edges & 12) == 12)
1005 return 0;
1006
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001007 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001008 if (!resize)
1009 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001010
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001011 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
1012
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001013 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001014 resize->width = shsurf->surface->geometry.width;
1015 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001016
Daniel Stone37816df2012-05-16 18:45:18 +01001017 wl_pointer_start_grab(ws->seat.pointer, &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001018
Daniel Stone37816df2012-05-16 18:45:18 +01001019 wl_pointer_set_focus(ws->seat.pointer, NULL,
1020 wl_fixed_from_int(0),
1021 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001022
1023 return 0;
1024}
1025
1026static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001027shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001028 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001029 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001030{
Daniel Stone37816df2012-05-16 18:45:18 +01001031 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001032 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001033
Alex Wu4539b082012-03-01 12:57:46 +08001034 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1035 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001036
Daniel Stone37816df2012-05-16 18:45:18 +01001037 if (ws->seat.pointer->button_count == 0 ||
1038 ws->seat.pointer->grab_serial != serial ||
1039 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001040 return;
1041
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001042 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001043 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001044}
1045
Juan Zhao96879df2012-02-07 08:45:41 +08001046static struct weston_output *
1047get_default_output(struct weston_compositor *compositor)
1048{
1049 return container_of(compositor->output_list.next,
1050 struct weston_output, link);
1051}
1052
Alex Wu4539b082012-03-01 12:57:46 +08001053static void
1054shell_unset_fullscreen(struct shell_surface *shsurf)
1055{
1056 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001057 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1058 shell_surface_is_top_fullscreen(shsurf)) {
1059 weston_output_switch_mode(shsurf->fullscreen_output,
1060 shsurf->fullscreen_output->origin);
1061 }
Alex Wu4539b082012-03-01 12:57:46 +08001062 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1063 shsurf->fullscreen.framerate = 0;
1064 wl_list_remove(&shsurf->fullscreen.transform.link);
1065 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001066 if (shsurf->fullscreen.black_surface)
1067 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001068 shsurf->fullscreen.black_surface = NULL;
1069 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001070 weston_surface_set_position(shsurf->surface,
1071 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001072 if (shsurf->saved_rotation_valid) {
1073 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1074 &shsurf->rotation.transform.link);
1075 shsurf->saved_rotation_valid = false;
1076 }
Alex Wu4539b082012-03-01 12:57:46 +08001077}
1078
Pekka Paalanen98262232011-12-01 10:42:22 +02001079static int
1080reset_shell_surface_type(struct shell_surface *surface)
1081{
1082 switch (surface->type) {
1083 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001084 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001085 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001086 case SHELL_SURFACE_MAXIMIZED:
1087 surface->output = get_default_output(surface->surface->compositor);
1088 weston_surface_set_position(surface->surface,
1089 surface->saved_x,
1090 surface->saved_y);
1091 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001092 case SHELL_SURFACE_PANEL:
1093 case SHELL_SURFACE_BACKGROUND:
1094 wl_list_remove(&surface->link);
1095 wl_list_init(&surface->link);
1096 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001097 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +02001098 case SHELL_SURFACE_LOCK:
1099 wl_resource_post_error(&surface->resource,
1100 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +02001101 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +02001102 return -1;
1103 case SHELL_SURFACE_NONE:
1104 case SHELL_SURFACE_TOPLEVEL:
1105 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001106 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001107 break;
1108 }
1109
1110 surface->type = SHELL_SURFACE_NONE;
1111 return 0;
1112}
1113
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001114static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001115set_surface_type(struct shell_surface *shsurf)
1116{
1117 struct weston_surface *surface = shsurf->surface;
1118 struct shell_surface *pshsurf = shsurf->parent;
1119 struct weston_surface *pes;
1120 struct shell_surface *priv;
1121 struct desktop_shell *shell = shsurf->shell;
1122
1123 reset_shell_surface_type(shsurf);
1124
1125 shsurf->type = shsurf->next_type;
1126 shsurf->next_type = SHELL_SURFACE_NONE;
1127
1128 switch (shsurf->type) {
1129 case SHELL_SURFACE_TOPLEVEL:
1130 break;
1131 case SHELL_SURFACE_TRANSIENT:
1132 pes = pshsurf->surface;
1133 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001134 pes->geometry.x + shsurf->transient.x,
1135 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001136 break;
1137
1138 case SHELL_SURFACE_MAXIMIZED:
1139 shsurf->saved_x = surface->geometry.x;
1140 shsurf->saved_y = surface->geometry.y;
1141 shsurf->saved_position_valid = true;
1142 break;
1143
1144 case SHELL_SURFACE_FULLSCREEN:
1145 shsurf->saved_x = surface->geometry.x;
1146 shsurf->saved_y = surface->geometry.y;
1147 shsurf->saved_position_valid = true;
1148
1149 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1150 wl_list_remove(&shsurf->rotation.transform.link);
1151 wl_list_init(&shsurf->rotation.transform.link);
1152 shsurf->surface->geometry.dirty = 1;
1153 shsurf->saved_rotation_valid = true;
1154 }
1155 break;
1156
1157 case SHELL_SURFACE_BACKGROUND:
1158 wl_list_for_each(priv, &shell->backgrounds, link) {
1159 if (priv->output == shsurf->output) {
1160 priv->surface->output = NULL;
1161 wl_list_remove(&priv->surface->layer_link);
1162 wl_list_remove(&priv->link);
1163 break;
1164 }
1165 }
1166
1167 wl_list_insert(&shell->backgrounds, &shsurf->link);
1168
1169 weston_surface_set_position(surface, shsurf->output->x,
1170 shsurf->output->y);
1171 break;
1172
1173 case SHELL_SURFACE_PANEL:
1174 wl_list_for_each(priv, &shell->panels, link) {
1175 if (priv->output == shsurf->output) {
1176 priv->surface->output = NULL;
1177 wl_list_remove(&priv->surface->layer_link);
1178 wl_list_remove(&priv->link);
1179 break;
1180 }
1181 }
1182
1183 wl_list_insert(&shell->panels, &shsurf->link);
1184
1185 weston_surface_set_position(surface, shsurf->output->x,
1186 shsurf->output->y);
1187 break;
1188
1189 default:
1190 break;
1191 }
1192}
1193
1194static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001195set_toplevel(struct shell_surface *shsurf)
1196{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001197 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001198}
1199
1200static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001201shell_surface_set_toplevel(struct wl_client *client,
1202 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001203{
Pekka Paalanen98262232011-12-01 10:42:22 +02001204 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001205
Tiago Vignattibc052c92012-04-19 16:18:18 +03001206 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001207}
1208
1209static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001210set_transient(struct shell_surface *shsurf,
1211 struct shell_surface *pshsurf, int x, int y, uint32_t flags)
1212{
1213 /* assign to parents output */
1214 shsurf->parent = pshsurf;
1215 shsurf->transient.x = x;
1216 shsurf->transient.y = y;
1217 shsurf->transient.flags = flags;
1218 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1219}
1220
1221static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001222shell_surface_set_transient(struct wl_client *client,
1223 struct wl_resource *resource,
1224 struct wl_resource *parent_resource,
1225 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001226{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001227 struct shell_surface *shsurf = resource->data;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001228 struct shell_surface *pshsurf = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001229
Tiago Vignatti491bac12012-05-18 16:37:43 -04001230 set_transient(shsurf, pshsurf, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001231}
1232
Tiago Vignattibe143262012-04-16 17:31:41 +03001233static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001234shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001235{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001236 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001237}
1238
1239static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001240get_output_panel_height(struct desktop_shell *shell,
1241 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001242{
1243 struct shell_surface *priv;
1244 int panel_height = 0;
1245
1246 if (!output)
1247 return 0;
1248
Tiago Vignattibe143262012-04-16 17:31:41 +03001249 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +08001250 if (priv->output == output) {
1251 panel_height = priv->surface->geometry.height;
1252 break;
1253 }
1254 }
1255 return panel_height;
1256}
1257
1258static void
1259shell_surface_set_maximized(struct wl_client *client,
1260 struct wl_resource *resource,
1261 struct wl_resource *output_resource )
1262{
1263 struct shell_surface *shsurf = resource->data;
1264 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001265 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001266 uint32_t edges = 0, panel_height = 0;
1267
1268 /* get the default output, if the client set it as NULL
1269 check whether the ouput is available */
1270 if (output_resource)
1271 shsurf->output = output_resource->data;
1272 else
1273 shsurf->output = get_default_output(es->compositor);
1274
Tiago Vignattibe143262012-04-16 17:31:41 +03001275 shell = shell_surface_get_shell(shsurf);
1276 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001277 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001278
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001279 shsurf->client->send_configure(shsurf->surface, edges,
1280 es->output->current->width,
1281 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001282
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001283 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001284}
1285
Alex Wu21858432012-04-01 20:13:08 +08001286static void
1287black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1288
Alex Wu4539b082012-03-01 12:57:46 +08001289static struct weston_surface *
1290create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001291 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001292 GLfloat x, GLfloat y, int w, int h)
1293{
1294 struct weston_surface *surface = NULL;
1295
1296 surface = weston_surface_create(ec);
1297 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001298 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001299 return NULL;
1300 }
1301
Alex Wu21858432012-04-01 20:13:08 +08001302 surface->configure = black_surface_configure;
1303 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001304 weston_surface_configure(surface, x, y, w, h);
1305 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1306 return surface;
1307}
1308
1309/* Create black surface and append it to the associated fullscreen surface.
1310 * Handle size dismatch and positioning according to the method. */
1311static void
1312shell_configure_fullscreen(struct shell_surface *shsurf)
1313{
1314 struct weston_output *output = shsurf->fullscreen_output;
1315 struct weston_surface *surface = shsurf->surface;
1316 struct weston_matrix *matrix;
1317 float scale;
1318
1319 center_on_output(surface, output);
1320
1321 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001322 shsurf->fullscreen.black_surface =
1323 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001324 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001325 output->x, output->y,
1326 output->current->width,
1327 output->current->height);
1328
1329 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1330 wl_list_insert(&surface->layer_link,
1331 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001332 shsurf->fullscreen.black_surface->output = output;
1333
1334 switch (shsurf->fullscreen.type) {
1335 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1336 break;
1337 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1338 matrix = &shsurf->fullscreen.transform.matrix;
1339 weston_matrix_init(matrix);
1340 scale = (float)output->current->width/(float)surface->geometry.width;
1341 weston_matrix_scale(matrix, scale, scale, 1);
1342 wl_list_remove(&shsurf->fullscreen.transform.link);
1343 wl_list_insert(surface->geometry.transformation_list.prev,
1344 &shsurf->fullscreen.transform.link);
1345 weston_surface_set_position(surface, output->x, output->y);
1346 break;
1347 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001348 if (shell_surface_is_top_fullscreen(shsurf)) {
1349 struct weston_mode mode = {0,
1350 surface->geometry.width,
1351 surface->geometry.height,
1352 shsurf->fullscreen.framerate};
1353
1354 if (weston_output_switch_mode(output, &mode) == 0) {
1355 weston_surface_configure(shsurf->fullscreen.black_surface,
1356 output->x, output->y,
1357 output->current->width,
1358 output->current->height);
1359 weston_surface_set_position(surface, output->x, output->y);
1360 break;
1361 }
1362 }
Alex Wu4539b082012-03-01 12:57:46 +08001363 break;
1364 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1365 break;
1366 default:
1367 break;
1368 }
1369}
1370
1371/* make the fullscreen and black surface at the top */
1372static void
1373shell_stack_fullscreen(struct shell_surface *shsurf)
1374{
Alex Wubd3354b2012-04-17 17:20:49 +08001375 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001376 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001377 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001378
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001379 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001380 wl_list_insert(&shell->fullscreen_layer.surface_list,
1381 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001382 weston_surface_damage(surface);
1383
1384 if (!shsurf->fullscreen.black_surface)
1385 shsurf->fullscreen.black_surface =
1386 create_black_surface(surface->compositor,
1387 surface,
1388 output->x, output->y,
1389 output->current->width,
1390 output->current->height);
1391
1392 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001393 wl_list_insert(&surface->layer_link,
1394 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001395 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001396}
1397
1398static void
1399shell_map_fullscreen(struct shell_surface *shsurf)
1400{
Alex Wu4539b082012-03-01 12:57:46 +08001401 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001402 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001403}
1404
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001405static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001406shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001407 struct wl_resource *resource,
1408 uint32_t method,
1409 uint32_t framerate,
1410 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001411{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001412 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001413 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001414
1415 if (output_resource)
1416 shsurf->output = output_resource->data;
1417 else
1418 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001419
Alex Wu4539b082012-03-01 12:57:46 +08001420 shsurf->fullscreen_output = shsurf->output;
1421 shsurf->fullscreen.type = method;
1422 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001423 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001424
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001425 shsurf->client->send_configure(shsurf->surface, 0,
1426 shsurf->output->current->width,
1427 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001428}
1429
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001430static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001431popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001432 struct wl_surface *surface,
1433 wl_fixed_t x,
1434 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001435{
Daniel Stone37816df2012-05-16 18:45:18 +01001436 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001437 struct shell_surface *priv =
1438 container_of(grab, struct shell_surface, popup.grab);
1439 struct wl_client *client = priv->surface->surface.resource.client;
1440
Pekka Paalanencb108432012-01-19 16:25:40 +02001441 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001442 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001443 grab->focus = surface;
1444 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001445 wl_pointer_set_focus(pointer, NULL,
1446 wl_fixed_from_int(0),
1447 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001448 grab->focus = NULL;
1449 }
1450}
1451
1452static void
Scott Moreau447013d2012-02-18 05:05:29 -07001453popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001454 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001455{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001456 struct wl_resource *resource;
1457
Daniel Stone37816df2012-05-16 18:45:18 +01001458 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001459 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001460 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001461}
1462
1463static void
Scott Moreau447013d2012-02-18 05:05:29 -07001464popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001465 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001466{
1467 struct wl_resource *resource;
1468 struct shell_surface *shsurf =
1469 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001470 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001471 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001472 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001473
Daniel Stone37816df2012-05-16 18:45:18 +01001474 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001475 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001476 display = wl_client_get_display(resource->client);
1477 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001478 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001479 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001480 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001481 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001482 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001483 wl_pointer_end_grab(grab->pointer);
1484 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001485 }
1486
Daniel Stone4dbadb12012-05-30 16:31:51 +01001487 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001488 shsurf->popup.initial_up = 1;
1489}
1490
Scott Moreau447013d2012-02-18 05:05:29 -07001491static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001492 popup_grab_focus,
1493 popup_grab_motion,
1494 popup_grab_button,
1495};
1496
1497static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001498shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001499{
Daniel Stone37816df2012-05-16 18:45:18 +01001500 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001501 struct weston_surface *es = shsurf->surface;
1502 struct weston_surface *parent = shsurf->parent->surface;
1503
1504 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001505 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001506
Pekka Paalanen938269a2012-02-07 14:19:01 +02001507 weston_surface_update_transform(parent);
1508 if (parent->transform.enabled) {
1509 shsurf->popup.parent_transform.matrix =
1510 parent->transform.matrix;
1511 } else {
1512 /* construct x, y translation matrix */
1513 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1514 shsurf->popup.parent_transform.matrix.d[12] =
1515 parent->geometry.x;
1516 shsurf->popup.parent_transform.matrix.d[13] =
1517 parent->geometry.y;
1518 }
1519 wl_list_insert(es->geometry.transformation_list.prev,
1520 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001521 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001522
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001523 shsurf->popup.initial_up = 0;
1524
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001525 /* We don't require the grab to still be active, but if another
1526 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001527 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1528 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001529 } else {
1530 wl_shell_surface_send_popup_done(&shsurf->resource);
1531 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001532}
1533
1534static void
1535shell_surface_set_popup(struct wl_client *client,
1536 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001537 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001538 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001539 struct wl_resource *parent_resource,
1540 int32_t x, int32_t y, uint32_t flags)
1541{
1542 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001543
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001544 shsurf->type = SHELL_SURFACE_POPUP;
1545 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001546 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001547 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001548 shsurf->popup.x = x;
1549 shsurf->popup.y = y;
1550}
1551
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001552static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001553 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001554 shell_surface_move,
1555 shell_surface_resize,
1556 shell_surface_set_toplevel,
1557 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001558 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001559 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001560 shell_surface_set_maximized,
1561 shell_surface_set_title,
1562 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001563};
1564
1565static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001566destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001567{
Daniel Stone37816df2012-05-16 18:45:18 +01001568 if (shsurf->popup.grab.pointer)
1569 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001570
Alex Wubd3354b2012-04-17 17:20:49 +08001571 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1572 shell_surface_is_top_fullscreen(shsurf)) {
1573 weston_output_switch_mode(shsurf->fullscreen_output,
1574 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001575 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001576
Alex Wuaa08e2d2012-03-05 11:01:40 +08001577 if (shsurf->fullscreen.black_surface)
1578 weston_surface_destroy(shsurf->fullscreen.black_surface);
1579
Alex Wubd3354b2012-04-17 17:20:49 +08001580 /* As destroy_resource() use wl_list_for_each_safe(),
1581 * we can always remove the listener.
1582 */
1583 wl_list_remove(&shsurf->surface_destroy_listener.link);
1584 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001585 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001586
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001587 wl_list_remove(&shsurf->link);
1588 free(shsurf);
1589}
1590
1591static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001592shell_destroy_shell_surface(struct wl_resource *resource)
1593{
1594 struct shell_surface *shsurf = resource->data;
1595
1596 destroy_shell_surface(shsurf);
1597}
1598
1599static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001600shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001601{
1602 struct shell_surface *shsurf = container_of(listener,
1603 struct shell_surface,
1604 surface_destroy_listener);
1605
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001606 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001607 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001608 } else {
1609 wl_signal_emit(&shsurf->resource.destroy_signal,
1610 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001611 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001612 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001613}
1614
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001615static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001616get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001617{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001618 struct wl_listener *listener;
1619
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001620 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1621 shell_handle_surface_destroy);
1622 if (listener)
1623 return container_of(listener, struct shell_surface,
1624 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001625
1626 return NULL;
1627}
1628
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001629static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001630shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1631
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001632static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001633create_shell_surface(void *shell, struct weston_surface *surface,
1634 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001635{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001636 struct shell_surface *shsurf;
1637
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001638 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001639 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001640 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001641 }
1642
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001643 shsurf = calloc(1, sizeof *shsurf);
1644 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001645 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001646 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001647 }
1648
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001649 surface->configure = shell_surface_configure;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001650 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001651
Tiago Vignattibc052c92012-04-19 16:18:18 +03001652 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001653 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001654 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001655 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001656 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001657 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1658 shsurf->fullscreen.framerate = 0;
1659 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001660 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001661 wl_list_init(&shsurf->fullscreen.transform.link);
1662
Tiago Vignattibc052c92012-04-19 16:18:18 +03001663 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001664 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1665 wl_signal_add(&surface->surface.resource.destroy_signal,
1666 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001667
1668 /* init link so its safe to always remove it in destroy_shell_surface */
1669 wl_list_init(&shsurf->link);
1670
Pekka Paalanen460099f2012-01-20 16:48:25 +02001671 /* empty when not in use */
1672 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001673 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001674
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001675 wl_list_init(&shsurf->workspace_transform.link);
1676
Pekka Paalanen98262232011-12-01 10:42:22 +02001677 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001678 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001679
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001680 shsurf->client = client;
1681
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001682 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001683}
1684
1685static void
1686shell_get_shell_surface(struct wl_client *client,
1687 struct wl_resource *resource,
1688 uint32_t id,
1689 struct wl_resource *surface_resource)
1690{
1691 struct weston_surface *surface = surface_resource->data;
1692 struct desktop_shell *shell = resource->data;
1693 struct shell_surface *shsurf;
1694
1695 if (get_shell_surface(surface)) {
1696 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001697 WL_DISPLAY_ERROR_INVALID_OBJECT,
1698 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001699 return;
1700 }
1701
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001702 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001703 if (!shsurf) {
1704 wl_resource_post_error(surface_resource,
1705 WL_DISPLAY_ERROR_INVALID_OBJECT,
1706 "surface->configure already set");
1707 return;
1708 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001709
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001710 shsurf->resource.destroy = shell_destroy_shell_surface;
1711 shsurf->resource.object.id = id;
1712 shsurf->resource.object.interface = &wl_shell_surface_interface;
1713 shsurf->resource.object.implementation =
1714 (void (**)(void)) &shell_surface_implementation;
1715 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001716
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001717 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001718}
1719
1720static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001721 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001722};
1723
Kristian Høgsberg07937562011-04-12 17:25:42 -04001724static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001725handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001726{
1727 proc->pid = 0;
1728}
1729
1730static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001731launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001732{
1733 if (shell->screensaver.binding)
1734 return;
1735
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001736 if (!shell->screensaver.path)
1737 return;
1738
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001739 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001740 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001741 return;
1742 }
1743
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001744 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001745 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001746 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001747 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001748}
1749
1750static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001751terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001752{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001753 if (shell->screensaver.process.pid == 0)
1754 return;
1755
1756 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001757}
1758
1759static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001760show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001761{
1762 struct wl_list *list;
1763
1764 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001765 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001766 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001767 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001768
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001769 wl_list_remove(&surface->surface->layer_link);
1770 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001771 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001772 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001773}
1774
1775static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001776hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001777{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001778 wl_list_remove(&surface->surface->layer_link);
1779 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001780 surface->surface->output = NULL;
1781}
1782
1783static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001784desktop_shell_set_background(struct wl_client *client,
1785 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001786 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001787 struct wl_resource *surface_resource)
1788{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001789 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001790
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001791 shsurf->next_type = SHELL_SURFACE_BACKGROUND;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001792 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001793
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001794 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001795 surface_resource,
1796 shsurf->output->current->width,
1797 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001798}
1799
1800static void
1801desktop_shell_set_panel(struct wl_client *client,
1802 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001803 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001804 struct wl_resource *surface_resource)
1805{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001806 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001807
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001808 shsurf->next_type = SHELL_SURFACE_PANEL;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001809 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001810
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001811 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001812 surface_resource,
1813 shsurf->output->current->width,
1814 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001815}
1816
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001817static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001818handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001819{
Tiago Vignattibe143262012-04-16 17:31:41 +03001820 struct desktop_shell *shell =
1821 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001822
Martin Minarik6d118362012-06-07 18:01:59 +02001823 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001824 shell->lock_surface = NULL;
1825}
1826
1827static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001828desktop_shell_set_lock_surface(struct wl_client *client,
1829 struct wl_resource *resource,
1830 struct wl_resource *surface_resource)
1831{
Tiago Vignattibe143262012-04-16 17:31:41 +03001832 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001833 struct shell_surface *surface = surface_resource->data;
1834
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001835 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001836
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001837 if (!shell->locked)
1838 return;
1839
Pekka Paalanen98262232011-12-01 10:42:22 +02001840 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001841
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001842 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1843 wl_signal_add(&surface_resource->destroy_signal,
1844 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001845
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001846 shell->lock_surface->next_type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001847}
1848
1849static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001850resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001851{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001852 struct shell_surface *tmp;
1853
1854 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1855 hide_screensaver(shell, tmp);
1856
1857 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001858
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001859 wl_list_remove(&shell->lock_layer.link);
1860 wl_list_insert(&shell->compositor->cursor_layer.link,
1861 &shell->fullscreen_layer.link);
1862 wl_list_insert(&shell->fullscreen_layer.link,
1863 &shell->panel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001864
1865 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001866 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001867 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001868 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001869}
1870
1871static void
1872desktop_shell_unlock(struct wl_client *client,
1873 struct wl_resource *resource)
1874{
Tiago Vignattibe143262012-04-16 17:31:41 +03001875 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001876
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001877 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001878
1879 if (shell->locked)
1880 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001881}
1882
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001883static void
1884desktop_shell_set_busy_surface(struct wl_client *client,
1885 struct wl_resource *resource,
1886 struct wl_resource *surface_resource)
1887{
1888 struct desktop_shell *shell = resource->data;
1889
1890 shell->busy_surface = surface_resource->data;
1891}
1892
Kristian Høgsberg75840622011-09-06 13:48:16 -04001893static const struct desktop_shell_interface desktop_shell_implementation = {
1894 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001895 desktop_shell_set_panel,
1896 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001897 desktop_shell_unlock,
1898 desktop_shell_set_busy_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04001899};
1900
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001901static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001902get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001903{
1904 struct shell_surface *shsurf;
1905
1906 shsurf = get_shell_surface(surface);
1907 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001908 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001909 return shsurf->type;
1910}
1911
Kristian Høgsberg75840622011-09-06 13:48:16 -04001912static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001913move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001914{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001915 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001916 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001917 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001918
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001919 if (surface == NULL)
1920 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001921
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001922 shsurf = get_shell_surface(surface);
1923 if (shsurf == NULL)
1924 return;
1925
1926 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001927 case SHELL_SURFACE_PANEL:
1928 case SHELL_SURFACE_BACKGROUND:
1929 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001930 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001931 return;
1932 default:
1933 break;
1934 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001935
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001936 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001937}
1938
1939static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001940resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001941{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001942 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001943 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001944 uint32_t edges = 0;
1945 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001946 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001947
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001948 if (surface == NULL)
1949 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001950
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001951 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001952 if (!shsurf)
1953 return;
1954
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001955 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001956 case SHELL_SURFACE_PANEL:
1957 case SHELL_SURFACE_BACKGROUND:
1958 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001959 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001960 return;
1961 default:
1962 break;
1963 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001964
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001965 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001966 wl_fixed_to_int(seat->pointer->grab_x),
1967 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01001968 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001969
Pekka Paalanen60921e52012-01-25 15:55:43 +02001970 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001971 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001972 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001973 edges |= 0;
1974 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001975 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001976
Pekka Paalanen60921e52012-01-25 15:55:43 +02001977 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001978 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001979 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001980 edges |= 0;
1981 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001982 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001983
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001984 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001985}
1986
1987static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001988surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001989 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001990{
Scott Moreau02709af2012-05-22 01:54:10 -06001991 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001992 struct shell_surface *shsurf;
1993 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01001994 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001995
1996 if (surface == NULL)
1997 return;
1998
1999 shsurf = get_shell_surface(surface);
2000 if (!shsurf)
2001 return;
2002
2003 switch (shsurf->type) {
2004 case SHELL_SURFACE_BACKGROUND:
2005 case SHELL_SURFACE_SCREENSAVER:
2006 return;
2007 default:
2008 break;
2009 }
2010
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002011 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002012
Scott Moreau02709af2012-05-22 01:54:10 -06002013 if (surface->alpha > 1.0)
2014 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002015 if (surface->alpha < step)
2016 surface->alpha = step;
2017
2018 surface->geometry.dirty = 1;
2019 weston_surface_damage(surface);
2020}
2021
2022static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002023do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002024 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002025{
Daniel Stone37816df2012-05-16 18:45:18 +01002026 struct weston_seat *ws = (struct weston_seat *) seat;
2027 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002028 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002029 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002030
2031 wl_list_for_each(output, &compositor->output_list, link) {
2032 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002033 wl_fixed_to_double(seat->pointer->x),
2034 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002035 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002036 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002037 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002038 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002039 increment = -output->zoom.increment;
2040 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002041 increment = output->zoom.increment *
2042 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002043 else
2044 increment = 0;
2045
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002046 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002047
Scott Moreaue6603982012-06-11 13:07:51 -06002048 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002049 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002050 else if (output->zoom.level > output->zoom.max_level)
2051 output->zoom.level = output->zoom.max_level;
2052 else
2053 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002054
Scott Moreaue6603982012-06-11 13:07:51 -06002055 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002056
Daniel Stone37816df2012-05-16 18:45:18 +01002057 weston_output_update_zoom(output,
2058 seat->pointer->x,
Scott Moreau7a1b32a2012-05-27 14:25:02 -06002059 seat->pointer->y,
Scott Moreaue6603982012-06-11 13:07:51 -06002060 ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002061 }
2062 }
2063}
2064
Scott Moreauccbf29d2012-02-22 14:21:41 -07002065static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002066zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002067 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002068{
2069 do_zoom(seat, time, 0, axis, value);
2070}
2071
2072static void
2073zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2074 void *data)
2075{
2076 do_zoom(seat, time, key, 0, 0);
2077}
2078
2079static void
2080terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2081 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002082{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002083 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002084
Daniel Stone325fc2d2012-05-30 16:31:58 +01002085 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002086}
2087
2088static void
Scott Moreau447013d2012-02-18 05:05:29 -07002089rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002090 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002091{
2092 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002093 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002094 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002095 struct shell_surface *shsurf = rotate->base.shsurf;
2096 struct weston_surface *surface;
2097 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2098
2099 if (!shsurf)
2100 return;
2101
2102 surface = shsurf->surface;
2103
2104 cx = 0.5f * surface->geometry.width;
2105 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002106
Daniel Stone37816df2012-05-16 18:45:18 +01002107 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2108 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002109 r = sqrtf(dx * dx + dy * dy);
2110
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002111 wl_list_remove(&shsurf->rotation.transform.link);
2112 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002113
2114 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002115 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002116 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002117
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002118 weston_matrix_init(&rotate->rotation);
2119 rotate->rotation.d[0] = dx / r;
2120 rotate->rotation.d[4] = -dy / r;
2121 rotate->rotation.d[1] = -rotate->rotation.d[4];
2122 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002123
2124 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002125 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002126 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002127 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002128 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002129
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002130 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002131 &shsurf->surface->geometry.transformation_list,
2132 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002133 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002134 wl_list_init(&shsurf->rotation.transform.link);
2135 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002136 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002137 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002138
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002139 /* We need to adjust the position of the surface
2140 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002141 cposx = surface->geometry.x + cx;
2142 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002143 dposx = rotate->center.x - cposx;
2144 dposy = rotate->center.y - cposy;
2145 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002146 weston_surface_set_position(surface,
2147 surface->geometry.x + dposx,
2148 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002149 }
2150
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002151 /* Repaint implies weston_surface_update_transform(), which
2152 * lazily applies the damage due to rotation update.
2153 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002154 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002155}
2156
2157static void
Scott Moreau447013d2012-02-18 05:05:29 -07002158rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002159 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002160{
2161 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002162 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002163 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002164 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002165 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002166
Daniel Stone4dbadb12012-05-30 16:31:51 +01002167 if (pointer->button_count == 0 &&
2168 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002169 if (shsurf)
2170 weston_matrix_multiply(&shsurf->rotation.rotation,
2171 &rotate->rotation);
2172 shell_grab_finish(&rotate->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002173 wl_pointer_end_grab(pointer);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002174 free(rotate);
2175 }
2176}
2177
Scott Moreau447013d2012-02-18 05:05:29 -07002178static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002179 noop_grab_focus,
2180 rotate_grab_motion,
2181 rotate_grab_button,
2182};
2183
2184static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002185rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002186 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002187{
2188 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002189 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002190 struct shell_surface *surface;
2191 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002192 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002193 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002194
2195 if (base_surface == NULL)
2196 return;
2197
2198 surface = get_shell_surface(base_surface);
2199 if (!surface)
2200 return;
2201
2202 switch (surface->type) {
2203 case SHELL_SURFACE_PANEL:
2204 case SHELL_SURFACE_BACKGROUND:
2205 case SHELL_SURFACE_FULLSCREEN:
2206 case SHELL_SURFACE_SCREENSAVER:
2207 return;
2208 default:
2209 break;
2210 }
2211
Pekka Paalanen460099f2012-01-20 16:48:25 +02002212 rotate = malloc(sizeof *rotate);
2213 if (!rotate)
2214 return;
2215
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002216 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02002217
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002218 weston_surface_to_global_float(surface->surface,
2219 surface->surface->geometry.width / 2,
2220 surface->surface->geometry.height / 2,
2221 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002222
Daniel Stone37816df2012-05-16 18:45:18 +01002223 wl_pointer_start_grab(seat->pointer, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002224
Daniel Stone37816df2012-05-16 18:45:18 +01002225 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2226 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002227 r = sqrtf(dx * dx + dy * dy);
2228 if (r > 20.0f) {
2229 struct weston_matrix inverse;
2230
2231 weston_matrix_init(&inverse);
2232 inverse.d[0] = dx / r;
2233 inverse.d[4] = dy / r;
2234 inverse.d[1] = -inverse.d[4];
2235 inverse.d[5] = inverse.d[0];
2236 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002237
2238 weston_matrix_init(&rotate->rotation);
2239 rotate->rotation.d[0] = dx / r;
2240 rotate->rotation.d[4] = -dy / r;
2241 rotate->rotation.d[1] = -rotate->rotation.d[4];
2242 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002243 } else {
2244 weston_matrix_init(&surface->rotation.rotation);
2245 weston_matrix_init(&rotate->rotation);
2246 }
2247
Daniel Stone37816df2012-05-16 18:45:18 +01002248 wl_pointer_set_focus(seat->pointer, NULL,
2249 wl_fixed_from_int(0),
2250 wl_fixed_from_int(0));
Pekka Paalanen460099f2012-01-20 16:48:25 +02002251}
2252
2253static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002254activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002255 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002256{
Alex Wu21858432012-04-01 20:13:08 +08002257 struct weston_surface *surf, *prev;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002258 struct workspace *ws;
2259 struct weston_layer *ws_layer;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002260
Daniel Stone37816df2012-05-16 18:45:18 +01002261 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002262
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002263 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002264 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002265 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002266 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002267 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002268
Pekka Paalanen77346a62011-11-30 16:26:35 +02002269 case SHELL_SURFACE_SCREENSAVER:
2270 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002271 if (shell->lock_surface)
2272 weston_surface_restack(es,
2273 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002274 break;
Alex Wu4539b082012-03-01 12:57:46 +08002275 case SHELL_SURFACE_FULLSCREEN:
2276 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002277 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002278 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002279 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002280 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002281 ws = get_current_workspace(shell);
2282 ws_layer = &ws->layer;
2283
Alex Wubd3354b2012-04-17 17:20:49 +08002284 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08002285 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
2286 wl_list_for_each_reverse_safe(surf,
2287 prev,
2288 &shell->fullscreen_layer.surface_list,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002289 layer_link) {
Alex Wu21858432012-04-01 20:13:08 +08002290 weston_surface_restack(surf,
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002291 &ws_layer->surface_list);
2292 }
Alex Wu21858432012-04-01 20:13:08 +08002293 }
2294
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002295 weston_surface_restack(es, &ws_layer->surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002296 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002297 }
2298}
2299
Alex Wu21858432012-04-01 20:13:08 +08002300/* no-op func for checking black surface */
2301static void
2302black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2303{
2304}
2305
2306static bool
2307is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2308{
2309 if (es->configure == black_surface_configure) {
2310 if (fs_surface)
2311 *fs_surface = (struct weston_surface *)es->private;
2312 return true;
2313 }
2314 return false;
2315}
2316
Kristian Høgsberg75840622011-09-06 13:48:16 -04002317static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002318click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002319 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002320{
Daniel Stone37816df2012-05-16 18:45:18 +01002321 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002322 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002323 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002324 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002325
Daniel Stone37816df2012-05-16 18:45:18 +01002326 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002327 if (!focus)
2328 return;
2329
Alex Wu21858432012-04-01 20:13:08 +08002330 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002331 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002332
Daniel Stone325fc2d2012-05-30 16:31:58 +01002333 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002334 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002335}
2336
2337static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002338lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002339{
Tiago Vignattibe143262012-04-16 17:31:41 +03002340 struct desktop_shell *shell =
2341 container_of(listener, struct desktop_shell, lock_listener);
Daniel Stone37816df2012-05-16 18:45:18 +01002342 struct weston_seat *seat;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002343 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002344 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002345
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002346 if (shell->locked) {
2347 wl_list_for_each(output, &shell->compositor->output_list, link)
2348 /* TODO: find a way to jump to other DPMS levels */
2349 if (output->set_dpms)
2350 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002351 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002352 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002353
2354 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002355
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002356 /* Hide all surfaces by removing the fullscreen, panel and
2357 * toplevel layers. This way nothing else can show or receive
2358 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002359
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002360 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002361 wl_list_remove(&shell->fullscreen_layer.link);
2362 wl_list_insert(&shell->compositor->cursor_layer.link,
2363 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002364
Pekka Paalanen77346a62011-11-30 16:26:35 +02002365 launch_screensaver(shell);
2366
2367 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
2368 show_screensaver(shell, shsurf);
2369
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002370 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02002371 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002372 weston_compositor_wake(shell->compositor);
2373 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002374 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002375
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002376 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002377 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002378
2379 /* reset keyboard foci */
Daniel Stone37816df2012-05-16 18:45:18 +01002380 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2381 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002382 }
2383
2384 /* TODO: disable bindings that should not work while locked. */
2385
2386 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002387}
2388
2389static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002390unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002391{
Tiago Vignattibe143262012-04-16 17:31:41 +03002392 struct desktop_shell *shell =
2393 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002394
Pekka Paalanend81c2162011-11-16 13:47:34 +02002395 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002396 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002397 return;
2398 }
2399
2400 /* If desktop-shell client has gone away, unlock immediately. */
2401 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002402 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002403 return;
2404 }
2405
2406 if (shell->prepare_event_sent)
2407 return;
2408
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002409 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002410 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002411}
2412
2413static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002414center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002415{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002416 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002417 GLfloat x = (mode->width - surface->geometry.width) / 2;
2418 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002419
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002420 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002421}
2422
2423static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002424map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002425 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002426{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002427 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002428 struct shell_surface *shsurf = get_shell_surface(surface);
2429 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002430 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002431 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002432 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002433 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002434
Pekka Paalanen60921e52012-01-25 15:55:43 +02002435 surface->geometry.width = width;
2436 surface->geometry.height = height;
2437 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002438
2439 /* initial positioning, see also configure() */
2440 switch (surface_type) {
2441 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002442 weston_surface_set_position(surface, 10 + random() % 400,
2443 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002444 break;
2445 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002446 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002447 break;
Alex Wu4539b082012-03-01 12:57:46 +08002448 case SHELL_SURFACE_FULLSCREEN:
2449 shell_map_fullscreen(shsurf);
2450 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002451 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002452 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002453 panel_height = get_output_panel_height(shell,surface->output);
2454 weston_surface_set_position(surface, surface->output->x,
2455 surface->output->y + panel_height);
2456 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002457 case SHELL_SURFACE_LOCK:
2458 center_on_output(surface, get_default_output(compositor));
2459 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002460 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002461 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002462 case SHELL_SURFACE_NONE:
2463 weston_surface_set_position(surface,
2464 surface->geometry.x + sx,
2465 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002466 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002467 default:
2468 ;
2469 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002470
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002471 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002472 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002473 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002474 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002475 wl_list_insert(&shell->background_layer.surface_list,
2476 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002477 break;
2478 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002479 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002480 wl_list_insert(&shell->panel_layer.surface_list,
2481 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002482 break;
2483 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002484 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002485 wl_list_insert(&shell->lock_layer.surface_list,
2486 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002487 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002488 break;
2489 case SHELL_SURFACE_SCREENSAVER:
2490 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002491 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002492 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002493 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002494 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002495 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002496 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002497 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002498 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002499 case SHELL_SURFACE_POPUP:
2500 case SHELL_SURFACE_TRANSIENT:
2501 parent = shsurf->parent->surface;
2502 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2503 break;
Alex Wu4539b082012-03-01 12:57:46 +08002504 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002505 case SHELL_SURFACE_NONE:
2506 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002507 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002508 ws = get_current_workspace(shell);
2509 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002510 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002511 }
2512
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002513 if (surface_type != SHELL_SURFACE_NONE) {
2514 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002515 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2516 surface->output = shsurf->output;
2517 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002518
Juan Zhao7bb92f02011-12-15 11:31:51 -05002519 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002520 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002521 if (shsurf->transient.flags ==
2522 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2523 break;
2524 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002525 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002526 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002527 if (!shell->locked) {
2528 wl_list_for_each(seat, &compositor->seat_list, link)
2529 activate(shell, surface, seat);
2530 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002531 break;
2532 default:
2533 break;
2534 }
2535
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002536 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002537 {
2538 switch (shell->win_animation_type) {
2539 case ANIMATION_FADE:
2540 weston_fade_run(surface, NULL, NULL);
2541 break;
2542 case ANIMATION_ZOOM:
2543 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2544 break;
2545 default:
2546 break;
2547 }
2548 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002549}
2550
2551static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002552configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002553 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002554{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002555 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2556 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002557
Pekka Paalanen77346a62011-11-30 16:26:35 +02002558 shsurf = get_shell_surface(surface);
2559 if (shsurf)
2560 surface_type = shsurf->type;
2561
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002562 surface->geometry.x = x;
2563 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002564 surface->geometry.width = width;
2565 surface->geometry.height = height;
2566 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002567
2568 switch (surface_type) {
2569 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002570 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002571 break;
Alex Wu4539b082012-03-01 12:57:46 +08002572 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002573 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002574 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002575 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002576 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002577 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002578 surface->geometry.x = surface->output->x;
2579 surface->geometry.y = surface->output->y +
2580 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002581 break;
Alex Wu4539b082012-03-01 12:57:46 +08002582 case SHELL_SURFACE_TOPLEVEL:
2583 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002584 default:
2585 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002586 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002587
Alex Wu4539b082012-03-01 12:57:46 +08002588 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002589 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002590 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002591
2592 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2593 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002594 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2595 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002596 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002597}
2598
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002599static void
2600shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2601{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002602 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002603 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002604 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002605
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002606 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002607 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002608 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002609 type_changed = 1;
2610 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002611
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002612 if (!weston_surface_is_mapped(es)) {
2613 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002614 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002615 es->geometry.width != es->buffer->width ||
2616 es->geometry.height != es->buffer->height) {
2617 GLfloat from_x, from_y;
2618 GLfloat to_x, to_y;
2619
2620 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2621 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2622 configure(shell, es,
2623 es->geometry.x + to_x - from_x,
2624 es->geometry.y + to_y - from_y,
2625 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002626 }
2627}
2628
Tiago Vignattibe143262012-04-16 17:31:41 +03002629static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002630
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002631static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002632desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002633{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002634 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002635 struct desktop_shell *shell =
2636 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002637
2638 shell->child.process.pid = 0;
2639 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002640
2641 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2642 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002643 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002644 shell->child.deathstamp = time;
2645 shell->child.deathcount = 0;
2646 }
2647
2648 shell->child.deathcount++;
2649 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002650 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002651 return;
2652 }
2653
Martin Minarik6d118362012-06-07 18:01:59 +02002654 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002655 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002656}
2657
2658static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002659launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002660{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002661 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002662
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002663 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002664 &shell->child.process,
2665 shell_exe,
2666 desktop_shell_sigchld);
2667
2668 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002669 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002670 return 0;
2671}
2672
2673static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002674bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2675{
Tiago Vignattibe143262012-04-16 17:31:41 +03002676 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002677
2678 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002679 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002680}
2681
Kristian Høgsberg75840622011-09-06 13:48:16 -04002682static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002683unbind_desktop_shell(struct wl_resource *resource)
2684{
Tiago Vignattibe143262012-04-16 17:31:41 +03002685 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002686
2687 if (shell->locked)
2688 resume_desktop(shell);
2689
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002690 shell->child.desktop_shell = NULL;
2691 shell->prepare_event_sent = false;
2692 free(resource);
2693}
2694
2695static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002696bind_desktop_shell(struct wl_client *client,
2697 void *data, uint32_t version, uint32_t id)
2698{
Tiago Vignattibe143262012-04-16 17:31:41 +03002699 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002700 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002701
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002702 resource = wl_client_add_object(client, &desktop_shell_interface,
2703 &desktop_shell_implementation,
2704 id, shell);
2705
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002706 if (client == shell->child.client) {
2707 resource->destroy = unbind_desktop_shell;
2708 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002709 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002710 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002711
2712 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2713 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002714 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002715}
2716
Pekka Paalanen6e168112011-11-24 11:34:05 +02002717static void
2718screensaver_set_surface(struct wl_client *client,
2719 struct wl_resource *resource,
2720 struct wl_resource *shell_surface_resource,
2721 struct wl_resource *output_resource)
2722{
Tiago Vignattibe143262012-04-16 17:31:41 +03002723 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002724 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002725 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002726
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002727 surface->next_type = SHELL_SURFACE_SCREENSAVER;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002728
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002729 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002730 surface->output = output;
2731 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002732}
2733
2734static const struct screensaver_interface screensaver_implementation = {
2735 screensaver_set_surface
2736};
2737
2738static void
2739unbind_screensaver(struct wl_resource *resource)
2740{
Tiago Vignattibe143262012-04-16 17:31:41 +03002741 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002742
Pekka Paalanen77346a62011-11-30 16:26:35 +02002743 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002744 free(resource);
2745}
2746
2747static void
2748bind_screensaver(struct wl_client *client,
2749 void *data, uint32_t version, uint32_t id)
2750{
Tiago Vignattibe143262012-04-16 17:31:41 +03002751 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002752 struct wl_resource *resource;
2753
2754 resource = wl_client_add_object(client, &screensaver_interface,
2755 &screensaver_implementation,
2756 id, shell);
2757
Pekka Paalanen77346a62011-11-30 16:26:35 +02002758 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002759 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002760 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002761 return;
2762 }
2763
2764 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2765 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002766 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002767}
2768
Kristian Høgsberg07045392012-02-19 18:52:44 -05002769struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002770 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002771 struct weston_surface *current;
2772 struct wl_listener listener;
2773 struct wl_keyboard_grab grab;
2774};
2775
2776static void
2777switcher_next(struct switcher *switcher)
2778{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002779 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002780 struct weston_surface *surface;
2781 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002782 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002783
2784 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002785 switch (get_shell_surface_type(surface)) {
2786 case SHELL_SURFACE_TOPLEVEL:
2787 case SHELL_SURFACE_FULLSCREEN:
2788 case SHELL_SURFACE_MAXIMIZED:
2789 if (first == NULL)
2790 first = surface;
2791 if (prev == switcher->current)
2792 next = surface;
2793 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06002794 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002795 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002796 weston_surface_damage(surface);
2797 break;
2798 default:
2799 break;
2800 }
Alex Wu1659daa2012-04-01 20:13:09 +08002801
2802 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06002803 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08002804 surface->geometry.dirty = 1;
2805 weston_surface_damage(surface);
2806 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002807 }
2808
2809 if (next == NULL)
2810 next = first;
2811
Alex Wu07b26062012-03-12 16:06:01 +08002812 if (next == NULL)
2813 return;
2814
Kristian Høgsberg07045392012-02-19 18:52:44 -05002815 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002816 wl_signal_add(&next->surface.resource.destroy_signal,
2817 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002818
2819 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002820 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08002821
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002822 shsurf = get_shell_surface(switcher->current);
2823 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002824 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002825}
2826
2827static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002828switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002829{
2830 struct switcher *switcher =
2831 container_of(listener, struct switcher, listener);
2832
2833 switcher_next(switcher);
2834}
2835
2836static void
Daniel Stone351eb612012-05-31 15:27:47 -04002837switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002838{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002839 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002840 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002841 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002842
2843 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04002844 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002845 weston_surface_damage(surface);
2846 }
2847
Alex Wu07b26062012-03-12 16:06:01 +08002848 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01002849 activate(switcher->shell, switcher->current,
2850 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002851 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01002852 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002853 free(switcher);
2854}
2855
2856static void
2857switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002858 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002859{
2860 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01002861 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04002862
Daniel Stonec9785ea2012-05-30 16:31:52 +01002863 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04002864 switcher_next(switcher);
2865}
2866
2867static void
2868switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
2869 uint32_t mods_depressed, uint32_t mods_latched,
2870 uint32_t mods_locked, uint32_t group)
2871{
2872 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002873 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002874
Daniel Stone351eb612012-05-31 15:27:47 -04002875 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
2876 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002877}
Kristian Høgsberg07045392012-02-19 18:52:44 -05002878
2879static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04002880 switcher_key,
2881 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05002882};
2883
2884static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002885switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2886 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002887{
Tiago Vignattibe143262012-04-16 17:31:41 +03002888 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002889 struct switcher *switcher;
2890
2891 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002892 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002893 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002894 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002895 wl_list_init(&switcher->listener.link);
2896
2897 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01002898 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
2899 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002900 switcher_next(switcher);
2901}
2902
Pekka Paalanen3c647232011-12-22 13:43:43 +02002903static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002904backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2905 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002906{
2907 struct weston_compositor *compositor = data;
2908 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002909 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002910
2911 /* TODO: we're limiting to simple use cases, where we assume just
2912 * control on the primary display. We'd have to extend later if we
2913 * ever get support for setting backlights on random desktop LCD
2914 * panels though */
2915 output = get_default_output(compositor);
2916 if (!output)
2917 return;
2918
2919 if (!output->set_backlight)
2920 return;
2921
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002922 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2923 backlight_new = output->backlight_current - 25;
2924 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2925 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002926
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002927 if (backlight_new < 5)
2928 backlight_new = 5;
2929 if (backlight_new > 255)
2930 backlight_new = 255;
2931
2932 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002933 output->set_backlight(output, output->backlight_current);
2934}
2935
2936static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002937debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2938 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002939{
Tiago Vignattibe143262012-04-16 17:31:41 +03002940 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002941 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002942 struct weston_surface *surface;
2943
2944 if (shell->debug_repaint_surface) {
2945 weston_surface_destroy(shell->debug_repaint_surface);
2946 shell->debug_repaint_surface = NULL;
2947 } else {
2948 surface = weston_surface_create(compositor);
2949 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2950 weston_surface_configure(surface, 0, 0, 8192, 8192);
2951 wl_list_insert(&compositor->fade_layer.surface_list,
2952 &surface->layer_link);
2953 weston_surface_assign_output(surface);
2954 pixman_region32_init(&surface->input);
2955
2956 /* Here's the dirty little trick that makes the
2957 * repaint debugging work: we force an
2958 * update_transform first to update dependent state
2959 * and clear the geometry.dirty bit. Then we clear
2960 * the surface damage so it only gets repainted
2961 * piecewise as we repaint other things. */
2962
2963 weston_surface_update_transform(surface);
2964 pixman_region32_fini(&surface->damage);
2965 pixman_region32_init(&surface->damage);
2966 shell->debug_repaint_surface = surface;
2967 }
2968}
2969
2970static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002971force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2972 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04002973{
2974 struct wl_client *client;
2975 pid_t pid;
2976 uid_t uid;
2977 gid_t gid;
2978
Daniel Stone325fc2d2012-05-30 16:31:58 +01002979 client = seat->keyboard->focus->resource.client;
2980 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04002981
Daniel Stone325fc2d2012-05-30 16:31:58 +01002982 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04002983}
2984
2985static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002986workspace_up_binding(struct wl_seat *seat, uint32_t time,
2987 uint32_t key, void *data)
2988{
2989 struct desktop_shell *shell = data;
2990 unsigned int new_index = shell->workspaces.current;
2991
2992 if (new_index != 0)
2993 new_index--;
2994
2995 change_workspace(shell, new_index);
2996}
2997
2998static void
2999workspace_down_binding(struct wl_seat *seat, uint32_t time,
3000 uint32_t key, void *data)
3001{
3002 struct desktop_shell *shell = data;
3003 unsigned int new_index = shell->workspaces.current;
3004
3005 if (new_index < shell->workspaces.num - 1)
3006 new_index++;
3007
3008 change_workspace(shell, new_index);
3009}
3010
3011static void
3012workspace_f_binding(struct wl_seat *seat, uint32_t time,
3013 uint32_t key, void *data)
3014{
3015 struct desktop_shell *shell = data;
3016 unsigned int new_index;
3017
3018 new_index = key - KEY_F1;
3019 if (new_index >= shell->workspaces.num)
3020 new_index = shell->workspaces.num - 1;
3021
3022 change_workspace(shell, new_index);
3023}
3024
3025
3026static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003027shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003028{
Tiago Vignattibe143262012-04-16 17:31:41 +03003029 struct desktop_shell *shell =
3030 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003031 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003032
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003033 if (shell->child.client)
3034 wl_client_destroy(shell->child.client);
3035
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003036 wl_list_remove(&shell->lock_listener.link);
3037 wl_list_remove(&shell->unlock_listener.link);
3038
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003039 wl_array_for_each(ws, &shell->workspaces.array)
3040 workspace_destroy(*ws);
3041 wl_array_release(&shell->workspaces.array);
3042
Pekka Paalanen3c647232011-12-22 13:43:43 +02003043 free(shell->screensaver.path);
3044 free(shell);
3045}
3046
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003047static void
3048shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3049{
3050 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003051 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003052
3053 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003054 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3055 MODIFIER_CTRL | MODIFIER_ALT,
3056 terminate_binding, ec);
3057 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3058 click_to_activate_binding,
3059 shell);
3060 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3061 MODIFIER_SUPER | MODIFIER_ALT,
3062 surface_opacity_binding, NULL);
3063 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3064 MODIFIER_SUPER, zoom_axis_binding,
3065 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003066
3067 /* configurable bindings */
3068 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003069 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3070 zoom_key_binding, NULL);
3071 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3072 zoom_key_binding, NULL);
3073 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3074 shell);
3075 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3076 resize_binding, shell);
3077 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3078 rotate_binding, NULL);
3079 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3080 shell);
3081 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3082 ec);
3083 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3084 backlight_binding, ec);
3085 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3086 ec);
3087 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3088 backlight_binding, ec);
3089 weston_compositor_add_key_binding(ec, KEY_SPACE, mod,
3090 debug_repaint_binding, shell);
3091 weston_compositor_add_key_binding(ec, KEY_K, mod,
3092 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003093 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3094 workspace_up_binding, shell);
3095 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3096 workspace_down_binding, shell);
3097
3098 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3099 if (shell->workspaces.num > 1) {
3100 num_workspace_bindings = shell->workspaces.num;
3101 if (num_workspace_bindings > 6)
3102 num_workspace_bindings = 6;
3103 for (i = 0; i < num_workspace_bindings; i++)
3104 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3105 workspace_f_binding,
3106 shell);
3107 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003108}
3109
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003110int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003111shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003112
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003113WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003114shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003115{
Tiago Vignattibe143262012-04-16 17:31:41 +03003116 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003117 struct workspace **pws;
3118 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003119
3120 shell = malloc(sizeof *shell);
3121 if (shell == NULL)
3122 return -1;
3123
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003124 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003125 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003126
3127 shell->destroy_listener.notify = shell_destroy;
3128 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3129 shell->lock_listener.notify = lock;
3130 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3131 shell->unlock_listener.notify = unlock;
3132 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003133 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003134 ec->shell_interface.create_shell_surface = create_shell_surface;
3135 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003136 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003137 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003138 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003139
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003140 wl_list_init(&shell->backgrounds);
3141 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003142 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003143
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003144 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3145 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003146 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3147 weston_layer_init(&shell->lock_layer, NULL);
3148
3149 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003150
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003151 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003152
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003153 for (i = 0; i < shell->workspaces.num; i++) {
3154 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3155 if (pws == NULL)
3156 return -1;
3157
3158 *pws = workspace_create();
3159 if (*pws == NULL)
3160 return -1;
3161 }
3162 activate_workspace(shell, 0);
3163
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003164 wl_list_init(&shell->workspaces.animation.link);
3165 shell->workspaces.animation.frame = animate_workspace_change_frame;
3166
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003167 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3168 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003169 return -1;
3170
Kristian Høgsberg75840622011-09-06 13:48:16 -04003171 if (wl_display_add_global(ec->wl_display,
3172 &desktop_shell_interface,
3173 shell, bind_desktop_shell) == NULL)
3174 return -1;
3175
Pekka Paalanen6e168112011-11-24 11:34:05 +02003176 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3177 shell, bind_screensaver) == NULL)
3178 return -1;
3179
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003180 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003181 if (launch_desktop_shell_process(shell) != 0)
3182 return -1;
3183
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003184 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003185 if (ec->seat->seat.pointer)
3186 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3187 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003188
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003189 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003190
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003191 return 0;
3192}