blob: c7ff3c547b2a92497b372ef1be748eaca049cc65 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040033#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050034
Pekka Paalanen50719bc2011-11-22 14:18:50 +020035#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040037#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020038#include "../shared/config-parser.h"
Martin Minarik6d118362012-06-07 18:01:59 +020039#include "log.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050040
Jonas Ådahle3cddce2012-06-13 00:01:22 +020041#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020042#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020043
Juan Zhaoe10d2792012-04-25 19:09:52 +080044enum animation_type {
45 ANIMATION_NONE,
46
47 ANIMATION_ZOOM,
48 ANIMATION_FADE
49};
50
Jonas Ådahl04769742012-06-13 00:01:24 +020051struct focus_state {
52 struct weston_seat *seat;
53 struct weston_surface *keyboard_focus;
54 struct wl_list link;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
57};
58
Jonas Ådahle3cddce2012-06-13 00:01:22 +020059struct workspace {
60 struct weston_layer layer;
Jonas Ådahl04769742012-06-13 00:01:24 +020061
62 struct wl_list focus_list;
63 struct wl_listener seat_destroyed_listener;
Jonas Ådahle3cddce2012-06-13 00:01:22 +020064};
65
Tiago Vignattibe143262012-04-16 17:31:41 +030066struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040068
69 struct wl_listener lock_listener;
70 struct wl_listener unlock_listener;
71 struct wl_listener destroy_listener;
Jan Arne Petersen42feced2012-06-21 21:52:17 +020072 struct wl_listener show_input_panel_listener;
73 struct wl_listener hide_input_panel_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020074
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050075 struct weston_layer fullscreen_layer;
76 struct weston_layer panel_layer;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050077 struct weston_layer background_layer;
78 struct weston_layer lock_layer;
79
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040080 struct wl_listener pointer_focus_listener;
81 struct weston_surface *busy_surface;
82
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020083 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050084 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020085 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020086 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020087
88 unsigned deathcount;
89 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020090 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020091
92 bool locked;
93 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020094
Pekka Paalanen068ae942011-11-28 14:11:15 +020095 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050096 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010097
98 struct wl_list backgrounds;
99 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +0200100
Pekka Paalanen77346a62011-11-30 16:26:35 +0200101 struct {
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200102 struct wl_array array;
103 unsigned int current;
104 unsigned int num;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200105
106 struct weston_animation animation;
107 int anim_dir;
108 uint32_t anim_timestamp;
109 double anim_current;
110 struct workspace *anim_from;
111 struct workspace *anim_to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200112 } workspaces;
113
114 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +0200115 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200116 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200117 struct wl_resource *binding;
118 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500119 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200120 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500121
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200122 struct {
123 struct wl_resource *binding;
124 struct wl_list surfaces;
125 } input_panel;
126
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300127 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800128 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -0500129 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400130};
131
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500132enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +0200133 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500134
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200135 SHELL_SURFACE_PANEL,
136 SHELL_SURFACE_BACKGROUND,
137 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200138 SHELL_SURFACE_SCREENSAVER,
Jan Arne Petersen42feced2012-06-21 21:52:17 +0200139 SHELL_SURFACE_INPUT_PANEL,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500140
141 SHELL_SURFACE_TOPLEVEL,
142 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500143 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800144 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500145 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200146};
147
Scott Moreauff1db4a2012-04-17 19:06:18 -0600148struct ping_timer {
149 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600150 uint32_t serial;
151};
152
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200153struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200154 struct wl_resource resource;
155
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500156 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200157 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500158 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300159 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200160
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400161 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400162 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500163 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800164 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800165 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600166 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100167
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500168 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200169 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500170 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200171 } rotation;
172
173 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700174 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500175 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200176 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500177 int32_t initial_up;
Daniel Stone37816df2012-05-16 18:45:18 +0100178 struct wl_seat *seat;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400179 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500180 } popup;
181
Alex Wu4539b082012-03-01 12:57:46 +0800182 struct {
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300183 int32_t x, y;
Tiago Vignatti491bac12012-05-18 16:37:43 -0400184 uint32_t flags;
Tiago Vignatti52e598c2012-05-07 15:23:08 +0300185 } transient;
186
187 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800188 enum wl_shell_surface_fullscreen_method type;
189 struct weston_transform transform; /* matrix from x, y */
190 uint32_t framerate;
191 struct weston_surface *black_surface;
192 } fullscreen;
193
Scott Moreauff1db4a2012-04-17 19:06:18 -0600194 struct ping_timer *ping_timer;
195
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200196 struct weston_transform workspace_transform;
197
Jonas Ådahl04769742012-06-13 00:01:24 +0200198 struct focus_state *focus_state;
199
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500200 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500201 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100202 struct wl_list link;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400203
204 const struct weston_shell_client *client;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200205};
206
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300207struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700208 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300209 struct shell_surface *shsurf;
210 struct wl_listener shsurf_destroy_listener;
211};
212
213struct weston_move_grab {
214 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100215 wl_fixed_t dx, dy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500216};
217
Pekka Paalanen460099f2012-01-20 16:48:25 +0200218struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300219 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500220 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200221 struct {
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -0400222 GLfloat x;
223 GLfloat y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200224 } center;
225};
226
Alex Wubd3354b2012-04-17 17:20:49 +0800227static struct shell_surface *
228get_shell_surface(struct weston_surface *surface);
229
230static struct desktop_shell *
231shell_surface_get_shell(struct shell_surface *shsurf);
232
233static bool
234shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
235{
236 struct desktop_shell *shell;
237 struct weston_surface *top_fs_es;
238
239 shell = shell_surface_get_shell(shsurf);
240
241 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
242 return false;
243
244 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
245 struct weston_surface,
246 layer_link);
247 return (shsurf == get_shell_surface(top_fs_es));
248}
249
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500250static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400251destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300252{
253 struct shell_grab *grab;
254
255 grab = container_of(listener, struct shell_grab,
256 shsurf_destroy_listener);
257
258 grab->shsurf = NULL;
259}
260
261static void
262shell_grab_init(struct shell_grab *grab,
263 const struct wl_pointer_grab_interface *interface,
264 struct shell_surface *shsurf)
265{
266 grab->grab.interface = interface;
267 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400268 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
269 wl_signal_add(&shsurf->resource.destroy_signal,
270 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300271
272}
273
274static void
275shell_grab_finish(struct shell_grab *grab)
276{
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400277 if (grab->shsurf)
278 wl_list_remove(&grab->shsurf_destroy_listener.link);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300279}
280
281static void
Alex Wu4539b082012-03-01 12:57:46 +0800282center_on_output(struct weston_surface *surface,
283 struct weston_output *output);
284
Daniel Stone496ca172012-05-30 16:31:42 +0100285static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300286get_modifier(char *modifier)
287{
288 if (!modifier)
289 return MODIFIER_SUPER;
290
291 if (!strcmp("ctrl", modifier))
292 return MODIFIER_CTRL;
293 else if (!strcmp("alt", modifier))
294 return MODIFIER_ALT;
295 else if (!strcmp("super", modifier))
296 return MODIFIER_SUPER;
297 else
298 return MODIFIER_SUPER;
299}
300
Juan Zhaoe10d2792012-04-25 19:09:52 +0800301static enum animation_type
302get_animation_type(char *animation)
303{
304 if (!animation)
305 return ANIMATION_NONE;
306
307 if (!strcmp("zoom", animation))
308 return ANIMATION_ZOOM;
309 else if (!strcmp("fade", animation))
310 return ANIMATION_FADE;
311 else
312 return ANIMATION_NONE;
313}
314
Alex Wu4539b082012-03-01 12:57:46 +0800315static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300316shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200317{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200318 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200319 char *path = NULL;
320 int duration = 60;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200321 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300322 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800323 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200324
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400325 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300326 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800327 { "animation", CONFIG_KEY_STRING, &win_animation},
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200328 { "num-workspaces",
329 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200330 };
331
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400332 struct config_key saver_keys[] = {
333 { "path", CONFIG_KEY_STRING, &path },
334 { "duration", CONFIG_KEY_INTEGER, &duration },
335 };
336
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200337 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400338 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200339 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
340 };
341
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200342 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500343 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200344 free(config_file);
345
Pekka Paalanen7296e792011-12-07 16:22:00 +0200346 shell->screensaver.path = path;
347 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300348 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800349 shell->win_animation_type = get_animation_type(win_animation);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200350 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
351}
352
353static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200354focus_state_destroy(struct focus_state *state)
355{
356 wl_list_remove(&state->seat_destroy_listener.link);
357 wl_list_remove(&state->surface_destroy_listener.link);
358 free(state);
359}
360
361static void
362focus_state_seat_destroy(struct wl_listener *listener, void *data)
363{
364 struct focus_state *state = container_of(listener,
365 struct focus_state,
366 seat_destroy_listener);
367
368 wl_list_remove(&state->link);
369 focus_state_destroy(state);
370}
371
372static void
373focus_state_surface_destroy(struct wl_listener *listener, void *data)
374{
375 struct focus_state *state = container_of(listener,
376 struct focus_state,
377 seat_destroy_listener);
378
379 wl_list_remove(&state->link);
380 focus_state_destroy(state);
381}
382
383static struct focus_state *
384focus_state_create(struct weston_seat *seat)
385{
386 struct wl_keyboard *keyboard = seat->seat.keyboard;
387 struct focus_state *state;
388 struct wl_surface *surface;
389 struct shell_surface *shsurf;
390
391 state = malloc(sizeof *state);
392 if (state == NULL)
393 return NULL;
394
395 surface = keyboard->focus;
396 shsurf = get_shell_surface((struct weston_surface *)keyboard->focus);
397 shsurf->focus_state = state;
398
399 state->seat = seat;
400 state->keyboard_focus = shsurf->surface;
401 wl_list_init(&state->link);
402
403 state->seat_destroy_listener.notify = focus_state_seat_destroy;
404 state->surface_destroy_listener.notify = focus_state_surface_destroy;
405 wl_signal_add(&seat->seat.destroy_signal,
406 &state->seat_destroy_listener);
407 wl_signal_add(&surface->resource.destroy_signal,
408 &state->surface_destroy_listener);
409
410 return state;
411}
412
413static void
414pop_focus_state(struct desktop_shell *shell, struct workspace *ws)
415{
416 struct focus_state *state, *next;
417
418 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
419 if (state->keyboard_focus)
420 wl_keyboard_set_focus(state->seat->seat.keyboard,
421 &state->keyboard_focus->surface);
422
423 focus_state_destroy(state);
424 }
425 wl_list_init(&ws->focus_list);
426}
427
428static void
429push_focus_state(struct desktop_shell *shell, struct workspace *ws)
430{
431 struct weston_seat *seat;
432 struct focus_state *state;
433 struct wl_keyboard *keyboard;
434
435 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
436 keyboard = seat->seat.keyboard;
437 if (keyboard && keyboard->focus) {
438 state = focus_state_create(seat);
439 if (state == NULL)
440 return;
441
442 wl_list_insert(&ws->focus_list, &state->link);
443
444 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
445 }
446 }
447}
448
449static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200450workspace_destroy(struct workspace *ws)
451{
Jonas Ådahl04769742012-06-13 00:01:24 +0200452 struct focus_state *state, *next;
453
454 wl_list_for_each_safe(state, next, &ws->focus_list, link)
455 focus_state_destroy(state);
456
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200457 free(ws);
458}
459
Jonas Ådahl04769742012-06-13 00:01:24 +0200460static void
461seat_destroyed(struct wl_listener *listener, void *data)
462{
463 struct weston_seat *seat = data;
464 struct focus_state *state, *next;
465 struct workspace *ws = container_of(listener,
466 struct workspace,
467 seat_destroyed_listener);
468
469 wl_list_for_each_safe(state, next, &ws->focus_list, link)
470 if (state->seat == seat)
471 wl_list_remove(&state->link);
472}
473
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200474static struct workspace *
475workspace_create(void)
476{
477 struct workspace *ws = malloc(sizeof *ws);
478 if (ws == NULL)
479 return NULL;
480
481 weston_layer_init(&ws->layer, NULL);
482
Jonas Ådahl04769742012-06-13 00:01:24 +0200483 wl_list_init(&ws->focus_list);
484 wl_list_init(&ws->seat_destroyed_listener.link);
485 ws->seat_destroyed_listener.notify = seat_destroyed;
486
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200487 return ws;
488}
489
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200490static int
491workspace_is_empty(struct workspace *ws)
492{
493 return wl_list_empty(&ws->layer.surface_list);
494}
495
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200496static struct workspace *
497get_workspace(struct desktop_shell *shell, unsigned int index)
498{
499 struct workspace **pws = shell->workspaces.array.data;
500 pws += index;
501 return *pws;
502}
503
504static struct workspace *
505get_current_workspace(struct desktop_shell *shell)
506{
507 return get_workspace(shell, shell->workspaces.current);
508}
509
510static void
511activate_workspace(struct desktop_shell *shell, unsigned int index)
512{
513 struct workspace *ws;
514
515 ws = get_workspace(shell, index);
516 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
517
518 shell->workspaces.current = index;
519}
520
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200521static unsigned int
522get_output_height(struct weston_output *output)
523{
524 return abs(output->region.extents.y1 - output->region.extents.y2);
525}
526
527static void
528surface_translate(struct weston_surface *surface, double d)
529{
530 struct shell_surface *shsurf = get_shell_surface(surface);
531 struct weston_transform *transform;
532
533 transform = &shsurf->workspace_transform;
534 if (wl_list_empty(&transform->link))
535 wl_list_insert(surface->geometry.transformation_list.prev,
536 &shsurf->workspace_transform.link);
537
538 weston_matrix_init(&shsurf->workspace_transform.matrix);
539 weston_matrix_translate(&shsurf->workspace_transform.matrix,
540 0.0, d, 0.0);
541 surface->geometry.dirty = 1;
542}
543
544static void
545workspace_translate_out(struct workspace *ws, double fraction)
546{
547 struct weston_surface *surface;
548 unsigned int height;
549 double d;
550
551 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
552 height = get_output_height(surface->output);
553 d = height * fraction;
554
555 surface_translate(surface, d);
556 }
557}
558
559static void
560workspace_translate_in(struct workspace *ws, double fraction)
561{
562 struct weston_surface *surface;
563 unsigned int height;
564 double d;
565
566 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
567 height = get_output_height(surface->output);
568
569 if (fraction > 0)
570 d = -(height - height * fraction);
571 else
572 d = height + height * fraction;
573
574 surface_translate(surface, d);
575 }
576}
577
578static void
579workspace_damage_all_surfaces(struct workspace *ws)
580{
581 struct weston_surface *surface;
582
583 wl_list_for_each(surface, &ws->layer.surface_list, layer_link)
584 weston_surface_damage(surface);
585}
586
587static void
588reverse_workspace_change_animation(struct desktop_shell *shell,
589 unsigned int index,
590 struct workspace *from,
591 struct workspace *to)
592{
593 shell->workspaces.current = index;
594
595 shell->workspaces.anim_to = to;
596 shell->workspaces.anim_from = from;
597 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
598 shell->workspaces.anim_timestamp = 0;
599
Jonas Ådahl04769742012-06-13 00:01:24 +0200600 push_focus_state(shell, from);
601 pop_focus_state(shell, to);
602
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200603 workspace_damage_all_surfaces(from);
604 workspace_damage_all_surfaces(to);
605}
606
607static void
608workspace_deactivate_transforms(struct workspace *ws)
609{
610 struct weston_surface *surface;
611 struct shell_surface *shsurf;
612
613 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
614 shsurf = get_shell_surface(surface);
615 wl_list_remove(&shsurf->workspace_transform.link);
616 wl_list_init(&shsurf->workspace_transform.link);
617 shsurf->surface->geometry.dirty = 1;
618 }
619}
620
621static void
622finish_workspace_change_animation(struct desktop_shell *shell,
623 struct workspace *from,
624 struct workspace *to)
625{
626 workspace_damage_all_surfaces(from);
627 workspace_damage_all_surfaces(to);
628
629 wl_list_remove(&shell->workspaces.animation.link);
630 workspace_deactivate_transforms(from);
631 workspace_deactivate_transforms(to);
632 shell->workspaces.anim_to = NULL;
633
634 wl_list_remove(&shell->workspaces.anim_from->layer.link);
635}
636
637static void
638animate_workspace_change_frame(struct weston_animation *animation,
639 struct weston_output *output, uint32_t msecs)
640{
641 struct desktop_shell *shell =
642 container_of(animation, struct desktop_shell,
643 workspaces.animation);
644 struct workspace *from = shell->workspaces.anim_from;
645 struct workspace *to = shell->workspaces.anim_to;
646 uint32_t t;
647 double x, y;
648
649 if (workspace_is_empty(from) && workspace_is_empty(to)) {
650 finish_workspace_change_animation(shell, from, to);
651 return;
652 }
653
654 if (shell->workspaces.anim_timestamp == 0) {
655 if (shell->workspaces.anim_current == 0.0)
656 shell->workspaces.anim_timestamp = msecs;
657 else
658 shell->workspaces.anim_timestamp =
659 msecs -
660 /* Invers of movement function 'y' below. */
661 (asin(1.0 - shell->workspaces.anim_current) *
662 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
663 M_2_PI);
664 }
665
666 t = msecs - shell->workspaces.anim_timestamp;
667
668 /*
669 * x = [0, π/2]
670 * y(x) = sin(x)
671 */
672 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
673 y = sin(x);
674
675 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
676 workspace_damage_all_surfaces(from);
677 workspace_damage_all_surfaces(to);
678
679 workspace_translate_out(from, shell->workspaces.anim_dir * y);
680 workspace_translate_in(to, shell->workspaces.anim_dir * y);
681 shell->workspaces.anim_current = y;
682
683 workspace_damage_all_surfaces(from);
684 workspace_damage_all_surfaces(to);
685 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200686 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200687 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200688}
689
690static void
691animate_workspace_change(struct desktop_shell *shell,
692 unsigned int index,
693 struct workspace *from,
694 struct workspace *to)
695{
696 struct weston_output *output;
697
698 int dir;
699
700 if (index > shell->workspaces.current)
701 dir = -1;
702 else
703 dir = 1;
704
705 shell->workspaces.current = index;
706
707 shell->workspaces.anim_dir = dir;
708 shell->workspaces.anim_from = from;
709 shell->workspaces.anim_to = to;
710 shell->workspaces.anim_current = 0.0;
711 shell->workspaces.anim_timestamp = 0;
712
713 output = container_of(shell->compositor->output_list.next,
714 struct weston_output, link);
715 wl_list_insert(&output->animation_list,
716 &shell->workspaces.animation.link);
717
718 wl_list_insert(&from->layer.link, &to->layer.link);
719
720 workspace_translate_in(to, 0);
721
Jonas Ådahl04769742012-06-13 00:01:24 +0200722 push_focus_state(shell, from);
723 pop_focus_state(shell, to);
724
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200725 workspace_damage_all_surfaces(from);
726 workspace_damage_all_surfaces(to);
727}
728
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200729static void
730change_workspace(struct desktop_shell *shell, unsigned int index)
731{
732 struct workspace *from;
733 struct workspace *to;
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200734
735 if (index == shell->workspaces.current)
736 return;
737
738 /* Don't change workspace when there is any fullscreen surfaces. */
739 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
740 return;
741
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200742 from = get_current_workspace(shell);
743 to = get_workspace(shell, index);
744
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200745 if (shell->workspaces.anim_from == to &&
746 shell->workspaces.anim_to == from) {
747 reverse_workspace_change_animation(shell, index, from, to);
748 return;
749 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200750
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200751 if (shell->workspaces.anim_to != NULL)
752 finish_workspace_change_animation(shell,
753 shell->workspaces.anim_from,
754 shell->workspaces.anim_to);
755
756 if (workspace_is_empty(to) && workspace_is_empty(from)) {
757 shell->workspaces.current = index;
758 wl_list_insert(&from->layer.link, &to->layer.link);
759 wl_list_remove(&from->layer.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200760
761 push_focus_state(shell, from);
762 pop_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200763 }
764 else
765 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200766}
767
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200768static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400769noop_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100770 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500771{
772 grab->focus = NULL;
773}
774
775static void
Scott Moreau447013d2012-02-18 05:05:29 -0700776move_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +0100777 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500778{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500779 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +0100780 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300781 struct shell_surface *shsurf = move->base.shsurf;
782 struct weston_surface *es;
Daniel Stone37816df2012-05-16 18:45:18 +0100783 int dx = wl_fixed_to_int(pointer->x + move->dx);
784 int dy = wl_fixed_to_int(pointer->y + move->dy);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300785
786 if (!shsurf)
787 return;
788
789 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500790
Daniel Stone103db7f2012-05-08 17:17:55 +0100791 weston_surface_configure(es, dx, dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200792 es->geometry.width, es->geometry.height);
Kristian Høgsberg6c6fb992012-06-21 12:06:22 -0400793
794 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500795}
796
797static void
Scott Moreau447013d2012-02-18 05:05:29 -0700798move_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100799 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500800{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300801 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
802 grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100803 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100804 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500805
Daniel Stone4dbadb12012-05-30 16:31:51 +0100806 if (pointer->button_count == 0 &&
807 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300808 shell_grab_finish(shell_grab);
Daniel Stone37816df2012-05-16 18:45:18 +0100809 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500810 free(grab);
811 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500812}
813
Scott Moreau447013d2012-02-18 05:05:29 -0700814static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500815 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500816 move_grab_motion,
817 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500818};
819
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600820static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400821busy_cursor_grab_focus(struct wl_pointer_grab *base,
822 struct wl_surface *surface, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600823{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400824 struct shell_grab *grab = (struct shell_grab *) base;
825 struct wl_pointer *pointer = base->pointer;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600826
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400827 if (grab->grab.focus != surface) {
828 shell_grab_finish(grab);
829 wl_pointer_end_grab(pointer);
830 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600831 }
832}
833
834static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400835busy_cursor_grab_motion(struct wl_pointer_grab *grab,
836 uint32_t time, int32_t x, int32_t y)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600837{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400838}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600839
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400840static void
841busy_cursor_grab_button(struct wl_pointer_grab *grab,
842 uint32_t time, uint32_t button, uint32_t state)
843{
844}
845
846static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
847 busy_cursor_grab_focus,
848 busy_cursor_grab_motion,
849 busy_cursor_grab_button,
850};
851
852static void
853set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
854{
855 struct shell_grab *grab;
856 struct desktop_shell *shell = shsurf->shell;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400857
858 grab = malloc(sizeof *grab);
859 if (!grab)
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600860 return;
861
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400862 shell_grab_init(grab, &busy_cursor_grab_interface, shsurf);
863 grab->grab.focus = &shsurf->surface->surface;
864 wl_pointer_start_grab(pointer, &grab->grab);
865 wl_pointer_set_focus(pointer, &shell->busy_surface->surface, 0, 0);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400866}
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600867
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400868static void
869end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
870{
871 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
872
873 if (grab->grab.interface == &busy_cursor_grab_interface) {
874 shell_grab_finish(grab);
875 wl_pointer_end_grab(pointer);
876 free(grab);
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600877 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600878}
879
Scott Moreau9521d5e2012-04-19 13:06:17 -0600880static void
881ping_timer_destroy(struct shell_surface *shsurf)
882{
883 if (!shsurf || !shsurf->ping_timer)
884 return;
885
886 if (shsurf->ping_timer->source)
887 wl_event_source_remove(shsurf->ping_timer->source);
888
889 free(shsurf->ping_timer);
890 shsurf->ping_timer = NULL;
891}
892
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400893static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600894ping_timeout_handler(void *data)
895{
896 struct shell_surface *shsurf = data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400897 struct weston_seat *seat;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600898
Scott Moreau9521d5e2012-04-19 13:06:17 -0600899 /* Client is not responding */
900 shsurf->unresponsive = 1;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400901
902 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
903 if (seat->seat.pointer->focus == &shsurf->surface->surface)
904 set_busy_cursor(shsurf, seat->seat.pointer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600905
906 return 1;
907}
908
909static void
910ping_handler(struct weston_surface *surface, uint32_t serial)
911{
Kristian Høgsbergb71302e2012-05-10 12:28:35 -0400912 struct shell_surface *shsurf = get_shell_surface(surface);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600913 struct wl_event_loop *loop;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400914 int ping_timeout = 200;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600915
916 if (!shsurf)
917 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400918 if (!shsurf->resource.client)
919 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600920
921 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300922 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600923 if (!shsurf->ping_timer)
924 return;
925
926 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600927 loop = wl_display_get_event_loop(surface->compositor->wl_display);
928 shsurf->ping_timer->source =
929 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
930 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
931
932 wl_shell_surface_send_ping(&shsurf->resource, serial);
933 }
934}
935
936static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400937handle_pointer_focus(struct wl_listener *listener, void *data)
938{
939 struct wl_pointer *pointer = data;
940 struct weston_surface *surface =
941 (struct weston_surface *) pointer->focus;
942 struct weston_compositor *compositor;
943 struct shell_surface *shsurf;
944 uint32_t serial;
945
946 if (!surface)
947 return;
948
949 compositor = surface->compositor;
950 shsurf = get_shell_surface(surface);
951
Pekka Paalanen4e1f2ff2012-06-06 16:59:45 +0300952 if (shsurf && shsurf->unresponsive) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400953 set_busy_cursor(shsurf, pointer);
954 } else {
955 serial = wl_display_next_serial(compositor->wl_display);
956 ping_handler(surface, serial);
957 }
958}
959
960static void
Scott Moreauff1db4a2012-04-17 19:06:18 -0600961shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
962 uint32_t serial)
963{
964 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400965 struct desktop_shell *shell = shsurf->shell;
966 struct weston_seat *seat;
967 struct weston_compositor *ec = shsurf->surface->compositor;
968 struct wl_pointer *pointer;
969 int was_unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600970
Scott Moreauff1db4a2012-04-17 19:06:18 -0600971 if (shsurf->ping_timer->serial == serial) {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400972 was_unresponsive = shsurf->unresponsive;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600973 shsurf->unresponsive = 0;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400974 if (was_unresponsive) {
975 /* Received pong from previously unresponsive client */
976 wl_list_for_each(seat, &ec->seat_list, link) {
977 pointer = seat->seat.pointer;
978 if (pointer->focus ==
979 &shell->busy_surface->surface &&
980 pointer->current ==
981 &shsurf->surface->surface)
982 end_busy_cursor(shsurf, pointer);
983 }
984 }
Scott Moreau9521d5e2012-04-19 13:06:17 -0600985 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600986 }
987}
988
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400989static void
990shell_surface_set_title(struct wl_client *client,
991 struct wl_resource *resource, const char *title)
992{
993 struct shell_surface *shsurf = resource->data;
994
995 free(shsurf->title);
996 shsurf->title = strdup(title);
997}
998
999static void
1000shell_surface_set_class(struct wl_client *client,
1001 struct wl_resource *resource, const char *class)
1002{
1003 struct shell_surface *shsurf = resource->data;
1004
1005 free(shsurf->class);
1006 shsurf->class = strdup(class);
1007}
1008
Scott Moreauff1db4a2012-04-17 19:06:18 -06001009static int
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001010surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001011{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001012 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001013
1014 if (!shsurf)
1015 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001016
1017 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001018 if (!move)
1019 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001020
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001021 shell_grab_init(&move->base, &move_grab_interface, shsurf);
1022
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001023 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
Daniel Stone37816df2012-05-16 18:45:18 +01001024 ws->seat.pointer->grab_x;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001025 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
Daniel Stone37816df2012-05-16 18:45:18 +01001026 ws->seat.pointer->grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001027
Daniel Stone37816df2012-05-16 18:45:18 +01001028 wl_pointer_start_grab(ws->seat.pointer, &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001029
Daniel Stone37816df2012-05-16 18:45:18 +01001030 wl_pointer_set_focus(ws->seat.pointer, NULL,
1031 wl_fixed_from_int(0),
1032 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001033
1034 return 0;
1035}
1036
1037static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001038shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001039 struct wl_resource *seat_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001040{
Daniel Stone37816df2012-05-16 18:45:18 +01001041 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001042 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001043
Daniel Stone37816df2012-05-16 18:45:18 +01001044 if (ws->seat.pointer->button_count == 0 ||
1045 ws->seat.pointer->grab_serial != serial ||
1046 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001047 return;
1048
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04001049 if (surface_move(shsurf, ws) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001050 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001051}
1052
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001053struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001054 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001055 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001056 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001057};
1058
1059static void
Scott Moreau447013d2012-02-18 05:05:29 -07001060resize_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001061 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001062{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001063 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001064 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001065 struct shell_surface *shsurf = resize->base.shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001066 int32_t width, height;
Daniel Stone103db7f2012-05-08 17:17:55 +01001067 wl_fixed_t from_x, from_y;
1068 wl_fixed_t to_x, to_y;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001069
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001070 if (!shsurf)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001071 return;
1072
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001073 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001074 pointer->grab_x, pointer->grab_y,
Daniel Stone103db7f2012-05-08 17:17:55 +01001075 &from_x, &from_y);
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001076 weston_surface_from_global_fixed(shsurf->surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001077 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001078
Daniel Stone103db7f2012-05-08 17:17:55 +01001079 width = resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001080 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001081 width += wl_fixed_to_int(from_x - to_x);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001082 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001083 width += wl_fixed_to_int(to_x - from_x);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001084 }
1085
Daniel Stone103db7f2012-05-08 17:17:55 +01001086 height = resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001087 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001088 height += wl_fixed_to_int(from_y - to_y);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001089 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001090 height += wl_fixed_to_int(to_y - from_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001091 }
1092
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001093 shsurf->client->send_configure(shsurf->surface,
1094 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001095}
1096
1097static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001098send_configure(struct weston_surface *surface,
1099 uint32_t edges, int32_t width, int32_t height)
1100{
1101 struct shell_surface *shsurf = get_shell_surface(surface);
1102
1103 wl_shell_surface_send_configure(&shsurf->resource,
1104 edges, width, height);
1105}
1106
1107static const struct weston_shell_client shell_client = {
1108 send_configure
1109};
1110
1111static void
Scott Moreau447013d2012-02-18 05:05:29 -07001112resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001113 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001114{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001115 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Daniel Stone37816df2012-05-16 18:45:18 +01001116 struct wl_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001117 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001118
Daniel Stone4dbadb12012-05-30 16:31:51 +01001119 if (pointer->button_count == 0 &&
1120 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001121 shell_grab_finish(&resize->base);
Daniel Stone37816df2012-05-16 18:45:18 +01001122 wl_pointer_end_grab(pointer);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001123 free(grab);
1124 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001125}
1126
Scott Moreau447013d2012-02-18 05:05:29 -07001127static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001128 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001129 resize_grab_motion,
1130 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001131};
1132
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001133static int
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001134surface_resize(struct shell_surface *shsurf,
1135 struct weston_seat *ws, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001136{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001137 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001138
Alex Wu4539b082012-03-01 12:57:46 +08001139 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1140 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001141
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001142 if (edges == 0 || edges > 15 ||
1143 (edges & 3) == 3 || (edges & 12) == 12)
1144 return 0;
1145
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001146 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001147 if (!resize)
1148 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001149
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001150 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
1151
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001152 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001153 resize->width = shsurf->surface->geometry.width;
1154 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001155
Daniel Stone37816df2012-05-16 18:45:18 +01001156 wl_pointer_start_grab(ws->seat.pointer, &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001157
Daniel Stone37816df2012-05-16 18:45:18 +01001158 wl_pointer_set_focus(ws->seat.pointer, NULL,
1159 wl_fixed_from_int(0),
1160 wl_fixed_from_int(0));
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001161
1162 return 0;
1163}
1164
1165static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001166shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001167 struct wl_resource *seat_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001168 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001169{
Daniel Stone37816df2012-05-16 18:45:18 +01001170 struct weston_seat *ws = seat_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001171 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001172
Alex Wu4539b082012-03-01 12:57:46 +08001173 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1174 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001175
Daniel Stone37816df2012-05-16 18:45:18 +01001176 if (ws->seat.pointer->button_count == 0 ||
1177 ws->seat.pointer->grab_serial != serial ||
1178 ws->seat.pointer->focus != &shsurf->surface->surface)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001179 return;
1180
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001181 if (surface_resize(shsurf, ws, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001182 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001183}
1184
Juan Zhao96879df2012-02-07 08:45:41 +08001185static struct weston_output *
1186get_default_output(struct weston_compositor *compositor)
1187{
1188 return container_of(compositor->output_list.next,
1189 struct weston_output, link);
1190}
1191
Alex Wu4539b082012-03-01 12:57:46 +08001192static void
1193shell_unset_fullscreen(struct shell_surface *shsurf)
1194{
1195 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +08001196 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1197 shell_surface_is_top_fullscreen(shsurf)) {
1198 weston_output_switch_mode(shsurf->fullscreen_output,
1199 shsurf->fullscreen_output->origin);
1200 }
Alex Wu4539b082012-03-01 12:57:46 +08001201 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1202 shsurf->fullscreen.framerate = 0;
1203 wl_list_remove(&shsurf->fullscreen.transform.link);
1204 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +08001205 if (shsurf->fullscreen.black_surface)
1206 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001207 shsurf->fullscreen.black_surface = NULL;
1208 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001209 weston_surface_set_position(shsurf->surface,
1210 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001211 if (shsurf->saved_rotation_valid) {
1212 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1213 &shsurf->rotation.transform.link);
1214 shsurf->saved_rotation_valid = false;
1215 }
Alex Wu4539b082012-03-01 12:57:46 +08001216}
1217
Pekka Paalanen98262232011-12-01 10:42:22 +02001218static int
1219reset_shell_surface_type(struct shell_surface *surface)
1220{
1221 switch (surface->type) {
1222 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +08001223 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +02001224 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001225 case SHELL_SURFACE_MAXIMIZED:
1226 surface->output = get_default_output(surface->surface->compositor);
1227 weston_surface_set_position(surface->surface,
1228 surface->saved_x,
1229 surface->saved_y);
1230 break;
Pekka Paalanen98262232011-12-01 10:42:22 +02001231 case SHELL_SURFACE_PANEL:
1232 case SHELL_SURFACE_BACKGROUND:
Jan Arne Petersen42feced2012-06-21 21:52:17 +02001233 case SHELL_SURFACE_INPUT_PANEL:
Pekka Paalanen98262232011-12-01 10:42:22 +02001234 wl_list_remove(&surface->link);
1235 wl_list_init(&surface->link);
1236 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001237 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +02001238 case SHELL_SURFACE_LOCK:
1239 wl_resource_post_error(&surface->resource,
1240 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +02001241 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +02001242 return -1;
1243 case SHELL_SURFACE_NONE:
1244 case SHELL_SURFACE_TOPLEVEL:
1245 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001246 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +02001247 break;
1248 }
1249
1250 surface->type = SHELL_SURFACE_NONE;
1251 return 0;
1252}
1253
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001254static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001255set_surface_type(struct shell_surface *shsurf)
1256{
1257 struct weston_surface *surface = shsurf->surface;
1258 struct shell_surface *pshsurf = shsurf->parent;
1259 struct weston_surface *pes;
1260 struct shell_surface *priv;
1261 struct desktop_shell *shell = shsurf->shell;
1262
1263 reset_shell_surface_type(shsurf);
1264
1265 shsurf->type = shsurf->next_type;
1266 shsurf->next_type = SHELL_SURFACE_NONE;
1267
1268 switch (shsurf->type) {
1269 case SHELL_SURFACE_TOPLEVEL:
1270 break;
1271 case SHELL_SURFACE_TRANSIENT:
1272 pes = pshsurf->surface;
1273 weston_surface_set_position(surface,
Tiago Vignatti52e598c2012-05-07 15:23:08 +03001274 pes->geometry.x + shsurf->transient.x,
1275 pes->geometry.y + shsurf->transient.y);
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001276 break;
1277
1278 case SHELL_SURFACE_MAXIMIZED:
1279 shsurf->saved_x = surface->geometry.x;
1280 shsurf->saved_y = surface->geometry.y;
1281 shsurf->saved_position_valid = true;
1282 break;
1283
1284 case SHELL_SURFACE_FULLSCREEN:
1285 shsurf->saved_x = surface->geometry.x;
1286 shsurf->saved_y = surface->geometry.y;
1287 shsurf->saved_position_valid = true;
1288
1289 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1290 wl_list_remove(&shsurf->rotation.transform.link);
1291 wl_list_init(&shsurf->rotation.transform.link);
1292 shsurf->surface->geometry.dirty = 1;
1293 shsurf->saved_rotation_valid = true;
1294 }
1295 break;
1296
1297 case SHELL_SURFACE_BACKGROUND:
1298 wl_list_for_each(priv, &shell->backgrounds, link) {
1299 if (priv->output == shsurf->output) {
1300 priv->surface->output = NULL;
1301 wl_list_remove(&priv->surface->layer_link);
1302 wl_list_remove(&priv->link);
1303 break;
1304 }
1305 }
1306
1307 wl_list_insert(&shell->backgrounds, &shsurf->link);
1308
1309 weston_surface_set_position(surface, shsurf->output->x,
1310 shsurf->output->y);
1311 break;
1312
1313 case SHELL_SURFACE_PANEL:
1314 wl_list_for_each(priv, &shell->panels, link) {
1315 if (priv->output == shsurf->output) {
1316 priv->surface->output = NULL;
1317 wl_list_remove(&priv->surface->layer_link);
1318 wl_list_remove(&priv->link);
1319 break;
1320 }
1321 }
1322
1323 wl_list_insert(&shell->panels, &shsurf->link);
1324
1325 weston_surface_set_position(surface, shsurf->output->x,
1326 shsurf->output->y);
1327 break;
1328
1329 default:
1330 break;
1331 }
1332}
1333
1334static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001335set_toplevel(struct shell_surface *shsurf)
1336{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001337 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001338}
1339
1340static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001341shell_surface_set_toplevel(struct wl_client *client,
1342 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001343{
Pekka Paalanen98262232011-12-01 10:42:22 +02001344 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001345
Tiago Vignattibc052c92012-04-19 16:18:18 +03001346 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001347}
1348
1349static void
Tiago Vignatti491bac12012-05-18 16:37:43 -04001350set_transient(struct shell_surface *shsurf,
1351 struct shell_surface *pshsurf, int x, int y, uint32_t flags)
1352{
1353 /* assign to parents output */
1354 shsurf->parent = pshsurf;
1355 shsurf->transient.x = x;
1356 shsurf->transient.y = y;
1357 shsurf->transient.flags = flags;
1358 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1359}
1360
1361static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001362shell_surface_set_transient(struct wl_client *client,
1363 struct wl_resource *resource,
1364 struct wl_resource *parent_resource,
1365 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001366{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001367 struct shell_surface *shsurf = resource->data;
Tiago Vignatti491bac12012-05-18 16:37:43 -04001368 struct shell_surface *pshsurf = parent_resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001369
Tiago Vignatti491bac12012-05-18 16:37:43 -04001370 set_transient(shsurf, pshsurf, x, y, flags);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001371}
1372
Tiago Vignattibe143262012-04-16 17:31:41 +03001373static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08001374shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001375{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001376 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08001377}
1378
1379static int
Tiago Vignattibe143262012-04-16 17:31:41 +03001380get_output_panel_height(struct desktop_shell *shell,
1381 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +08001382{
1383 struct shell_surface *priv;
1384 int panel_height = 0;
1385
1386 if (!output)
1387 return 0;
1388
Tiago Vignattibe143262012-04-16 17:31:41 +03001389 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +08001390 if (priv->output == output) {
1391 panel_height = priv->surface->geometry.height;
1392 break;
1393 }
1394 }
1395 return panel_height;
1396}
1397
1398static void
1399shell_surface_set_maximized(struct wl_client *client,
1400 struct wl_resource *resource,
1401 struct wl_resource *output_resource )
1402{
1403 struct shell_surface *shsurf = resource->data;
1404 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001405 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +08001406 uint32_t edges = 0, panel_height = 0;
1407
1408 /* get the default output, if the client set it as NULL
1409 check whether the ouput is available */
1410 if (output_resource)
1411 shsurf->output = output_resource->data;
1412 else
1413 shsurf->output = get_default_output(es->compositor);
1414
Tiago Vignattibe143262012-04-16 17:31:41 +03001415 shell = shell_surface_get_shell(shsurf);
1416 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001417 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001418
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001419 shsurf->client->send_configure(shsurf->surface, edges,
1420 es->output->current->width,
1421 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +08001422
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001423 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001424}
1425
Alex Wu21858432012-04-01 20:13:08 +08001426static void
1427black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1428
Alex Wu4539b082012-03-01 12:57:46 +08001429static struct weston_surface *
1430create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +08001431 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +08001432 GLfloat x, GLfloat y, int w, int h)
1433{
1434 struct weston_surface *surface = NULL;
1435
1436 surface = weston_surface_create(ec);
1437 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001438 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08001439 return NULL;
1440 }
1441
Alex Wu21858432012-04-01 20:13:08 +08001442 surface->configure = black_surface_configure;
1443 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +08001444 weston_surface_configure(surface, x, y, w, h);
1445 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1446 return surface;
1447}
1448
1449/* Create black surface and append it to the associated fullscreen surface.
1450 * Handle size dismatch and positioning according to the method. */
1451static void
1452shell_configure_fullscreen(struct shell_surface *shsurf)
1453{
1454 struct weston_output *output = shsurf->fullscreen_output;
1455 struct weston_surface *surface = shsurf->surface;
1456 struct weston_matrix *matrix;
1457 float scale;
1458
1459 center_on_output(surface, output);
1460
1461 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001462 shsurf->fullscreen.black_surface =
1463 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +08001464 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001465 output->x, output->y,
1466 output->current->width,
1467 output->current->height);
1468
1469 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1470 wl_list_insert(&surface->layer_link,
1471 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +08001472 shsurf->fullscreen.black_surface->output = output;
1473
1474 switch (shsurf->fullscreen.type) {
1475 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1476 break;
1477 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1478 matrix = &shsurf->fullscreen.transform.matrix;
1479 weston_matrix_init(matrix);
1480 scale = (float)output->current->width/(float)surface->geometry.width;
1481 weston_matrix_scale(matrix, scale, scale, 1);
1482 wl_list_remove(&shsurf->fullscreen.transform.link);
1483 wl_list_insert(surface->geometry.transformation_list.prev,
1484 &shsurf->fullscreen.transform.link);
1485 weston_surface_set_position(surface, output->x, output->y);
1486 break;
1487 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +08001488 if (shell_surface_is_top_fullscreen(shsurf)) {
1489 struct weston_mode mode = {0,
1490 surface->geometry.width,
1491 surface->geometry.height,
1492 shsurf->fullscreen.framerate};
1493
1494 if (weston_output_switch_mode(output, &mode) == 0) {
1495 weston_surface_configure(shsurf->fullscreen.black_surface,
1496 output->x, output->y,
1497 output->current->width,
1498 output->current->height);
1499 weston_surface_set_position(surface, output->x, output->y);
1500 break;
1501 }
1502 }
Alex Wu4539b082012-03-01 12:57:46 +08001503 break;
1504 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1505 break;
1506 default:
1507 break;
1508 }
1509}
1510
1511/* make the fullscreen and black surface at the top */
1512static void
1513shell_stack_fullscreen(struct shell_surface *shsurf)
1514{
Alex Wubd3354b2012-04-17 17:20:49 +08001515 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +08001516 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +03001517 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001518
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001519 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001520 wl_list_insert(&shell->fullscreen_layer.surface_list,
1521 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +08001522 weston_surface_damage(surface);
1523
1524 if (!shsurf->fullscreen.black_surface)
1525 shsurf->fullscreen.black_surface =
1526 create_black_surface(surface->compositor,
1527 surface,
1528 output->x, output->y,
1529 output->current->width,
1530 output->current->height);
1531
1532 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001533 wl_list_insert(&surface->layer_link,
1534 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001535 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001536}
1537
1538static void
1539shell_map_fullscreen(struct shell_surface *shsurf)
1540{
Alex Wu4539b082012-03-01 12:57:46 +08001541 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001542 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001543}
1544
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001545static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001546shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001547 struct wl_resource *resource,
1548 uint32_t method,
1549 uint32_t framerate,
1550 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001551{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001552 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001553 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001554
1555 if (output_resource)
1556 shsurf->output = output_resource->data;
1557 else
1558 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001559
Alex Wu4539b082012-03-01 12:57:46 +08001560 shsurf->fullscreen_output = shsurf->output;
1561 shsurf->fullscreen.type = method;
1562 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001563 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001564
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001565 shsurf->client->send_configure(shsurf->surface, 0,
1566 shsurf->output->current->width,
1567 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001568}
1569
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001570static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001571popup_grab_focus(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001572 struct wl_surface *surface,
1573 wl_fixed_t x,
1574 wl_fixed_t y)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001575{
Daniel Stone37816df2012-05-16 18:45:18 +01001576 struct wl_pointer *pointer = grab->pointer;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001577 struct shell_surface *priv =
1578 container_of(grab, struct shell_surface, popup.grab);
1579 struct wl_client *client = priv->surface->surface.resource.client;
1580
Pekka Paalanencb108432012-01-19 16:25:40 +02001581 if (surface && surface->resource.client == client) {
Daniel Stone37816df2012-05-16 18:45:18 +01001582 wl_pointer_set_focus(pointer, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001583 grab->focus = surface;
1584 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001585 wl_pointer_set_focus(pointer, NULL,
1586 wl_fixed_from_int(0),
1587 wl_fixed_from_int(0));
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001588 grab->focus = NULL;
1589 }
1590}
1591
1592static void
Scott Moreau447013d2012-02-18 05:05:29 -07001593popup_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01001594 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001595{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001596 struct wl_resource *resource;
1597
Daniel Stone37816df2012-05-16 18:45:18 +01001598 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001599 if (resource)
Daniel Stone37816df2012-05-16 18:45:18 +01001600 wl_pointer_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001601}
1602
1603static void
Scott Moreau447013d2012-02-18 05:05:29 -07001604popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001605 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001606{
1607 struct wl_resource *resource;
1608 struct shell_surface *shsurf =
1609 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001610 struct wl_display *display;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001611 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001612 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001613
Daniel Stone37816df2012-05-16 18:45:18 +01001614 resource = grab->pointer->focus_resource;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001615 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001616 display = wl_client_get_display(resource->client);
1617 serial = wl_display_get_serial(display);
Daniel Stone37816df2012-05-16 18:45:18 +01001618 wl_pointer_send_button(resource, serial, time, button, state);
Daniel Stone4dbadb12012-05-30 16:31:51 +01001619 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001620 (shsurf->popup.initial_up ||
Daniel Stone37816df2012-05-16 18:45:18 +01001621 time - shsurf->popup.seat->pointer->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001622 wl_shell_surface_send_popup_done(&shsurf->resource);
Daniel Stone37816df2012-05-16 18:45:18 +01001623 wl_pointer_end_grab(grab->pointer);
1624 shsurf->popup.grab.pointer = NULL;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001625 }
1626
Daniel Stone4dbadb12012-05-30 16:31:51 +01001627 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001628 shsurf->popup.initial_up = 1;
1629}
1630
Scott Moreau447013d2012-02-18 05:05:29 -07001631static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001632 popup_grab_focus,
1633 popup_grab_motion,
1634 popup_grab_button,
1635};
1636
1637static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001638shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001639{
Daniel Stone37816df2012-05-16 18:45:18 +01001640 struct wl_seat *seat = shsurf->popup.seat;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001641 struct weston_surface *es = shsurf->surface;
1642 struct weston_surface *parent = shsurf->parent->surface;
1643
1644 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001645 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001646
Pekka Paalanen938269a2012-02-07 14:19:01 +02001647 weston_surface_update_transform(parent);
1648 if (parent->transform.enabled) {
1649 shsurf->popup.parent_transform.matrix =
1650 parent->transform.matrix;
1651 } else {
1652 /* construct x, y translation matrix */
1653 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1654 shsurf->popup.parent_transform.matrix.d[12] =
1655 parent->geometry.x;
1656 shsurf->popup.parent_transform.matrix.d[13] =
1657 parent->geometry.y;
1658 }
1659 wl_list_insert(es->geometry.transformation_list.prev,
1660 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001661 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001662
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001663 shsurf->popup.initial_up = 0;
1664
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001665 /* We don't require the grab to still be active, but if another
1666 * grab has started in the meantime, we end the popup now. */
Daniel Stone37816df2012-05-16 18:45:18 +01001667 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1668 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001669 } else {
1670 wl_shell_surface_send_popup_done(&shsurf->resource);
1671 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001672}
1673
1674static void
1675shell_surface_set_popup(struct wl_client *client,
1676 struct wl_resource *resource,
Daniel Stone37816df2012-05-16 18:45:18 +01001677 struct wl_resource *seat_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001678 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001679 struct wl_resource *parent_resource,
1680 int32_t x, int32_t y, uint32_t flags)
1681{
1682 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001683
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001684 shsurf->type = SHELL_SURFACE_POPUP;
1685 shsurf->parent = parent_resource->data;
Daniel Stone37816df2012-05-16 18:45:18 +01001686 shsurf->popup.seat = seat_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001687 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001688 shsurf->popup.x = x;
1689 shsurf->popup.y = y;
1690}
1691
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001692static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001693 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001694 shell_surface_move,
1695 shell_surface_resize,
1696 shell_surface_set_toplevel,
1697 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001698 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001699 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001700 shell_surface_set_maximized,
1701 shell_surface_set_title,
1702 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001703};
1704
1705static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001706destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001707{
Daniel Stone37816df2012-05-16 18:45:18 +01001708 if (shsurf->popup.grab.pointer)
1709 wl_pointer_end_grab(shsurf->popup.grab.pointer);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001710
Alex Wubd3354b2012-04-17 17:20:49 +08001711 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1712 shell_surface_is_top_fullscreen(shsurf)) {
1713 weston_output_switch_mode(shsurf->fullscreen_output,
1714 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001715 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001716
Alex Wuaa08e2d2012-03-05 11:01:40 +08001717 if (shsurf->fullscreen.black_surface)
1718 weston_surface_destroy(shsurf->fullscreen.black_surface);
1719
Alex Wubd3354b2012-04-17 17:20:49 +08001720 /* As destroy_resource() use wl_list_for_each_safe(),
1721 * we can always remove the listener.
1722 */
1723 wl_list_remove(&shsurf->surface_destroy_listener.link);
1724 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001725 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001726
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001727 wl_list_remove(&shsurf->link);
1728 free(shsurf);
1729}
1730
1731static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001732shell_destroy_shell_surface(struct wl_resource *resource)
1733{
1734 struct shell_surface *shsurf = resource->data;
1735
1736 destroy_shell_surface(shsurf);
1737}
1738
1739static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001740shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001741{
1742 struct shell_surface *shsurf = container_of(listener,
1743 struct shell_surface,
1744 surface_destroy_listener);
1745
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001746 if (shsurf->resource.client) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001747 wl_resource_destroy(&shsurf->resource);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001748 } else {
1749 wl_signal_emit(&shsurf->resource.destroy_signal,
1750 &shsurf->resource);
Tiago Vignattibc052c92012-04-19 16:18:18 +03001751 destroy_shell_surface(shsurf);
Kristian Høgsberg633b1452012-06-07 18:08:47 -04001752 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001753}
1754
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001755static void
1756shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1757
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001758static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001759get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001760{
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001761 if (surface->configure == shell_surface_configure)
1762 return surface->private;
1763 else
1764 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001765}
1766
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001767static struct shell_surface *
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001768create_shell_surface(void *shell, struct weston_surface *surface,
1769 const struct weston_shell_client *client)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001770{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001771 struct shell_surface *shsurf;
1772
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001773 if (surface->configure) {
Martin Minarik6d118362012-06-07 18:01:59 +02001774 weston_log("surface->configure already set\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001775 return NULL;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001776 }
1777
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001778 shsurf = calloc(1, sizeof *shsurf);
1779 if (!shsurf) {
Martin Minarik6d118362012-06-07 18:01:59 +02001780 weston_log("no memory to allocate shell surface\n");
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001781 return NULL;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001782 }
1783
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001784 surface->configure = shell_surface_configure;
Kristian Høgsbergd8134452012-06-21 12:49:02 -04001785 surface->private = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001786 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001787
Tiago Vignattibc052c92012-04-19 16:18:18 +03001788 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001789 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08001790 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001791 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001792 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001793 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1794 shsurf->fullscreen.framerate = 0;
1795 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001796 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001797 wl_list_init(&shsurf->fullscreen.transform.link);
1798
Tiago Vignattibc052c92012-04-19 16:18:18 +03001799 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001800 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1801 wl_signal_add(&surface->surface.resource.destroy_signal,
1802 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001803
1804 /* init link so its safe to always remove it in destroy_shell_surface */
1805 wl_list_init(&shsurf->link);
1806
Pekka Paalanen460099f2012-01-20 16:48:25 +02001807 /* empty when not in use */
1808 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001809 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001810
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001811 wl_list_init(&shsurf->workspace_transform.link);
1812
Pekka Paalanen98262232011-12-01 10:42:22 +02001813 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001814 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001815
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001816 shsurf->client = client;
1817
Kristian Høgsberg45ba8692012-05-21 14:27:33 -04001818 return shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001819}
1820
1821static void
1822shell_get_shell_surface(struct wl_client *client,
1823 struct wl_resource *resource,
1824 uint32_t id,
1825 struct wl_resource *surface_resource)
1826{
1827 struct weston_surface *surface = surface_resource->data;
1828 struct desktop_shell *shell = resource->data;
1829 struct shell_surface *shsurf;
1830
1831 if (get_shell_surface(surface)) {
1832 wl_resource_post_error(surface_resource,
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001833 WL_DISPLAY_ERROR_INVALID_OBJECT,
1834 "desktop_shell::get_shell_surface already requested");
Tiago Vignattibc052c92012-04-19 16:18:18 +03001835 return;
1836 }
1837
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001838 shsurf = create_shell_surface(shell, surface, &shell_client);
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001839 if (!shsurf) {
1840 wl_resource_post_error(surface_resource,
1841 WL_DISPLAY_ERROR_INVALID_OBJECT,
1842 "surface->configure already set");
1843 return;
1844 }
Tiago Vignattibc052c92012-04-19 16:18:18 +03001845
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001846 shsurf->resource.destroy = shell_destroy_shell_surface;
1847 shsurf->resource.object.id = id;
1848 shsurf->resource.object.interface = &wl_shell_surface_interface;
1849 shsurf->resource.object.implementation =
1850 (void (**)(void)) &shell_surface_implementation;
1851 shsurf->resource.data = shsurf;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001852
Kristian Høgsberg9540ea62012-05-21 14:28:57 -04001853 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001854}
1855
1856static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001857 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001858};
1859
Kristian Høgsberg07937562011-04-12 17:25:42 -04001860static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001861handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001862{
1863 proc->pid = 0;
1864}
1865
1866static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001867launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001868{
1869 if (shell->screensaver.binding)
1870 return;
1871
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001872 if (!shell->screensaver.path)
1873 return;
1874
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001875 if (shell->screensaver.process.pid != 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001876 weston_log("old screensaver still running\n");
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001877 return;
1878 }
1879
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001880 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001881 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001882 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001883 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001884}
1885
1886static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001887terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001888{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001889 if (shell->screensaver.process.pid == 0)
1890 return;
1891
1892 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001893}
1894
1895static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001896show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001897{
1898 struct wl_list *list;
1899
1900 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001901 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001902 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001903 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001904
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001905 wl_list_remove(&surface->surface->layer_link);
1906 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001907 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001908 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001909}
1910
1911static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001912hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001913{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001914 wl_list_remove(&surface->surface->layer_link);
1915 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001916 surface->surface->output = NULL;
1917}
1918
1919static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02001920show_input_panel(struct desktop_shell *shell, struct shell_surface *surface)
1921{
Kristian Høgsberg1ce6a2a2012-06-21 22:34:39 -04001922 if (weston_surface_is_mapped(surface->surface))
1923 return;
1924
Jan Arne Petersen42feced2012-06-21 21:52:17 +02001925 wl_list_remove(&surface->surface->layer_link);
1926 wl_list_insert(&shell->panel_layer.surface_list, &surface->surface->layer_link);
1927 surface->surface->output = surface->output;
1928 weston_surface_damage(surface->surface);
Kristian Høgsberg414bd422012-06-21 22:07:30 -04001929
Kristian Høgsberg1ce6a2a2012-06-21 22:34:39 -04001930 weston_slide_run(surface->surface,
1931 surface->surface->geometry.height, 0,
1932 NULL, NULL);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02001933}
1934
1935static void
1936hide_input_panel(struct desktop_shell *shell, struct shell_surface *surface)
1937{
1938 weston_surface_damage_below(surface->surface);
1939 wl_list_remove(&surface->surface->layer_link);
1940 wl_list_init(&surface->surface->layer_link);
1941 surface->surface->output = NULL;
1942
1943 weston_compositor_schedule_repaint(surface->surface->compositor);
1944}
1945
1946static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001947desktop_shell_set_background(struct wl_client *client,
1948 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001949 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001950 struct wl_resource *surface_resource)
1951{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001952 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001953
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001954 shsurf->next_type = SHELL_SURFACE_BACKGROUND;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001955 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001956
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001957 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001958 surface_resource,
1959 shsurf->output->current->width,
1960 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001961}
1962
1963static void
1964desktop_shell_set_panel(struct wl_client *client,
1965 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001966 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001967 struct wl_resource *surface_resource)
1968{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001969 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001970
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001971 shsurf->next_type = SHELL_SURFACE_PANEL;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001972 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001973
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001974 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001975 surface_resource,
1976 shsurf->output->current->width,
1977 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001978}
1979
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001980static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001981handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001982{
Tiago Vignattibe143262012-04-16 17:31:41 +03001983 struct desktop_shell *shell =
1984 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001985
Martin Minarik6d118362012-06-07 18:01:59 +02001986 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001987 shell->lock_surface = NULL;
1988}
1989
1990static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001991desktop_shell_set_lock_surface(struct wl_client *client,
1992 struct wl_resource *resource,
1993 struct wl_resource *surface_resource)
1994{
Tiago Vignattibe143262012-04-16 17:31:41 +03001995 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001996 struct shell_surface *surface = surface_resource->data;
1997
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001998 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001999
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002000 if (!shell->locked)
2001 return;
2002
Pekka Paalanen98262232011-12-01 10:42:22 +02002003 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002004
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002005 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2006 wl_signal_add(&surface_resource->destroy_signal,
2007 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002008
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002009 shell->lock_surface->next_type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002010}
2011
2012static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002013resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002014{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002015 struct shell_surface *tmp;
2016
2017 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
2018 hide_screensaver(shell, tmp);
2019
2020 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002021
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002022 wl_list_remove(&shell->lock_layer.link);
2023 wl_list_insert(&shell->compositor->cursor_layer.link,
2024 &shell->fullscreen_layer.link);
2025 wl_list_insert(&shell->fullscreen_layer.link,
2026 &shell->panel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002027
Jonas Ådahl04769742012-06-13 00:01:24 +02002028 pop_focus_state(shell, get_current_workspace(shell));
2029
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002030 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02002031 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002032 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02002033 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002034}
2035
2036static void
2037desktop_shell_unlock(struct wl_client *client,
2038 struct wl_resource *resource)
2039{
Tiago Vignattibe143262012-04-16 17:31:41 +03002040 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002041
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002042 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002043
2044 if (shell->locked)
2045 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002046}
2047
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002048static void
2049desktop_shell_set_busy_surface(struct wl_client *client,
2050 struct wl_resource *resource,
2051 struct wl_resource *surface_resource)
2052{
2053 struct desktop_shell *shell = resource->data;
2054
2055 shell->busy_surface = surface_resource->data;
2056}
2057
Kristian Høgsberg75840622011-09-06 13:48:16 -04002058static const struct desktop_shell_interface desktop_shell_implementation = {
2059 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002060 desktop_shell_set_panel,
2061 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04002062 desktop_shell_unlock,
2063 desktop_shell_set_busy_surface
Kristian Høgsberg75840622011-09-06 13:48:16 -04002064};
2065
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002066static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002067get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002068{
2069 struct shell_surface *shsurf;
2070
2071 shsurf = get_shell_surface(surface);
2072 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02002073 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002074 return shsurf->type;
2075}
2076
Kristian Høgsberg75840622011-09-06 13:48:16 -04002077static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002078move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002079{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002080 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002081 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002082 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002083
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002084 if (surface == NULL)
2085 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002086
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002087 shsurf = get_shell_surface(surface);
2088 if (shsurf == NULL)
2089 return;
2090
2091 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002092 case SHELL_SURFACE_PANEL:
2093 case SHELL_SURFACE_BACKGROUND:
2094 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02002095 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberga4a4de42012-06-21 16:46:14 -04002096 case SHELL_SURFACE_INPUT_PANEL:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002097 return;
2098 default:
2099 break;
2100 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04002101
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04002102 surface_move(shsurf, (struct weston_seat *) seat);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002103}
2104
2105static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002106resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002107{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002108 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002109 (struct weston_surface *) seat->pointer->focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002110 uint32_t edges = 0;
2111 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002112 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002113
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002114 if (surface == NULL)
2115 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002116
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002117 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002118 if (!shsurf)
2119 return;
2120
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002121 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002122 case SHELL_SURFACE_PANEL:
2123 case SHELL_SURFACE_BACKGROUND:
2124 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02002125 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberga4a4de42012-06-21 16:46:14 -04002126 case SHELL_SURFACE_INPUT_PANEL:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002127 return;
2128 default:
2129 break;
2130 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04002131
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02002132 weston_surface_from_global(surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002133 wl_fixed_to_int(seat->pointer->grab_x),
2134 wl_fixed_to_int(seat->pointer->grab_y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002135 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002136
Pekka Paalanen60921e52012-01-25 15:55:43 +02002137 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002138 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002139 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002140 edges |= 0;
2141 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002142 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002143
Pekka Paalanen60921e52012-01-25 15:55:43 +02002144 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002145 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002146 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04002147 edges |= 0;
2148 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002149 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002150
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04002151 surface_resize(shsurf, (struct weston_seat *) seat, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002152}
2153
2154static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002155surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002156 wl_fixed_t value, void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002157{
Scott Moreau02709af2012-05-22 01:54:10 -06002158 float step = 0.05;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002159 struct shell_surface *shsurf;
2160 struct weston_surface *surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002161 (struct weston_surface *) seat->pointer->focus;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002162
2163 if (surface == NULL)
2164 return;
2165
2166 shsurf = get_shell_surface(surface);
2167 if (!shsurf)
2168 return;
2169
2170 switch (shsurf->type) {
2171 case SHELL_SURFACE_BACKGROUND:
2172 case SHELL_SURFACE_SCREENSAVER:
2173 return;
2174 default:
2175 break;
2176 }
2177
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002178 surface->alpha += wl_fixed_to_double(value) * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002179
Scott Moreau02709af2012-05-22 01:54:10 -06002180 if (surface->alpha > 1.0)
2181 surface->alpha = 1.0;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002182 if (surface->alpha < step)
2183 surface->alpha = step;
2184
2185 surface->geometry.dirty = 1;
2186 weston_surface_damage(surface);
2187}
2188
2189static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002190do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002191 wl_fixed_t value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07002192{
Daniel Stone37816df2012-05-16 18:45:18 +01002193 struct weston_seat *ws = (struct weston_seat *) seat;
2194 struct weston_compositor *compositor = ws->compositor;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002195 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06002196 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002197
2198 wl_list_for_each(output, &compositor->output_list, link) {
2199 if (pixman_region32_contains_point(&output->region,
Daniel Stone37816df2012-05-16 18:45:18 +01002200 wl_fixed_to_double(seat->pointer->x),
2201 wl_fixed_to_double(seat->pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01002202 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01002203 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002204 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01002205 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002206 increment = -output->zoom.increment;
2207 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002208 increment = output->zoom.increment *
2209 wl_fixed_to_double(value);
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002210 else
2211 increment = 0;
2212
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04002213 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07002214
Scott Moreaue6603982012-06-11 13:07:51 -06002215 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06002216 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06002217 else if (output->zoom.level > output->zoom.max_level)
2218 output->zoom.level = output->zoom.max_level;
2219 else
2220 output->zoom.active = 1;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002221
Scott Moreaue6603982012-06-11 13:07:51 -06002222 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002223
Scott Moreau8dacaab2012-06-17 18:10:58 -06002224 weston_output_update_zoom(output, output->zoom.type);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002225 }
2226 }
2227}
2228
Scott Moreauccbf29d2012-02-22 14:21:41 -07002229static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002230zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01002231 wl_fixed_t value, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01002232{
2233 do_zoom(seat, time, 0, axis, value);
2234}
2235
2236static void
2237zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2238 void *data)
2239{
2240 do_zoom(seat, time, key, 0, 0);
2241}
2242
2243static void
2244terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2245 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002246{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002247 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002248
Daniel Stone325fc2d2012-05-30 16:31:58 +01002249 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002250}
2251
2252static void
Scott Moreau447013d2012-02-18 05:05:29 -07002253rotate_grab_motion(struct wl_pointer_grab *grab,
Daniel Stone103db7f2012-05-08 17:17:55 +01002254 uint32_t time, wl_fixed_t x, wl_fixed_t y)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002255{
2256 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002257 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002258 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002259 struct shell_surface *shsurf = rotate->base.shsurf;
2260 struct weston_surface *surface;
2261 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2262
2263 if (!shsurf)
2264 return;
2265
2266 surface = shsurf->surface;
2267
2268 cx = 0.5f * surface->geometry.width;
2269 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002270
Daniel Stone37816df2012-05-16 18:45:18 +01002271 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2272 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002273 r = sqrtf(dx * dx + dy * dy);
2274
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002275 wl_list_remove(&shsurf->rotation.transform.link);
2276 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002277
2278 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002279 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002280 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002281
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002282 weston_matrix_init(&rotate->rotation);
2283 rotate->rotation.d[0] = dx / r;
2284 rotate->rotation.d[4] = -dy / r;
2285 rotate->rotation.d[1] = -rotate->rotation.d[4];
2286 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02002287
2288 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002289 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002290 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002291 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02002292 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002293
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02002294 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002295 &shsurf->surface->geometry.transformation_list,
2296 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002297 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002298 wl_list_init(&shsurf->rotation.transform.link);
2299 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002300 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002301 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002302
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002303 /* We need to adjust the position of the surface
2304 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002305 cposx = surface->geometry.x + cx;
2306 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002307 dposx = rotate->center.x - cposx;
2308 dposy = rotate->center.y - cposy;
2309 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002310 weston_surface_set_position(surface,
2311 surface->geometry.x + dposx,
2312 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002313 }
2314
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02002315 /* Repaint implies weston_surface_update_transform(), which
2316 * lazily applies the damage due to rotation update.
2317 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002318 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002319}
2320
2321static void
Scott Moreau447013d2012-02-18 05:05:29 -07002322rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002323 uint32_t time, uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002324{
2325 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002326 container_of(grab, struct rotate_grab, base.grab);
Daniel Stone37816df2012-05-16 18:45:18 +01002327 struct wl_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002328 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002329 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002330
Daniel Stone4dbadb12012-05-30 16:31:51 +01002331 if (pointer->button_count == 0 &&
2332 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002333 if (shsurf)
2334 weston_matrix_multiply(&shsurf->rotation.rotation,
2335 &rotate->rotation);
2336 shell_grab_finish(&rotate->base);
Daniel Stone37816df2012-05-16 18:45:18 +01002337 wl_pointer_end_grab(pointer);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002338 free(rotate);
2339 }
2340}
2341
Scott Moreau447013d2012-02-18 05:05:29 -07002342static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02002343 noop_grab_focus,
2344 rotate_grab_motion,
2345 rotate_grab_button,
2346};
2347
2348static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002349rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stonee5a01202012-05-04 11:21:57 +01002350 void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02002351{
2352 struct weston_surface *base_surface =
Daniel Stone37816df2012-05-16 18:45:18 +01002353 (struct weston_surface *) seat->pointer->focus;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002354 struct shell_surface *surface;
2355 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002356 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002357 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02002358
2359 if (base_surface == NULL)
2360 return;
2361
2362 surface = get_shell_surface(base_surface);
2363 if (!surface)
2364 return;
2365
2366 switch (surface->type) {
2367 case SHELL_SURFACE_PANEL:
2368 case SHELL_SURFACE_BACKGROUND:
2369 case SHELL_SURFACE_FULLSCREEN:
2370 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberga4a4de42012-06-21 16:46:14 -04002371 case SHELL_SURFACE_INPUT_PANEL:
Pekka Paalanen460099f2012-01-20 16:48:25 +02002372 return;
2373 default:
2374 break;
2375 }
2376
Pekka Paalanen460099f2012-01-20 16:48:25 +02002377 rotate = malloc(sizeof *rotate);
2378 if (!rotate)
2379 return;
2380
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03002381 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02002382
Kristian Høgsbergb2af93e2012-06-07 20:10:23 -04002383 weston_surface_to_global_float(surface->surface,
2384 surface->surface->geometry.width / 2,
2385 surface->surface->geometry.height / 2,
2386 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002387
Daniel Stone37816df2012-05-16 18:45:18 +01002388 wl_pointer_start_grab(seat->pointer, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002389
Daniel Stone37816df2012-05-16 18:45:18 +01002390 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2391 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002392 r = sqrtf(dx * dx + dy * dy);
2393 if (r > 20.0f) {
2394 struct weston_matrix inverse;
2395
2396 weston_matrix_init(&inverse);
2397 inverse.d[0] = dx / r;
2398 inverse.d[4] = dy / r;
2399 inverse.d[1] = -inverse.d[4];
2400 inverse.d[5] = inverse.d[0];
2401 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01002402
2403 weston_matrix_init(&rotate->rotation);
2404 rotate->rotation.d[0] = dx / r;
2405 rotate->rotation.d[4] = -dy / r;
2406 rotate->rotation.d[1] = -rotate->rotation.d[4];
2407 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002408 } else {
2409 weston_matrix_init(&surface->rotation.rotation);
2410 weston_matrix_init(&rotate->rotation);
2411 }
2412
Daniel Stone37816df2012-05-16 18:45:18 +01002413 wl_pointer_set_focus(seat->pointer, NULL,
2414 wl_fixed_from_int(0),
2415 wl_fixed_from_int(0));
Pekka Paalanen460099f2012-01-20 16:48:25 +02002416}
2417
2418static void
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002419lower_fullscreen_layer(struct desktop_shell *shell)
2420{
2421 struct workspace *ws;
2422 struct weston_surface *surface, *prev;
2423
2424 ws = get_current_workspace(shell);
2425 wl_list_for_each_reverse_safe(surface, prev,
2426 &shell->fullscreen_layer.surface_list,
2427 layer_link)
2428 weston_surface_restack(surface, &ws->layer.surface_list);
2429}
2430
2431static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002432activate(struct desktop_shell *shell, struct weston_surface *es,
Daniel Stone37816df2012-05-16 18:45:18 +01002433 struct weston_seat *seat)
Kristian Høgsberg75840622011-09-06 13:48:16 -04002434{
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002435 struct workspace *ws;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002436
Daniel Stone37816df2012-05-16 18:45:18 +01002437 weston_surface_activate(es, seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002438
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002439 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002440 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002441 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002442 case SHELL_SURFACE_LOCK:
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002443 case SHELL_SURFACE_INPUT_PANEL:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002444 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002445
Pekka Paalanen77346a62011-11-30 16:26:35 +02002446 case SHELL_SURFACE_SCREENSAVER:
2447 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002448 if (shell->lock_surface)
2449 weston_surface_restack(es,
2450 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002451 break;
Alex Wu4539b082012-03-01 12:57:46 +08002452 case SHELL_SURFACE_FULLSCREEN:
2453 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08002454 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08002455 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08002456 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002457 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002458 ws = get_current_workspace(shell);
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04002459 lower_fullscreen_layer(shell);
2460 weston_surface_restack(es, &ws->layer.surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002461 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002462 }
2463}
2464
Alex Wu21858432012-04-01 20:13:08 +08002465/* no-op func for checking black surface */
2466static void
2467black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2468{
2469}
2470
2471static bool
2472is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2473{
2474 if (es->configure == black_surface_configure) {
2475 if (fs_surface)
2476 *fs_surface = (struct weston_surface *)es->private;
2477 return true;
2478 }
2479 return false;
2480}
2481
Kristian Høgsberg75840622011-09-06 13:48:16 -04002482static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01002483click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002484 void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002485{
Daniel Stone37816df2012-05-16 18:45:18 +01002486 struct weston_seat *ws = (struct weston_seat *) seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03002487 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002488 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08002489 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002490
Daniel Stone37816df2012-05-16 18:45:18 +01002491 focus = (struct weston_surface *) seat->pointer->focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08002492 if (!focus)
2493 return;
2494
Alex Wu21858432012-04-01 20:13:08 +08002495 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08002496 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08002497
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04002498 switch (get_shell_surface_type(focus)) {
2499 case SHELL_SURFACE_BACKGROUND:
2500 case SHELL_SURFACE_SCREENSAVER:
2501 case SHELL_SURFACE_INPUT_PANEL:
2502 return;
2503 default:
2504 break;
2505 }
2506
Daniel Stone325fc2d2012-05-30 16:31:58 +01002507 if (seat->pointer->grab == &seat->pointer->default_grab)
Daniel Stone37816df2012-05-16 18:45:18 +01002508 activate(shell, focus, ws);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05002509}
2510
2511static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002512lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002513{
Tiago Vignattibe143262012-04-16 17:31:41 +03002514 struct desktop_shell *shell =
2515 container_of(listener, struct desktop_shell, lock_listener);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002516 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002517 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002518
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002519 if (shell->locked) {
2520 wl_list_for_each(output, &shell->compositor->output_list, link)
2521 /* TODO: find a way to jump to other DPMS levels */
2522 if (output->set_dpms)
2523 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002524 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002525 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002526
2527 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002528
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002529 /* Hide all surfaces by removing the fullscreen, panel and
2530 * toplevel layers. This way nothing else can show or receive
2531 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002532
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002533 wl_list_remove(&shell->panel_layer.link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002534 wl_list_remove(&shell->fullscreen_layer.link);
2535 wl_list_insert(&shell->compositor->cursor_layer.link,
2536 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002537
Pekka Paalanen77346a62011-11-30 16:26:35 +02002538 launch_screensaver(shell);
2539
2540 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
2541 show_screensaver(shell, shsurf);
2542
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002543 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02002544 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002545 weston_compositor_wake(shell->compositor);
2546 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002547 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002548
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002549 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002550 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002551
Jonas Ådahl04769742012-06-13 00:01:24 +02002552 /* stash keyboard foci in current workspace */
2553 push_focus_state(shell, get_current_workspace(shell));
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002554
2555 /* TODO: disable bindings that should not work while locked. */
2556
2557 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002558}
2559
2560static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002561unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002562{
Tiago Vignattibe143262012-04-16 17:31:41 +03002563 struct desktop_shell *shell =
2564 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002565
Pekka Paalanend81c2162011-11-16 13:47:34 +02002566 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002567 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002568 return;
2569 }
2570
2571 /* If desktop-shell client has gone away, unlock immediately. */
2572 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002573 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002574 return;
2575 }
2576
2577 if (shell->prepare_event_sent)
2578 return;
2579
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002580 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002581 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002582}
2583
2584static void
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002585show_input_panels(struct wl_listener *listener, void *data)
2586{
2587 struct desktop_shell *shell =
2588 container_of(listener, struct desktop_shell, show_input_panel_listener);
2589 struct shell_surface *priv;
2590
2591 wl_list_for_each(priv, &shell->input_panel.surfaces, link) {
2592 show_input_panel(shell, priv);
2593 }
2594}
2595
2596static void
2597hide_input_panels(struct wl_listener *listener, void *data)
2598{
2599 struct desktop_shell *shell =
2600 container_of(listener, struct desktop_shell, hide_input_panel_listener);
2601 struct shell_surface *priv;
2602
2603 wl_list_for_each(priv, &shell->input_panel.surfaces, link) {
2604 hide_input_panel(shell, priv);
2605 }
2606}
2607
2608static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002609center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02002610{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002611 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002612 GLfloat x = (mode->width - surface->geometry.width) / 2;
2613 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002614
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002615 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002616}
2617
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002618
2619static void
2620bottom_center_on_output(struct weston_surface *surface, struct weston_output *output)
2621{
2622 struct weston_mode *mode = output->current;
2623 GLfloat x = (mode->width - surface->geometry.width) / 2;
2624 GLfloat y = mode->height - surface->geometry.height;
2625
2626 weston_surface_set_position(surface, output->x + x, output->y + y);
2627}
2628
Pekka Paalanen77346a62011-11-30 16:26:35 +02002629static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002630map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002631 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002632{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002633 struct weston_compositor *compositor = shell->compositor;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002634 struct shell_surface *shsurf = get_shell_surface(surface);
2635 enum shell_surface_type surface_type = shsurf->type;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002636 struct weston_surface *parent;
Daniel Stoneb2104682012-05-30 16:31:56 +01002637 struct weston_seat *seat;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002638 struct workspace *ws;
Juan Zhao96879df2012-02-07 08:45:41 +08002639 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002640
Pekka Paalanen60921e52012-01-25 15:55:43 +02002641 surface->geometry.width = width;
2642 surface->geometry.height = height;
2643 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002644
2645 /* initial positioning, see also configure() */
2646 switch (surface_type) {
2647 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02002648 weston_surface_set_position(surface, 10 + random() % 400,
2649 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002650 break;
2651 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002652 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002653 break;
Alex Wu4539b082012-03-01 12:57:46 +08002654 case SHELL_SURFACE_FULLSCREEN:
2655 shell_map_fullscreen(shsurf);
2656 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002657 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002658 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002659 panel_height = get_output_panel_height(shell,surface->output);
2660 weston_surface_set_position(surface, surface->output->x,
2661 surface->output->y + panel_height);
2662 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002663 case SHELL_SURFACE_LOCK:
2664 center_on_output(surface, get_default_output(compositor));
2665 break;
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002666 case SHELL_SURFACE_INPUT_PANEL:
2667 bottom_center_on_output(surface, get_default_output(compositor));
Kristian Høgsberg1ce6a2a2012-06-21 22:34:39 -04002668 /* Don't map the input panel here, wait for
2669 * show_input_panels signal. */
2670 return;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002671 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002672 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002673 case SHELL_SURFACE_NONE:
2674 weston_surface_set_position(surface,
2675 surface->geometry.x + sx,
2676 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002677 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002678 default:
2679 ;
2680 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002681
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002682 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002683 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002684 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002685 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002686 wl_list_insert(&shell->background_layer.surface_list,
2687 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002688 break;
2689 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002690 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002691 wl_list_insert(&shell->panel_layer.surface_list,
2692 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002693 break;
2694 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002695 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002696 wl_list_insert(&shell->lock_layer.surface_list,
2697 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002698 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002699 break;
2700 case SHELL_SURFACE_SCREENSAVER:
2701 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002702 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002703 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002704 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002705 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002706 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002707 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002708 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002709 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002710 case SHELL_SURFACE_POPUP:
2711 case SHELL_SURFACE_TRANSIENT:
2712 parent = shsurf->parent->surface;
2713 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2714 break;
Alex Wu4539b082012-03-01 12:57:46 +08002715 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002716 case SHELL_SURFACE_NONE:
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002717 case SHELL_SURFACE_INPUT_PANEL:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002718 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002719 default:
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002720 ws = get_current_workspace(shell);
2721 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002722 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002723 }
2724
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002725 if (surface_type != SHELL_SURFACE_NONE) {
2726 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002727 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2728 surface->output = shsurf->output;
2729 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002730
Juan Zhao7bb92f02011-12-15 11:31:51 -05002731 switch (surface_type) {
Juan Zhao7bb92f02011-12-15 11:31:51 -05002732 case SHELL_SURFACE_TRANSIENT:
Tiago Vignatti99aeb1e2012-05-23 22:06:26 +03002733 if (shsurf->transient.flags ==
2734 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2735 break;
2736 case SHELL_SURFACE_TOPLEVEL:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002737 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002738 case SHELL_SURFACE_MAXIMIZED:
Daniel Stoneb2104682012-05-30 16:31:56 +01002739 if (!shell->locked) {
2740 wl_list_for_each(seat, &compositor->seat_list, link)
2741 activate(shell, surface, seat);
2742 }
Juan Zhao7bb92f02011-12-15 11:31:51 -05002743 break;
2744 default:
2745 break;
2746 }
2747
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002748 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002749 {
2750 switch (shell->win_animation_type) {
2751 case ANIMATION_FADE:
2752 weston_fade_run(surface, NULL, NULL);
2753 break;
2754 case ANIMATION_ZOOM:
2755 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2756 break;
2757 default:
2758 break;
2759 }
2760 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002761}
2762
2763static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002764configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002765 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002766{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002767 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2768 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002769
Pekka Paalanen77346a62011-11-30 16:26:35 +02002770 shsurf = get_shell_surface(surface);
2771 if (shsurf)
2772 surface_type = shsurf->type;
2773
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002774 surface->geometry.x = x;
2775 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002776 surface->geometry.width = width;
2777 surface->geometry.height = height;
2778 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002779
2780 switch (surface_type) {
2781 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002782 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002783 break;
Alex Wu4539b082012-03-01 12:57:46 +08002784 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002785 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002786 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002787 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002788 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002789 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002790 surface->geometry.x = surface->output->x;
2791 surface->geometry.y = surface->output->y +
2792 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002793 break;
Alex Wu4539b082012-03-01 12:57:46 +08002794 case SHELL_SURFACE_TOPLEVEL:
2795 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002796 default:
2797 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002798 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002799
Alex Wu4539b082012-03-01 12:57:46 +08002800 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002801 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002802 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002803
2804 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2805 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002806 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2807 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002808 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002809}
2810
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002811static void
2812shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2813{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002814 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002815 struct desktop_shell *shell = shsurf->shell;
Tiago Vignatti70e5c9c2012-05-07 15:23:07 +03002816 int type_changed = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002817
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002818 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002819 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002820 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002821 type_changed = 1;
2822 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002823
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002824 if (!weston_surface_is_mapped(es)) {
2825 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002826 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002827 es->geometry.width != es->buffer->width ||
2828 es->geometry.height != es->buffer->height) {
2829 GLfloat from_x, from_y;
2830 GLfloat to_x, to_y;
2831
2832 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2833 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2834 configure(shell, es,
2835 es->geometry.x + to_x - from_x,
2836 es->geometry.y + to_y - from_y,
2837 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002838 }
2839}
2840
Tiago Vignattibe143262012-04-16 17:31:41 +03002841static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002842
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002843static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002844desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002845{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002846 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002847 struct desktop_shell *shell =
2848 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002849
2850 shell->child.process.pid = 0;
2851 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002852
2853 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2854 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002855 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002856 shell->child.deathstamp = time;
2857 shell->child.deathcount = 0;
2858 }
2859
2860 shell->child.deathcount++;
2861 if (shell->child.deathcount > 5) {
Martin Minarik6d118362012-06-07 18:01:59 +02002862 weston_log("weston-desktop-shell died, giving up.\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002863 return;
2864 }
2865
Martin Minarik6d118362012-06-07 18:01:59 +02002866 weston_log("weston-desktop-shell died, respawning...\n");
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002867 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002868}
2869
2870static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002871launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002872{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002873 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002874
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002875 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002876 &shell->child.process,
2877 shell_exe,
2878 desktop_shell_sigchld);
2879
2880 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002881 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002882 return 0;
2883}
2884
2885static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002886bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2887{
Tiago Vignattibe143262012-04-16 17:31:41 +03002888 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002889
2890 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002891 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002892}
2893
Kristian Høgsberg75840622011-09-06 13:48:16 -04002894static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002895unbind_desktop_shell(struct wl_resource *resource)
2896{
Tiago Vignattibe143262012-04-16 17:31:41 +03002897 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002898
2899 if (shell->locked)
2900 resume_desktop(shell);
2901
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002902 shell->child.desktop_shell = NULL;
2903 shell->prepare_event_sent = false;
2904 free(resource);
2905}
2906
2907static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002908bind_desktop_shell(struct wl_client *client,
2909 void *data, uint32_t version, uint32_t id)
2910{
Tiago Vignattibe143262012-04-16 17:31:41 +03002911 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002912 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002913
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002914 resource = wl_client_add_object(client, &desktop_shell_interface,
2915 &desktop_shell_implementation,
2916 id, shell);
2917
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002918 if (client == shell->child.client) {
2919 resource->destroy = unbind_desktop_shell;
2920 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002921 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002922 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002923
2924 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2925 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002926 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002927}
2928
Pekka Paalanen6e168112011-11-24 11:34:05 +02002929static void
2930screensaver_set_surface(struct wl_client *client,
2931 struct wl_resource *resource,
2932 struct wl_resource *shell_surface_resource,
2933 struct wl_resource *output_resource)
2934{
Tiago Vignattibe143262012-04-16 17:31:41 +03002935 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002936 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002937 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002938
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002939 surface->next_type = SHELL_SURFACE_SCREENSAVER;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002940
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002941 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002942 surface->output = output;
2943 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002944}
2945
2946static const struct screensaver_interface screensaver_implementation = {
2947 screensaver_set_surface
2948};
2949
2950static void
2951unbind_screensaver(struct wl_resource *resource)
2952{
Tiago Vignattibe143262012-04-16 17:31:41 +03002953 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002954
Pekka Paalanen77346a62011-11-30 16:26:35 +02002955 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002956 free(resource);
2957}
2958
2959static void
2960bind_screensaver(struct wl_client *client,
2961 void *data, uint32_t version, uint32_t id)
2962{
Tiago Vignattibe143262012-04-16 17:31:41 +03002963 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002964 struct wl_resource *resource;
2965
2966 resource = wl_client_add_object(client, &screensaver_interface,
2967 &screensaver_implementation,
2968 id, shell);
2969
Pekka Paalanen77346a62011-11-30 16:26:35 +02002970 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002971 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002972 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002973 return;
2974 }
2975
2976 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2977 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002978 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002979}
2980
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002981static void
2982input_panel_set_surface(struct wl_client *client,
2983 struct wl_resource *resource,
2984 struct wl_resource *shell_surface_resource,
2985 struct wl_resource *output_resource)
2986{
2987 struct desktop_shell *shell = resource->data;
2988 struct shell_surface *surface = shell_surface_resource->data;
2989 struct weston_output *output = output_resource->data;
2990
2991 surface->next_type = SHELL_SURFACE_INPUT_PANEL;
2992
2993 surface->fullscreen_output = output;
2994 surface->output = output;
2995 wl_list_insert(shell->input_panel.surfaces.prev, &surface->link);
2996}
2997
2998static const struct input_panel_interface input_panel_implementation = {
2999 input_panel_set_surface
3000};
3001
3002static void
3003unbind_input_panel(struct wl_resource *resource)
3004{
3005 struct desktop_shell *shell = resource->data;
3006
3007 shell->input_panel.binding = NULL;
3008 free(resource);
3009}
3010
3011static void
3012bind_input_panel(struct wl_client *client,
3013 void *data, uint32_t version, uint32_t id)
3014{
3015 struct desktop_shell *shell = data;
3016 struct wl_resource *resource;
3017
3018 resource = wl_client_add_object(client, &input_panel_interface,
3019 &input_panel_implementation,
3020 id, shell);
3021
3022 if (shell->input_panel.binding == NULL) {
3023 resource->destroy = unbind_input_panel;
3024 shell->input_panel.binding = resource;
3025 return;
3026 }
3027
3028 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3029 "interface object already bound");
3030 wl_resource_destroy(resource);
3031}
3032
Kristian Høgsberg07045392012-02-19 18:52:44 -05003033struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03003034 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003035 struct weston_surface *current;
3036 struct wl_listener listener;
3037 struct wl_keyboard_grab grab;
3038};
3039
3040static void
3041switcher_next(struct switcher *switcher)
3042{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003043 struct weston_surface *surface;
3044 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003045 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003046 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003047
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003048 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05003049 switch (get_shell_surface_type(surface)) {
3050 case SHELL_SURFACE_TOPLEVEL:
3051 case SHELL_SURFACE_FULLSCREEN:
3052 case SHELL_SURFACE_MAXIMIZED:
3053 if (first == NULL)
3054 first = surface;
3055 if (prev == switcher->current)
3056 next = surface;
3057 prev = surface;
Scott Moreau02709af2012-05-22 01:54:10 -06003058 surface->alpha = 0.25;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05003059 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003060 weston_surface_damage(surface);
3061 break;
3062 default:
3063 break;
3064 }
Alex Wu1659daa2012-04-01 20:13:09 +08003065
3066 if (is_black_surface(surface, NULL)) {
Scott Moreau02709af2012-05-22 01:54:10 -06003067 surface->alpha = 0.25;
Alex Wu1659daa2012-04-01 20:13:09 +08003068 surface->geometry.dirty = 1;
3069 weston_surface_damage(surface);
3070 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05003071 }
3072
3073 if (next == NULL)
3074 next = first;
3075
Alex Wu07b26062012-03-12 16:06:01 +08003076 if (next == NULL)
3077 return;
3078
Kristian Høgsberg07045392012-02-19 18:52:44 -05003079 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003080 wl_signal_add(&next->surface.resource.destroy_signal,
3081 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003082
3083 switcher->current = next;
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003084 next->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08003085
Kristian Høgsberg32e56862012-04-02 22:18:58 -04003086 shsurf = get_shell_surface(switcher->current);
3087 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003088 shsurf->fullscreen.black_surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003089}
3090
3091static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003092switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003093{
3094 struct switcher *switcher =
3095 container_of(listener, struct switcher, listener);
3096
3097 switcher_next(switcher);
3098}
3099
3100static void
Daniel Stone351eb612012-05-31 15:27:47 -04003101switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003102{
Kristian Høgsberg07045392012-02-19 18:52:44 -05003103 struct weston_surface *surface;
Daniel Stone37816df2012-05-16 18:45:18 +01003104 struct wl_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003105 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003106
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003107 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
Kristian Høgsberga416fa12012-05-21 14:06:52 -04003108 surface->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003109 weston_surface_damage(surface);
3110 }
3111
Alex Wu07b26062012-03-12 16:06:01 +08003112 if (switcher->current)
Daniel Stone37816df2012-05-16 18:45:18 +01003113 activate(switcher->shell, switcher->current,
3114 (struct weston_seat *) keyboard->seat);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003115 wl_list_remove(&switcher->listener.link);
Daniel Stone37816df2012-05-16 18:45:18 +01003116 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003117 free(switcher);
3118}
3119
3120static void
3121switcher_key(struct wl_keyboard_grab *grab,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003122 uint32_t time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003123{
3124 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01003125 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04003126
Daniel Stonec9785ea2012-05-30 16:31:52 +01003127 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04003128 switcher_next(switcher);
3129}
3130
3131static void
3132switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3133 uint32_t mods_depressed, uint32_t mods_latched,
3134 uint32_t mods_locked, uint32_t group)
3135{
3136 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stone37816df2012-05-16 18:45:18 +01003137 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003138
Daniel Stone351eb612012-05-31 15:27:47 -04003139 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3140 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04003141}
Kristian Høgsberg07045392012-02-19 18:52:44 -05003142
3143static const struct wl_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04003144 switcher_key,
3145 switcher_modifier,
Kristian Høgsberg07045392012-02-19 18:52:44 -05003146};
3147
3148static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003149switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3150 void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05003151{
Tiago Vignattibe143262012-04-16 17:31:41 +03003152 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003153 struct switcher *switcher;
3154
3155 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003156 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003157 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003158 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05003159 wl_list_init(&switcher->listener.link);
3160
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003161 lower_fullscreen_layer(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003162 switcher->grab.interface = &switcher_grab;
Daniel Stone37816df2012-05-16 18:45:18 +01003163 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3164 wl_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05003165 switcher_next(switcher);
3166}
3167
Pekka Paalanen3c647232011-12-22 13:43:43 +02003168static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003169backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3170 void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003171{
3172 struct weston_compositor *compositor = data;
3173 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003174 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003175
3176 /* TODO: we're limiting to simple use cases, where we assume just
3177 * control on the primary display. We'd have to extend later if we
3178 * ever get support for setting backlights on random desktop LCD
3179 * panels though */
3180 output = get_default_output(compositor);
3181 if (!output)
3182 return;
3183
3184 if (!output->set_backlight)
3185 return;
3186
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003187 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3188 backlight_new = output->backlight_current - 25;
3189 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3190 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003191
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03003192 if (backlight_new < 5)
3193 backlight_new = 5;
3194 if (backlight_new > 255)
3195 backlight_new = 255;
3196
3197 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003198 output->set_backlight(output, output->backlight_current);
3199}
3200
3201static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003202debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3203 void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003204{
Tiago Vignattibe143262012-04-16 17:31:41 +03003205 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003206 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05003207 struct weston_surface *surface;
3208
3209 if (shell->debug_repaint_surface) {
3210 weston_surface_destroy(shell->debug_repaint_surface);
3211 shell->debug_repaint_surface = NULL;
3212 } else {
3213 surface = weston_surface_create(compositor);
3214 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3215 weston_surface_configure(surface, 0, 0, 8192, 8192);
3216 wl_list_insert(&compositor->fade_layer.surface_list,
3217 &surface->layer_link);
3218 weston_surface_assign_output(surface);
3219 pixman_region32_init(&surface->input);
3220
3221 /* Here's the dirty little trick that makes the
3222 * repaint debugging work: we force an
3223 * update_transform first to update dependent state
3224 * and clear the geometry.dirty bit. Then we clear
3225 * the surface damage so it only gets repainted
3226 * piecewise as we repaint other things. */
3227
3228 weston_surface_update_transform(surface);
3229 pixman_region32_fini(&surface->damage);
3230 pixman_region32_init(&surface->damage);
3231 shell->debug_repaint_surface = surface;
3232 }
3233}
3234
3235static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01003236force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3237 void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003238{
3239 struct wl_client *client;
3240 pid_t pid;
3241 uid_t uid;
3242 gid_t gid;
3243
Daniel Stone325fc2d2012-05-30 16:31:58 +01003244 client = seat->keyboard->focus->resource.client;
3245 wl_client_get_credentials(client, &pid, &uid, &gid);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003246
Daniel Stone325fc2d2012-05-30 16:31:58 +01003247 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04003248}
3249
3250static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003251workspace_up_binding(struct wl_seat *seat, uint32_t time,
3252 uint32_t key, void *data)
3253{
3254 struct desktop_shell *shell = data;
3255 unsigned int new_index = shell->workspaces.current;
3256
3257 if (new_index != 0)
3258 new_index--;
3259
3260 change_workspace(shell, new_index);
3261}
3262
3263static void
3264workspace_down_binding(struct wl_seat *seat, uint32_t time,
3265 uint32_t key, void *data)
3266{
3267 struct desktop_shell *shell = data;
3268 unsigned int new_index = shell->workspaces.current;
3269
3270 if (new_index < shell->workspaces.num - 1)
3271 new_index++;
3272
3273 change_workspace(shell, new_index);
3274}
3275
3276static void
3277workspace_f_binding(struct wl_seat *seat, uint32_t time,
3278 uint32_t key, void *data)
3279{
3280 struct desktop_shell *shell = data;
3281 unsigned int new_index;
3282
3283 new_index = key - KEY_F1;
3284 if (new_index >= shell->workspaces.num)
3285 new_index = shell->workspaces.num - 1;
3286
3287 change_workspace(shell, new_index);
3288}
3289
3290
3291static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003292shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02003293{
Tiago Vignattibe143262012-04-16 17:31:41 +03003294 struct desktop_shell *shell =
3295 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003296 struct workspace **ws;
Pekka Paalanen3c647232011-12-22 13:43:43 +02003297
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02003298 if (shell->child.client)
3299 wl_client_destroy(shell->child.client);
3300
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003301 wl_list_remove(&shell->lock_listener.link);
3302 wl_list_remove(&shell->unlock_listener.link);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003303 wl_list_remove(&shell->show_input_panel_listener.link);
3304 wl_list_remove(&shell->hide_input_panel_listener.link);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04003305
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003306 wl_array_for_each(ws, &shell->workspaces.array)
3307 workspace_destroy(*ws);
3308 wl_array_release(&shell->workspaces.array);
3309
Pekka Paalanen3c647232011-12-22 13:43:43 +02003310 free(shell->screensaver.path);
3311 free(shell);
3312}
3313
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003314static void
3315shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3316{
3317 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003318 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003319
3320 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01003321 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3322 MODIFIER_CTRL | MODIFIER_ALT,
3323 terminate_binding, ec);
3324 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3325 click_to_activate_binding,
3326 shell);
3327 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3328 MODIFIER_SUPER | MODIFIER_ALT,
3329 surface_opacity_binding, NULL);
3330 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3331 MODIFIER_SUPER, zoom_axis_binding,
3332 NULL);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003333
3334 /* configurable bindings */
3335 mod = shell->binding_modifier;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003336 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3337 zoom_key_binding, NULL);
3338 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3339 zoom_key_binding, NULL);
3340 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3341 shell);
3342 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3343 resize_binding, shell);
3344 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3345 rotate_binding, NULL);
3346 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3347 shell);
3348 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3349 ec);
3350 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3351 backlight_binding, ec);
3352 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3353 ec);
3354 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3355 backlight_binding, ec);
3356 weston_compositor_add_key_binding(ec, KEY_SPACE, mod,
3357 debug_repaint_binding, shell);
3358 weston_compositor_add_key_binding(ec, KEY_K, mod,
3359 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003360 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3361 workspace_up_binding, shell);
3362 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3363 workspace_down_binding, shell);
3364
3365 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3366 if (shell->workspaces.num > 1) {
3367 num_workspace_bindings = shell->workspaces.num;
3368 if (num_workspace_bindings > 6)
3369 num_workspace_bindings = 6;
3370 for (i = 0; i < num_workspace_bindings; i++)
3371 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3372 workspace_f_binding,
3373 shell);
3374 }
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003375}
3376
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003377int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003378shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04003379
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003380WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003381shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003382{
Tiago Vignattibe143262012-04-16 17:31:41 +03003383 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003384 struct workspace **pws;
3385 unsigned int i;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003386
3387 shell = malloc(sizeof *shell);
3388 if (shell == NULL)
3389 return -1;
3390
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04003391 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003392 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003393
3394 shell->destroy_listener.notify = shell_destroy;
3395 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3396 shell->lock_listener.notify = lock;
3397 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3398 shell->unlock_listener.notify = unlock;
3399 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003400 shell->show_input_panel_listener.notify = show_input_panels;
3401 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3402 shell->hide_input_panel_listener.notify = hide_input_panels;
3403 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06003404 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03003405 ec->shell_interface.create_shell_surface = create_shell_surface;
3406 ec->shell_interface.set_toplevel = set_toplevel;
Tiago Vignatti491bac12012-05-18 16:37:43 -04003407 ec->shell_interface.set_transient = set_transient;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003408 ec->shell_interface.move = surface_move;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04003409 ec->shell_interface.resize = surface_resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003410
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003411 wl_list_init(&shell->backgrounds);
3412 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003413 wl_list_init(&shell->screensaver.surfaces);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003414 wl_list_init(&shell->input_panel.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003415
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003416 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3417 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003418 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3419 weston_layer_init(&shell->lock_layer, NULL);
3420
3421 wl_array_init(&shell->workspaces.array);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003422
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05003423 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02003424
Jonas Ådahle3cddce2012-06-13 00:01:22 +02003425 for (i = 0; i < shell->workspaces.num; i++) {
3426 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3427 if (pws == NULL)
3428 return -1;
3429
3430 *pws = workspace_create();
3431 if (*pws == NULL)
3432 return -1;
3433 }
3434 activate_workspace(shell, 0);
3435
Jonas Ådahl62fcd042012-06-13 00:01:23 +02003436 wl_list_init(&shell->workspaces.animation.link);
3437 shell->workspaces.animation.frame = animate_workspace_change_frame;
3438
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003439 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3440 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003441 return -1;
3442
Kristian Høgsberg75840622011-09-06 13:48:16 -04003443 if (wl_display_add_global(ec->wl_display,
3444 &desktop_shell_interface,
3445 shell, bind_desktop_shell) == NULL)
3446 return -1;
3447
Pekka Paalanen6e168112011-11-24 11:34:05 +02003448 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3449 shell, bind_screensaver) == NULL)
3450 return -1;
3451
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003452 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3453 shell, bind_input_panel) == NULL)
3454 return -1;
3455
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05003456 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02003457 if (launch_desktop_shell_process(shell) != 0)
3458 return -1;
3459
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003460 shell->pointer_focus_listener.notify = handle_pointer_focus;
Pekka Paalanen43e1ba82012-06-07 15:07:06 +03003461 if (ec->seat->seat.pointer)
3462 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3463 &shell->pointer_focus_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003464
Tiago Vignatti0b52d482012-04-20 18:54:25 +03003465 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003466
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05003467 return 0;
3468}