blob: 738f6c8d57df8e410a2b0279177884a256c534ff [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øgsberg4cca3492011-01-18 07:53:49 -050033
Pekka Paalanen50719bc2011-11-22 14:18:50 +020034#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050035#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040036#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020037#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
Juan Zhaoe10d2792012-04-25 19:09:52 +080039enum animation_type {
40 ANIMATION_NONE,
41
42 ANIMATION_ZOOM,
43 ANIMATION_FADE
44};
45
Tiago Vignattibe143262012-04-16 17:31:41 +030046struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050047 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040048
49 struct wl_listener lock_listener;
50 struct wl_listener unlock_listener;
51 struct wl_listener destroy_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020052
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050053 struct weston_layer fullscreen_layer;
54 struct weston_layer panel_layer;
55 struct weston_layer toplevel_layer;
56 struct weston_layer background_layer;
57 struct weston_layer lock_layer;
58
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020059 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050060 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020061 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020062 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020063
64 unsigned deathcount;
65 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020066 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020067
68 bool locked;
69 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020070
Pekka Paalanen068ae942011-11-28 14:11:15 +020071 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050072 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010073
74 struct wl_list backgrounds;
75 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020076
Pekka Paalanen77346a62011-11-30 16:26:35 +020077 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020078 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020079 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020080 struct wl_resource *binding;
81 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050082 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020083 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050084
Tiago Vignatti0b52d482012-04-20 18:54:25 +030085 uint32_t binding_modifier;
Juan Zhaoe10d2792012-04-25 19:09:52 +080086 enum animation_type win_animation_type;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050087 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040088};
89
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050090enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020091 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050092
Pekka Paalanen57da4a82011-11-23 16:42:16 +020093 SHELL_SURFACE_PANEL,
94 SHELL_SURFACE_BACKGROUND,
95 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020096 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050097
98 SHELL_SURFACE_TOPLEVEL,
99 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500100 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +0800101 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500102 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200103};
104
Scott Moreauff1db4a2012-04-17 19:06:18 -0600105struct ping_timer {
106 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600107 uint32_t serial;
108};
109
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200110struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200111 struct wl_resource resource;
112
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500113 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200114 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500115 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300116 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200117
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400118 enum shell_surface_type type, next_type;
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400119 char *title, *class;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500120 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800121 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800122 bool saved_rotation_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600123 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100124
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500125 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200126 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500127 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200128 } rotation;
129
130 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700131 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500132 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200133 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500134 int32_t initial_up;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400135 struct wl_input_device *device;
136 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500137 } popup;
138
Alex Wu4539b082012-03-01 12:57:46 +0800139 struct {
140 enum wl_shell_surface_fullscreen_method type;
141 struct weston_transform transform; /* matrix from x, y */
142 uint32_t framerate;
143 struct weston_surface *black_surface;
144 } fullscreen;
145
Scott Moreauff1db4a2012-04-17 19:06:18 -0600146 struct ping_timer *ping_timer;
147
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600148 struct {
149 struct weston_animation current;
150 int exists;
151 int fading_in;
152 uint32_t timestamp;
153 } unresponsive_animation;
154
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500155 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500156 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100157 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200158};
159
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300160struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700161 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300162 struct shell_surface *shsurf;
163 struct wl_listener shsurf_destroy_listener;
164};
165
166struct weston_move_grab {
167 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500168 int32_t dx, dy;
169};
170
Pekka Paalanen460099f2012-01-20 16:48:25 +0200171struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300172 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500173 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200174 struct {
175 int32_t x;
176 int32_t y;
177 } center;
178};
179
Alex Wubd3354b2012-04-17 17:20:49 +0800180static struct shell_surface *
181get_shell_surface(struct weston_surface *surface);
182
183static struct desktop_shell *
184shell_surface_get_shell(struct shell_surface *shsurf);
185
186static bool
187shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
188{
189 struct desktop_shell *shell;
190 struct weston_surface *top_fs_es;
191
192 shell = shell_surface_get_shell(shsurf);
193
194 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
195 return false;
196
197 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
198 struct weston_surface,
199 layer_link);
200 return (shsurf == get_shell_surface(top_fs_es));
201}
202
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500203static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400204destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300205{
206 struct shell_grab *grab;
207
208 grab = container_of(listener, struct shell_grab,
209 shsurf_destroy_listener);
210
211 grab->shsurf = NULL;
212}
213
214static void
215shell_grab_init(struct shell_grab *grab,
216 const struct wl_pointer_grab_interface *interface,
217 struct shell_surface *shsurf)
218{
219 grab->grab.interface = interface;
220 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400221 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
222 wl_signal_add(&shsurf->resource.destroy_signal,
223 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300224
225}
226
227static void
228shell_grab_finish(struct shell_grab *grab)
229{
230 wl_list_remove(&grab->shsurf_destroy_listener.link);
231}
232
233static void
Alex Wu4539b082012-03-01 12:57:46 +0800234center_on_output(struct weston_surface *surface,
235 struct weston_output *output);
236
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300237static uint32_t
238get_modifier(char *modifier)
239{
240 if (!modifier)
241 return MODIFIER_SUPER;
242
243 if (!strcmp("ctrl", modifier))
244 return MODIFIER_CTRL;
245 else if (!strcmp("alt", modifier))
246 return MODIFIER_ALT;
247 else if (!strcmp("super", modifier))
248 return MODIFIER_SUPER;
249 else
250 return MODIFIER_SUPER;
251}
252
Juan Zhaoe10d2792012-04-25 19:09:52 +0800253static enum animation_type
254get_animation_type(char *animation)
255{
256 if (!animation)
257 return ANIMATION_NONE;
258
259 if (!strcmp("zoom", animation))
260 return ANIMATION_ZOOM;
261 else if (!strcmp("fade", animation))
262 return ANIMATION_FADE;
263 else
264 return ANIMATION_NONE;
265}
266
Alex Wu4539b082012-03-01 12:57:46 +0800267static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300268shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200269{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200270 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200271 char *path = NULL;
272 int duration = 60;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300273 char *modifier = NULL;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800274 char *win_animation = NULL;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200275
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400276 struct config_key shell_keys[] = {
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300277 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
Juan Zhaoe10d2792012-04-25 19:09:52 +0800278 { "animation", CONFIG_KEY_STRING, &win_animation},
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200279 };
280
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400281 struct config_key saver_keys[] = {
282 { "path", CONFIG_KEY_STRING, &path },
283 { "duration", CONFIG_KEY_INTEGER, &duration },
284 };
285
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200286 struct config_section cs[] = {
Kristian Høgsberga4e8b332012-04-20 16:48:21 -0400287 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200288 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
289 };
290
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200291 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500292 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200293 free(config_file);
294
Pekka Paalanen7296e792011-12-07 16:22:00 +0200295 shell->screensaver.path = path;
296 shell->screensaver.duration = duration;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300297 shell->binding_modifier = get_modifier(modifier);
Juan Zhaoe10d2792012-04-25 19:09:52 +0800298 shell->win_animation_type = get_animation_type(win_animation);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200299}
300
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200301static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400302noop_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500303 struct wl_surface *surface, int32_t x, int32_t y)
304{
305 grab->focus = NULL;
306}
307
308static void
Scott Moreau447013d2012-02-18 05:05:29 -0700309move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500310 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500311{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500312 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500313 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300314 struct shell_surface *shsurf = move->base.shsurf;
315 struct weston_surface *es;
316
317 if (!shsurf)
318 return;
319
320 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500321
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500322 weston_surface_configure(es,
323 device->x + move->dx,
324 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200325 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500326}
327
328static void
Scott Moreau447013d2012-02-18 05:05:29 -0700329move_grab_button(struct wl_pointer_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +0100330 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500331{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300332 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
333 grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500334 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500335
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500336 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300337 shell_grab_finish(shell_grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400338 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500339 free(grab);
340 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500341}
342
Scott Moreau447013d2012-02-18 05:05:29 -0700343static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500344 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500345 move_grab_motion,
346 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500347};
348
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600349static void
350unresponsive_surface_fade(struct shell_surface *shsurf, bool reverse)
351{
352 shsurf->unresponsive_animation.fading_in = reverse ? 0 : 1;
353
354 if(!shsurf->unresponsive_animation.exists) {
355 wl_list_insert(&shsurf->surface->compositor->animation_list,
356 &shsurf->unresponsive_animation.current.link);
357 shsurf->unresponsive_animation.exists = 1;
358 shsurf->unresponsive_animation.timestamp = weston_compositor_get_time();
359 weston_surface_damage(shsurf->surface);
360 }
361}
362
363static void
Scott Moreau9521d5e2012-04-19 13:06:17 -0600364unresponsive_fade_frame(struct weston_animation *animation,
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600365 struct weston_output *output, uint32_t msecs)
366{
367 struct shell_surface *shsurf =
368 container_of(animation, struct shell_surface, unresponsive_animation.current);
369 struct weston_surface *surface = shsurf->surface;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600370 unsigned int step = 8;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600371
372 if (!surface || !shsurf)
373 return;
374
375 if (shsurf->unresponsive_animation.fading_in) {
376 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
377 if (surface->saturation > 1)
378 surface->saturation -= 5;
379 if (surface->brightness > 200)
380 surface->brightness--;
381
382 shsurf->unresponsive_animation.timestamp += step;
383 }
384
385 if (surface->saturation <= 1 && surface->brightness <= 200) {
386 wl_list_remove(&shsurf->unresponsive_animation.current.link);
387 shsurf->unresponsive_animation.exists = 0;
388 }
389 }
390 else {
391 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
392 if (surface->saturation < 255)
393 surface->saturation += 5;
394 if (surface->brightness < 255)
395 surface->brightness++;
396
397 shsurf->unresponsive_animation.timestamp += step;
398 }
399
400 if (surface->saturation >= 255 && surface->brightness >= 255) {
401 surface->saturation = surface->brightness = 255;
402 wl_list_remove(&shsurf->unresponsive_animation.current.link);
403 shsurf->unresponsive_animation.exists = 0;
404 }
405 }
406
407 surface->geometry.dirty = 1;
408 weston_surface_damage(surface);
409}
410
Scott Moreau9521d5e2012-04-19 13:06:17 -0600411static void
412ping_timer_destroy(struct shell_surface *shsurf)
413{
414 if (!shsurf || !shsurf->ping_timer)
415 return;
416
417 if (shsurf->ping_timer->source)
418 wl_event_source_remove(shsurf->ping_timer->source);
419
420 free(shsurf->ping_timer);
421 shsurf->ping_timer = NULL;
422}
423
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400424static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600425ping_timeout_handler(void *data)
426{
427 struct shell_surface *shsurf = data;
428
Scott Moreau9521d5e2012-04-19 13:06:17 -0600429 /* Client is not responding */
430 shsurf->unresponsive = 1;
431 unresponsive_surface_fade(shsurf, false);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600432
433 return 1;
434}
435
436static void
437ping_handler(struct weston_surface *surface, uint32_t serial)
438{
439 struct shell_surface *shsurf;
440 shsurf = get_shell_surface(surface);
441 struct wl_event_loop *loop;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600442 int ping_timeout = 2500;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600443
444 if (!shsurf)
445 return;
Kristian Høgsbergca535c12012-04-21 23:20:07 -0400446 if (!shsurf->resource.client)
447 return;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600448
449 if (!shsurf->ping_timer) {
Ander Conselvan de Oliveirafb980892012-04-27 13:55:55 +0300450 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600451 if (!shsurf->ping_timer)
452 return;
453
454 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600455 loop = wl_display_get_event_loop(surface->compositor->wl_display);
456 shsurf->ping_timer->source =
457 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
458 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
459
460 wl_shell_surface_send_ping(&shsurf->resource, serial);
461 }
462}
463
464static void
465shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
466 uint32_t serial)
467{
468 struct shell_surface *shsurf = resource->data;
469
Scott Moreauff1db4a2012-04-17 19:06:18 -0600470 if (shsurf->ping_timer->serial == serial) {
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600471 if (shsurf->unresponsive) {
472 /* Received pong from previously unresponsive client */
473 unresponsive_surface_fade(shsurf, true);
474 }
Scott Moreauff1db4a2012-04-17 19:06:18 -0600475 shsurf->unresponsive = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600476 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600477 }
478}
479
Kristian Høgsberge7afd912012-05-02 09:47:44 -0400480static void
481shell_surface_set_title(struct wl_client *client,
482 struct wl_resource *resource, const char *title)
483{
484 struct shell_surface *shsurf = resource->data;
485
486 free(shsurf->title);
487 shsurf->title = strdup(title);
488}
489
490static void
491shell_surface_set_class(struct wl_client *client,
492 struct wl_resource *resource, const char *class)
493{
494 struct shell_surface *shsurf = resource->data;
495
496 free(shsurf->class);
497 shsurf->class = strdup(class);
498}
499
Scott Moreauff1db4a2012-04-17 19:06:18 -0600500static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500501weston_surface_move(struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400502 struct weston_input_device *wd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500503{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500504 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300505 struct shell_surface *shsurf = get_shell_surface(es);
506
507 if (!shsurf)
508 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500509
510 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400511 if (!move)
512 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500513
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300514 shell_grab_init(&move->base, &move_grab_interface, shsurf);
515
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200516 move->dx = es->geometry.x - wd->input_device.grab_x;
517 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500518
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300519 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400520 &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500521
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400522 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400523
524 return 0;
525}
526
527static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200528shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400529 struct wl_resource *input_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400530{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500531 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200532 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400533
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500534 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400535 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500536 wd->input_device.pointer_focus != &shsurf->surface->surface)
537 return;
538
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400539 if (weston_surface_move(shsurf->surface, wd) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400540 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500541}
542
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500543struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300544 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500545 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200546 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500547};
548
549static void
Scott Moreau447013d2012-02-18 05:05:29 -0700550resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500551 uint32_t time, int32_t x, int32_t y)
552{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500553 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500554 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500555 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200556 int32_t from_x, from_y;
557 int32_t to_x, to_y;
558
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300559 if (!resize->base.shsurf)
560 return;
561
562 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200563 device->grab_x, device->grab_y,
564 &from_x, &from_y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300565 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200566 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500567
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200568 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200569 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200570 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200571 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500572 } else {
573 width = resize->width;
574 }
575
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200576 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200577 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200578 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200579 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500580 } else {
581 height = resize->height;
582 }
583
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300584 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400585 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500586}
587
588static void
Scott Moreau447013d2012-02-18 05:05:29 -0700589resize_grab_button(struct wl_pointer_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +0100590 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500591{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300592 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500593 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500594
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500595 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300596 shell_grab_finish(&resize->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400597 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500598 free(grab);
599 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500600}
601
Scott Moreau447013d2012-02-18 05:05:29 -0700602static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500603 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500604 resize_grab_motion,
605 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500606};
607
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400608static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500609weston_surface_resize(struct shell_surface *shsurf,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400610 struct weston_input_device *wd, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500611{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500612 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500613
Alex Wu4539b082012-03-01 12:57:46 +0800614 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
615 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500616
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200617 if (edges == 0 || edges > 15 ||
618 (edges & 3) == 3 || (edges & 12) == 12)
619 return 0;
620
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500621 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400622 if (!resize)
623 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500624
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300625 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
626
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500627 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200628 resize->width = shsurf->surface->geometry.width;
629 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400630
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300631 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400632 &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500633
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400634 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400635
636 return 0;
637}
638
639static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200640shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400641 struct wl_resource *input_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200642 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400643{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500644 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200645 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400646
Alex Wu4539b082012-03-01 12:57:46 +0800647 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
648 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400649
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500650 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400651 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500652 wd->input_device.pointer_focus != &shsurf->surface->surface)
653 return;
654
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400655 if (weston_surface_resize(shsurf, wd, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400656 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500657}
658
Juan Zhao96879df2012-02-07 08:45:41 +0800659static struct weston_output *
660get_default_output(struct weston_compositor *compositor)
661{
662 return container_of(compositor->output_list.next,
663 struct weston_output, link);
664}
665
Alex Wu4539b082012-03-01 12:57:46 +0800666static void
667shell_unset_fullscreen(struct shell_surface *shsurf)
668{
669 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800670 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
671 shell_surface_is_top_fullscreen(shsurf)) {
672 weston_output_switch_mode(shsurf->fullscreen_output,
673 shsurf->fullscreen_output->origin);
674 }
Alex Wu4539b082012-03-01 12:57:46 +0800675 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
676 shsurf->fullscreen.framerate = 0;
677 wl_list_remove(&shsurf->fullscreen.transform.link);
678 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800679 if (shsurf->fullscreen.black_surface)
680 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800681 shsurf->fullscreen.black_surface = NULL;
682 shsurf->fullscreen_output = NULL;
Alex Wu4539b082012-03-01 12:57:46 +0800683 weston_surface_set_position(shsurf->surface,
684 shsurf->saved_x, shsurf->saved_y);
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800685 if (shsurf->saved_rotation_valid) {
686 wl_list_insert(&shsurf->surface->geometry.transformation_list,
687 &shsurf->rotation.transform.link);
688 shsurf->saved_rotation_valid = false;
689 }
Alex Wu4539b082012-03-01 12:57:46 +0800690}
691
Pekka Paalanen98262232011-12-01 10:42:22 +0200692static int
693reset_shell_surface_type(struct shell_surface *surface)
694{
695 switch (surface->type) {
696 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800697 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200698 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800699 case SHELL_SURFACE_MAXIMIZED:
700 surface->output = get_default_output(surface->surface->compositor);
701 weston_surface_set_position(surface->surface,
702 surface->saved_x,
703 surface->saved_y);
704 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200705 case SHELL_SURFACE_PANEL:
706 case SHELL_SURFACE_BACKGROUND:
707 wl_list_remove(&surface->link);
708 wl_list_init(&surface->link);
709 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200710 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200711 case SHELL_SURFACE_LOCK:
712 wl_resource_post_error(&surface->resource,
713 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200714 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200715 return -1;
716 case SHELL_SURFACE_NONE:
717 case SHELL_SURFACE_TOPLEVEL:
718 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500719 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200720 break;
721 }
722
723 surface->type = SHELL_SURFACE_NONE;
724 return 0;
725}
726
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500727static void
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400728set_surface_type(struct shell_surface *shsurf)
729{
730 struct weston_surface *surface = shsurf->surface;
731 struct shell_surface *pshsurf = shsurf->parent;
732 struct weston_surface *pes;
733 struct shell_surface *priv;
734 struct desktop_shell *shell = shsurf->shell;
735
736 reset_shell_surface_type(shsurf);
737
738 shsurf->type = shsurf->next_type;
739 shsurf->next_type = SHELL_SURFACE_NONE;
740
741 switch (shsurf->type) {
742 case SHELL_SURFACE_TOPLEVEL:
743 break;
744 case SHELL_SURFACE_TRANSIENT:
745 pes = pshsurf->surface;
746 weston_surface_set_position(surface,
747 pes->geometry.x + shsurf->popup.x,
748 pes->geometry.y + shsurf->popup.y);
749 break;
750
751 case SHELL_SURFACE_MAXIMIZED:
752 shsurf->saved_x = surface->geometry.x;
753 shsurf->saved_y = surface->geometry.y;
754 shsurf->saved_position_valid = true;
755 break;
756
757 case SHELL_SURFACE_FULLSCREEN:
758 shsurf->saved_x = surface->geometry.x;
759 shsurf->saved_y = surface->geometry.y;
760 shsurf->saved_position_valid = true;
761
762 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
763 wl_list_remove(&shsurf->rotation.transform.link);
764 wl_list_init(&shsurf->rotation.transform.link);
765 shsurf->surface->geometry.dirty = 1;
766 shsurf->saved_rotation_valid = true;
767 }
768 break;
769
770 case SHELL_SURFACE_BACKGROUND:
771 wl_list_for_each(priv, &shell->backgrounds, link) {
772 if (priv->output == shsurf->output) {
773 priv->surface->output = NULL;
774 wl_list_remove(&priv->surface->layer_link);
775 wl_list_remove(&priv->link);
776 break;
777 }
778 }
779
780 wl_list_insert(&shell->backgrounds, &shsurf->link);
781
782 weston_surface_set_position(surface, shsurf->output->x,
783 shsurf->output->y);
784 break;
785
786 case SHELL_SURFACE_PANEL:
787 wl_list_for_each(priv, &shell->panels, link) {
788 if (priv->output == shsurf->output) {
789 priv->surface->output = NULL;
790 wl_list_remove(&priv->surface->layer_link);
791 wl_list_remove(&priv->link);
792 break;
793 }
794 }
795
796 wl_list_insert(&shell->panels, &shsurf->link);
797
798 weston_surface_set_position(surface, shsurf->output->x,
799 shsurf->output->y);
800 break;
801
802 default:
803 break;
804 }
805}
806
807static void
Tiago Vignattibc052c92012-04-19 16:18:18 +0300808set_toplevel(struct shell_surface *shsurf)
809{
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400810 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
Tiago Vignattibc052c92012-04-19 16:18:18 +0300811}
812
813static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200814shell_surface_set_toplevel(struct wl_client *client,
815 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400816{
Pekka Paalanen98262232011-12-01 10:42:22 +0200817 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400818
Tiago Vignattibc052c92012-04-19 16:18:18 +0300819 set_toplevel(surface);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400820}
821
822static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200823shell_surface_set_transient(struct wl_client *client,
824 struct wl_resource *resource,
825 struct wl_resource *parent_resource,
826 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400827{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200828 struct shell_surface *shsurf = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200829
Alex Wu4539b082012-03-01 12:57:46 +0800830 /* assign to parents output */
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400831 shsurf->parent = parent_resource->data;
832 shsurf->popup.x = x;
833 shsurf->popup.y = y;
834 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400835}
836
Tiago Vignattibe143262012-04-16 17:31:41 +0300837static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800838shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200839{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400840 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800841}
842
843static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300844get_output_panel_height(struct desktop_shell *shell,
845 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800846{
847 struct shell_surface *priv;
848 int panel_height = 0;
849
850 if (!output)
851 return 0;
852
Tiago Vignattibe143262012-04-16 17:31:41 +0300853 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800854 if (priv->output == output) {
855 panel_height = priv->surface->geometry.height;
856 break;
857 }
858 }
859 return panel_height;
860}
861
862static void
863shell_surface_set_maximized(struct wl_client *client,
864 struct wl_resource *resource,
865 struct wl_resource *output_resource )
866{
867 struct shell_surface *shsurf = resource->data;
868 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300869 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800870 uint32_t edges = 0, panel_height = 0;
871
872 /* get the default output, if the client set it as NULL
873 check whether the ouput is available */
874 if (output_resource)
875 shsurf->output = output_resource->data;
876 else
877 shsurf->output = get_default_output(es->compositor);
878
Tiago Vignattibe143262012-04-16 17:31:41 +0300879 shell = shell_surface_get_shell(shsurf);
880 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800881 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500882
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400883 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500884 es->output->current->width,
885 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800886
Kristian Høgsberg7f366e72012-04-27 17:20:01 -0400887 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200888}
889
Alex Wu21858432012-04-01 20:13:08 +0800890static void
891black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
892
Alex Wu4539b082012-03-01 12:57:46 +0800893static struct weston_surface *
894create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800895 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800896 GLfloat x, GLfloat y, int w, int h)
897{
898 struct weston_surface *surface = NULL;
899
900 surface = weston_surface_create(ec);
901 if (surface == NULL) {
902 fprintf(stderr, "no memory\n");
903 return NULL;
904 }
905
Alex Wu21858432012-04-01 20:13:08 +0800906 surface->configure = black_surface_configure;
907 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800908 weston_surface_configure(surface, x, y, w, h);
909 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
910 return surface;
911}
912
913/* Create black surface and append it to the associated fullscreen surface.
914 * Handle size dismatch and positioning according to the method. */
915static void
916shell_configure_fullscreen(struct shell_surface *shsurf)
917{
918 struct weston_output *output = shsurf->fullscreen_output;
919 struct weston_surface *surface = shsurf->surface;
920 struct weston_matrix *matrix;
921 float scale;
922
923 center_on_output(surface, output);
924
925 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500926 shsurf->fullscreen.black_surface =
927 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800928 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500929 output->x, output->y,
930 output->current->width,
931 output->current->height);
932
933 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
934 wl_list_insert(&surface->layer_link,
935 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800936 shsurf->fullscreen.black_surface->output = output;
937
938 switch (shsurf->fullscreen.type) {
939 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
940 break;
941 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
942 matrix = &shsurf->fullscreen.transform.matrix;
943 weston_matrix_init(matrix);
944 scale = (float)output->current->width/(float)surface->geometry.width;
945 weston_matrix_scale(matrix, scale, scale, 1);
946 wl_list_remove(&shsurf->fullscreen.transform.link);
947 wl_list_insert(surface->geometry.transformation_list.prev,
948 &shsurf->fullscreen.transform.link);
949 weston_surface_set_position(surface, output->x, output->y);
950 break;
951 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +0800952 if (shell_surface_is_top_fullscreen(shsurf)) {
953 struct weston_mode mode = {0,
954 surface->geometry.width,
955 surface->geometry.height,
956 shsurf->fullscreen.framerate};
957
958 if (weston_output_switch_mode(output, &mode) == 0) {
959 weston_surface_configure(shsurf->fullscreen.black_surface,
960 output->x, output->y,
961 output->current->width,
962 output->current->height);
963 weston_surface_set_position(surface, output->x, output->y);
964 break;
965 }
966 }
Alex Wu4539b082012-03-01 12:57:46 +0800967 break;
968 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
969 break;
970 default:
971 break;
972 }
973}
974
975/* make the fullscreen and black surface at the top */
976static void
977shell_stack_fullscreen(struct shell_surface *shsurf)
978{
Alex Wubd3354b2012-04-17 17:20:49 +0800979 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +0800980 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300981 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800982
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500983 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500984 wl_list_insert(&shell->fullscreen_layer.surface_list,
985 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +0800986 weston_surface_damage(surface);
987
988 if (!shsurf->fullscreen.black_surface)
989 shsurf->fullscreen.black_surface =
990 create_black_surface(surface->compositor,
991 surface,
992 output->x, output->y,
993 output->current->width,
994 output->current->height);
995
996 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500997 wl_list_insert(&surface->layer_link,
998 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500999 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +08001000}
1001
1002static void
1003shell_map_fullscreen(struct shell_surface *shsurf)
1004{
Alex Wu4539b082012-03-01 12:57:46 +08001005 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001006 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001007}
1008
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001009static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001010shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -05001011 struct wl_resource *resource,
1012 uint32_t method,
1013 uint32_t framerate,
1014 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001015{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001016 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001017 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +08001018
1019 if (output_resource)
1020 shsurf->output = output_resource->data;
1021 else
1022 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001023
Alex Wu4539b082012-03-01 12:57:46 +08001024 shsurf->fullscreen_output = shsurf->output;
1025 shsurf->fullscreen.type = method;
1026 shsurf->fullscreen.framerate = framerate;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001027 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
Alex Wu4539b082012-03-01 12:57:46 +08001028
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001029 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001030 shsurf->output->current->width,
1031 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001032}
1033
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001034static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001035popup_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001036 struct wl_surface *surface, int32_t x, int32_t y)
1037{
1038 struct wl_input_device *device = grab->input_device;
1039 struct shell_surface *priv =
1040 container_of(grab, struct shell_surface, popup.grab);
1041 struct wl_client *client = priv->surface->surface.resource.client;
1042
Pekka Paalanencb108432012-01-19 16:25:40 +02001043 if (surface && surface->resource.client == client) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001044 wl_input_device_set_pointer_focus(device, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001045 grab->focus = surface;
1046 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001047 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001048 grab->focus = NULL;
1049 }
1050}
1051
1052static void
Scott Moreau447013d2012-02-18 05:05:29 -07001053popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001054 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001055{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001056 struct wl_resource *resource;
1057
1058 resource = grab->input_device->pointer_focus_resource;
1059 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001060 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001061}
1062
1063static void
Scott Moreau447013d2012-02-18 05:05:29 -07001064popup_grab_button(struct wl_pointer_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +01001065 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001066{
1067 struct wl_resource *resource;
1068 struct shell_surface *shsurf =
1069 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001070 struct wl_display *display;
1071 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001072
1073 resource = grab->input_device->pointer_focus_resource;
1074 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001075 display = wl_client_get_display(resource->client);
1076 serial = wl_display_get_serial(display);
1077 wl_input_device_send_button(resource, serial,
1078 time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001079 } else if (state == 0 &&
1080 (shsurf->popup.initial_up ||
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001081 time - shsurf->popup.device->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001082 wl_shell_surface_send_popup_done(&shsurf->resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001083 wl_input_device_end_pointer_grab(grab->input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001084 shsurf->popup.grab.input_device = NULL;
1085 }
1086
1087 if (state == 0)
1088 shsurf->popup.initial_up = 1;
1089}
1090
Scott Moreau447013d2012-02-18 05:05:29 -07001091static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001092 popup_grab_focus,
1093 popup_grab_motion,
1094 popup_grab_button,
1095};
1096
1097static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001098shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001099{
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001100 struct wl_input_device *device = shsurf->popup.device;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001101 struct weston_surface *es = shsurf->surface;
1102 struct weston_surface *parent = shsurf->parent->surface;
1103
1104 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001105 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001106
Pekka Paalanen938269a2012-02-07 14:19:01 +02001107 weston_surface_update_transform(parent);
1108 if (parent->transform.enabled) {
1109 shsurf->popup.parent_transform.matrix =
1110 parent->transform.matrix;
1111 } else {
1112 /* construct x, y translation matrix */
1113 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1114 shsurf->popup.parent_transform.matrix.d[12] =
1115 parent->geometry.x;
1116 shsurf->popup.parent_transform.matrix.d[13] =
1117 parent->geometry.y;
1118 }
1119 wl_list_insert(es->geometry.transformation_list.prev,
1120 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001121 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001122
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001123 shsurf->popup.initial_up = 0;
1124
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001125 /* We don't require the grab to still be active, but if another
1126 * grab has started in the meantime, we end the popup now. */
1127 if (device->grab_serial == shsurf->popup.serial) {
1128 wl_input_device_start_pointer_grab(device,
1129 &shsurf->popup.grab);
1130 } else {
1131 wl_shell_surface_send_popup_done(&shsurf->resource);
1132 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001133}
1134
1135static void
1136shell_surface_set_popup(struct wl_client *client,
1137 struct wl_resource *resource,
1138 struct wl_resource *input_device_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001139 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001140 struct wl_resource *parent_resource,
1141 int32_t x, int32_t y, uint32_t flags)
1142{
1143 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001144
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001145 shsurf->type = SHELL_SURFACE_POPUP;
1146 shsurf->parent = parent_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001147 shsurf->popup.device = input_device_resource->data;
1148 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001149 shsurf->popup.x = x;
1150 shsurf->popup.y = y;
1151}
1152
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001153static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001154 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001155 shell_surface_move,
1156 shell_surface_resize,
1157 shell_surface_set_toplevel,
1158 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001159 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001160 shell_surface_set_popup,
Kristian Høgsberge7afd912012-05-02 09:47:44 -04001161 shell_surface_set_maximized,
1162 shell_surface_set_title,
1163 shell_surface_set_class
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001164};
1165
1166static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001167destroy_shell_surface(struct shell_surface *shsurf)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001168{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001169 if (shsurf->popup.grab.input_device)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001170 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001171
Alex Wubd3354b2012-04-17 17:20:49 +08001172 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1173 shell_surface_is_top_fullscreen(shsurf)) {
1174 weston_output_switch_mode(shsurf->fullscreen_output,
1175 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001176 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001177
Alex Wuaa08e2d2012-03-05 11:01:40 +08001178 if (shsurf->fullscreen.black_surface)
1179 weston_surface_destroy(shsurf->fullscreen.black_surface);
1180
Alex Wubd3354b2012-04-17 17:20:49 +08001181 /* As destroy_resource() use wl_list_for_each_safe(),
1182 * we can always remove the listener.
1183 */
1184 wl_list_remove(&shsurf->surface_destroy_listener.link);
1185 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001186 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001187
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001188 wl_list_remove(&shsurf->link);
1189 free(shsurf);
1190}
1191
1192static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001193shell_destroy_shell_surface(struct wl_resource *resource)
1194{
1195 struct shell_surface *shsurf = resource->data;
1196
1197 destroy_shell_surface(shsurf);
1198}
1199
1200static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001201shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001202{
1203 struct shell_surface *shsurf = container_of(listener,
1204 struct shell_surface,
1205 surface_destroy_listener);
1206
Tiago Vignattibc052c92012-04-19 16:18:18 +03001207 /* tricky way to check if resource was in fact created */
1208 if (shsurf->resource.object.implementation != 0)
1209 wl_resource_destroy(&shsurf->resource);
1210 else
1211 destroy_shell_surface(shsurf);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001212}
1213
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001214static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001215get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001216{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001217 struct wl_listener *listener;
1218
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001219 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1220 shell_handle_surface_destroy);
1221 if (listener)
1222 return container_of(listener, struct shell_surface,
1223 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001224
1225 return NULL;
1226}
1227
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001228static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001229shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1230
1231static void
Tiago Vignattibc052c92012-04-19 16:18:18 +03001232create_shell_surface(void *shell, struct weston_surface *surface,
1233 struct shell_surface **ret)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001234{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001235 struct shell_surface *shsurf;
1236
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001237 if (surface->configure) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001238 fprintf(stderr, "surface->configure already set\n");
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001239 return;
1240 }
1241
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001242 shsurf = calloc(1, sizeof *shsurf);
1243 if (!shsurf) {
Tiago Vignattibc052c92012-04-19 16:18:18 +03001244 fprintf(stderr, "no memory to allocate shell surface\n");
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001245 return;
1246 }
1247
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001248 surface->configure = shell_surface_configure;
Tiago Vignattibc052c92012-04-19 16:18:18 +03001249 surface->compositor->shell_interface.shell = shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001250
Tiago Vignattibc052c92012-04-19 16:18:18 +03001251 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001252 shsurf->unresponsive = 0;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001253 shsurf->unresponsive_animation.exists = 0;
1254 shsurf->unresponsive_animation.fading_in = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001255 shsurf->unresponsive_animation.current.frame = unresponsive_fade_frame;
Alex Wu4539b082012-03-01 12:57:46 +08001256 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001257 shsurf->saved_rotation_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001258 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001259 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1260 shsurf->fullscreen.framerate = 0;
1261 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001262 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001263 wl_list_init(&shsurf->fullscreen.transform.link);
1264
Tiago Vignattibc052c92012-04-19 16:18:18 +03001265 wl_signal_init(&shsurf->resource.destroy_signal);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001266 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1267 wl_signal_add(&surface->surface.resource.destroy_signal,
1268 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001269
1270 /* init link so its safe to always remove it in destroy_shell_surface */
1271 wl_list_init(&shsurf->link);
1272
Pekka Paalanen460099f2012-01-20 16:48:25 +02001273 /* empty when not in use */
1274 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001275 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001276
Pekka Paalanen98262232011-12-01 10:42:22 +02001277 shsurf->type = SHELL_SURFACE_NONE;
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001278 shsurf->next_type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001279
Tiago Vignattibc052c92012-04-19 16:18:18 +03001280 *ret = shsurf;
1281}
1282
1283static void
1284shell_get_shell_surface(struct wl_client *client,
1285 struct wl_resource *resource,
1286 uint32_t id,
1287 struct wl_resource *surface_resource)
1288{
1289 struct weston_surface *surface = surface_resource->data;
1290 struct desktop_shell *shell = resource->data;
1291 struct shell_surface *shsurf;
1292
1293 if (get_shell_surface(surface)) {
1294 wl_resource_post_error(surface_resource,
1295 WL_DISPLAY_ERROR_INVALID_OBJECT,
1296 "desktop_shell::get_shell_surface already requested");
1297 return;
1298 }
1299
1300 create_shell_surface(shell, surface, &shsurf);
1301 if (!shsurf) {
1302 wl_resource_post_error(surface_resource,
1303 WL_DISPLAY_ERROR_INVALID_OBJECT,
1304 "surface->configure already set");
1305 return;
1306 }
1307
1308 shsurf->resource.destroy = shell_destroy_shell_surface;
1309 shsurf->resource.object.id = id;
1310 shsurf->resource.object.interface = &wl_shell_surface_interface;
1311 shsurf->resource.object.implementation =
1312 (void (**)(void)) &shell_surface_implementation;
1313 shsurf->resource.data = shsurf;
1314
1315 wl_client_add_resource(client, &shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001316}
1317
1318static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001319 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001320};
1321
Kristian Høgsberg07937562011-04-12 17:25:42 -04001322static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001323handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001324{
1325 proc->pid = 0;
1326}
1327
1328static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001329launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001330{
1331 if (shell->screensaver.binding)
1332 return;
1333
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001334 if (!shell->screensaver.path)
1335 return;
1336
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001337 if (shell->screensaver.process.pid != 0) {
1338 fprintf(stderr, "old screensaver still running\n");
1339 return;
1340 }
1341
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001342 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001343 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001344 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001345 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001346}
1347
1348static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001349terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001350{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001351 if (shell->screensaver.process.pid == 0)
1352 return;
1353
1354 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001355}
1356
1357static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001358show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001359{
1360 struct wl_list *list;
1361
1362 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001363 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001364 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001365 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001366
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001367 wl_list_remove(&surface->surface->layer_link);
1368 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001369 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001370 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001371}
1372
1373static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001374hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001375{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001376 wl_list_remove(&surface->surface->layer_link);
1377 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001378 surface->surface->output = NULL;
1379}
1380
1381static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001382desktop_shell_set_background(struct wl_client *client,
1383 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001384 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001385 struct wl_resource *surface_resource)
1386{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001387 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001388
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001389 shsurf->next_type = SHELL_SURFACE_BACKGROUND;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001390 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001391
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001392 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001393 surface_resource,
1394 shsurf->output->current->width,
1395 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001396}
1397
1398static void
1399desktop_shell_set_panel(struct wl_client *client,
1400 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001401 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001402 struct wl_resource *surface_resource)
1403{
Pekka Paalanen068ae942011-11-28 14:11:15 +02001404 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001405
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001406 shsurf->next_type = SHELL_SURFACE_PANEL;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001407 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001408
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001409 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001410 surface_resource,
1411 shsurf->output->current->width,
1412 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001413}
1414
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001415static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001416handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001417{
Tiago Vignattibe143262012-04-16 17:31:41 +03001418 struct desktop_shell *shell =
1419 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001420
1421 fprintf(stderr, "lock surface gone\n");
1422 shell->lock_surface = NULL;
1423}
1424
1425static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001426desktop_shell_set_lock_surface(struct wl_client *client,
1427 struct wl_resource *resource,
1428 struct wl_resource *surface_resource)
1429{
Tiago Vignattibe143262012-04-16 17:31:41 +03001430 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001431 struct shell_surface *surface = surface_resource->data;
1432
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001433 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001434
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001435 if (!shell->locked)
1436 return;
1437
Pekka Paalanen98262232011-12-01 10:42:22 +02001438 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001439
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001440 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1441 wl_signal_add(&surface_resource->destroy_signal,
1442 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001443
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04001444 shell->lock_surface->next_type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001445}
1446
1447static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001448resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001449{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001450 struct shell_surface *tmp;
1451
1452 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1453 hide_screensaver(shell, tmp);
1454
1455 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001456
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001457 wl_list_remove(&shell->lock_layer.link);
1458 wl_list_insert(&shell->compositor->cursor_layer.link,
1459 &shell->fullscreen_layer.link);
1460 wl_list_insert(&shell->fullscreen_layer.link,
1461 &shell->panel_layer.link);
1462 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001463
1464 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001465 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001466 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001467 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001468}
1469
1470static void
1471desktop_shell_unlock(struct wl_client *client,
1472 struct wl_resource *resource)
1473{
Tiago Vignattibe143262012-04-16 17:31:41 +03001474 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001475
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001476 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001477
1478 if (shell->locked)
1479 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001480}
1481
Kristian Høgsberg75840622011-09-06 13:48:16 -04001482static const struct desktop_shell_interface desktop_shell_implementation = {
1483 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001484 desktop_shell_set_panel,
1485 desktop_shell_set_lock_surface,
1486 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001487};
1488
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001489static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001490get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001491{
1492 struct shell_surface *shsurf;
1493
1494 shsurf = get_shell_surface(surface);
1495 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001496 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001497 return shsurf->type;
1498}
1499
Kristian Høgsberg75840622011-09-06 13:48:16 -04001500static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001501move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001502 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001503{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001504 struct weston_surface *surface =
1505 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001506
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001507 if (surface == NULL)
1508 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001509
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001510 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001511 case SHELL_SURFACE_PANEL:
1512 case SHELL_SURFACE_BACKGROUND:
1513 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001514 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001515 return;
1516 default:
1517 break;
1518 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001519
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001520 weston_surface_move(surface, (struct weston_input_device *) device);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001521}
1522
1523static void
1524resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001525 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001526{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001527 struct weston_surface *surface =
1528 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001529 uint32_t edges = 0;
1530 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001531 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001532
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001533 if (surface == NULL)
1534 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001535
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001536 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001537 if (!shsurf)
1538 return;
1539
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001540 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001541 case SHELL_SURFACE_PANEL:
1542 case SHELL_SURFACE_BACKGROUND:
1543 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001544 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001545 return;
1546 default:
1547 break;
1548 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001549
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001550 weston_surface_from_global(surface,
1551 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001552
Pekka Paalanen60921e52012-01-25 15:55:43 +02001553 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001554 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001555 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001556 edges |= 0;
1557 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001558 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001559
Pekka Paalanen60921e52012-01-25 15:55:43 +02001560 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001561 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001562 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001563 edges |= 0;
1564 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001565 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001566
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001567 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001568 edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001569}
1570
1571static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001572surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1573 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1574{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001575 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001576 struct shell_surface *shsurf;
1577 struct weston_surface *surface =
1578 (struct weston_surface *) device->pointer_focus;
1579
1580 if (surface == NULL)
1581 return;
1582
1583 shsurf = get_shell_surface(surface);
1584 if (!shsurf)
1585 return;
1586
1587 switch (shsurf->type) {
1588 case SHELL_SURFACE_BACKGROUND:
1589 case SHELL_SURFACE_SCREENSAVER:
1590 return;
1591 default:
1592 break;
1593 }
1594
1595 surface->alpha += value * step;
1596
1597 if (surface->alpha > 255)
1598 surface->alpha = 255;
1599 if (surface->alpha < step)
1600 surface->alpha = step;
1601
1602 surface->geometry.dirty = 1;
1603 weston_surface_damage(surface);
1604}
1605
1606static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001607zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001608 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001609{
1610 struct weston_input_device *wd = (struct weston_input_device *) device;
1611 struct weston_compositor *compositor = wd->compositor;
1612 struct weston_output *output;
1613
1614 wl_list_for_each(output, &compositor->output_list, link) {
1615 if (pixman_region32_contains_point(&output->region,
1616 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001617 output->zoom.active = 1;
1618 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001619
1620 if (output->zoom.level >= 1.0) {
1621 output->zoom.active = 0;
1622 output->zoom.level = 1.0;
1623 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001624
1625 if (output->zoom.level < output->zoom.increment)
1626 output->zoom.level = output->zoom.increment;
1627
1628 weston_output_update_zoom(output, device->x, device->y);
1629 }
1630 }
1631}
1632
Scott Moreauccbf29d2012-02-22 14:21:41 -07001633static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001634terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001635 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001636{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001637 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001638
1639 if (state)
1640 wl_display_terminate(compositor->wl_display);
1641}
1642
1643static void
Scott Moreau447013d2012-02-18 05:05:29 -07001644rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001645 uint32_t time, int32_t x, int32_t y)
1646{
1647 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001648 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001649 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001650 struct shell_surface *shsurf = rotate->base.shsurf;
1651 struct weston_surface *surface;
1652 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1653
1654 if (!shsurf)
1655 return;
1656
1657 surface = shsurf->surface;
1658
1659 cx = 0.5f * surface->geometry.width;
1660 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001661
1662 dx = device->x - rotate->center.x;
1663 dy = device->y - rotate->center.y;
1664 r = sqrtf(dx * dx + dy * dy);
1665
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001666 wl_list_remove(&shsurf->rotation.transform.link);
1667 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001668
1669 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001670 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001671 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001672
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001673 weston_matrix_init(&rotate->rotation);
1674 rotate->rotation.d[0] = dx / r;
1675 rotate->rotation.d[4] = -dy / r;
1676 rotate->rotation.d[1] = -rotate->rotation.d[4];
1677 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001678
1679 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001680 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001681 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001682 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001683 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001684
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001685 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001686 &shsurf->surface->geometry.transformation_list,
1687 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001688 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001689 wl_list_init(&shsurf->rotation.transform.link);
1690 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001691 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001692 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001693
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001694 /* We need to adjust the position of the surface
1695 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001696 cposx = surface->geometry.x + cx;
1697 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001698 dposx = rotate->center.x - cposx;
1699 dposy = rotate->center.y - cposy;
1700 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001701 weston_surface_set_position(surface,
1702 surface->geometry.x + dposx,
1703 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001704 }
1705
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001706 /* Repaint implies weston_surface_update_transform(), which
1707 * lazily applies the damage due to rotation update.
1708 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001709 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001710}
1711
1712static void
Scott Moreau447013d2012-02-18 05:05:29 -07001713rotate_grab_button(struct wl_pointer_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +01001714 uint32_t time, uint32_t button, uint32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001715{
1716 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001717 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001718 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001719 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001720
1721 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001722 if (shsurf)
1723 weston_matrix_multiply(&shsurf->rotation.rotation,
1724 &rotate->rotation);
1725 shell_grab_finish(&rotate->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001726 wl_input_device_end_pointer_grab(device);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001727 free(rotate);
1728 }
1729}
1730
Scott Moreau447013d2012-02-18 05:05:29 -07001731static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001732 noop_grab_focus,
1733 rotate_grab_motion,
1734 rotate_grab_button,
1735};
1736
1737static void
1738rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001739 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001740{
1741 struct weston_surface *base_surface =
1742 (struct weston_surface *) device->pointer_focus;
1743 struct shell_surface *surface;
1744 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001745 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001746 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001747
1748 if (base_surface == NULL)
1749 return;
1750
1751 surface = get_shell_surface(base_surface);
1752 if (!surface)
1753 return;
1754
1755 switch (surface->type) {
1756 case SHELL_SURFACE_PANEL:
1757 case SHELL_SURFACE_BACKGROUND:
1758 case SHELL_SURFACE_FULLSCREEN:
1759 case SHELL_SURFACE_SCREENSAVER:
1760 return;
1761 default:
1762 break;
1763 }
1764
Pekka Paalanen460099f2012-01-20 16:48:25 +02001765 rotate = malloc(sizeof *rotate);
1766 if (!rotate)
1767 return;
1768
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001769 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001770
1771 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001772 surface->surface->geometry.width / 2,
1773 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001774 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001775
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001776 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001777
1778 dx = device->x - rotate->center.x;
1779 dy = device->y - rotate->center.y;
1780 r = sqrtf(dx * dx + dy * dy);
1781 if (r > 20.0f) {
1782 struct weston_matrix inverse;
1783
1784 weston_matrix_init(&inverse);
1785 inverse.d[0] = dx / r;
1786 inverse.d[4] = dy / r;
1787 inverse.d[1] = -inverse.d[4];
1788 inverse.d[5] = inverse.d[0];
1789 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001790
1791 weston_matrix_init(&rotate->rotation);
1792 rotate->rotation.d[0] = dx / r;
1793 rotate->rotation.d[4] = -dy / r;
1794 rotate->rotation.d[1] = -rotate->rotation.d[4];
1795 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001796 } else {
1797 weston_matrix_init(&surface->rotation.rotation);
1798 weston_matrix_init(&rotate->rotation);
1799 }
1800
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001801 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001802}
1803
1804static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001805activate(struct desktop_shell *shell, struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001806 struct weston_input_device *device)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001807{
Alex Wu21858432012-04-01 20:13:08 +08001808 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001809
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001810 weston_surface_activate(es, device);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001811
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001812 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001813 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001814 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001815 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001816 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001817
Pekka Paalanen77346a62011-11-30 16:26:35 +02001818 case SHELL_SURFACE_SCREENSAVER:
1819 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001820 if (shell->lock_surface)
1821 weston_surface_restack(es,
1822 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001823 break;
Alex Wu4539b082012-03-01 12:57:46 +08001824 case SHELL_SURFACE_FULLSCREEN:
1825 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001826 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001827 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001828 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001829 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001830 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001831 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1832 wl_list_for_each_reverse_safe(surf,
1833 prev,
1834 &shell->fullscreen_layer.surface_list,
1835 layer_link)
1836 weston_surface_restack(surf,
1837 &shell->toplevel_layer.surface_list);
1838 }
1839
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001840 weston_surface_restack(es,
1841 &shell->toplevel_layer.surface_list);
1842 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001843 }
1844}
1845
Alex Wu21858432012-04-01 20:13:08 +08001846/* no-op func for checking black surface */
1847static void
1848black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1849{
1850}
1851
1852static bool
1853is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1854{
1855 if (es->configure == black_surface_configure) {
1856 if (fs_surface)
1857 *fs_surface = (struct weston_surface *)es->private;
1858 return true;
1859 }
1860 return false;
1861}
1862
Kristian Høgsberg75840622011-09-06 13:48:16 -04001863static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001864click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001865 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001866 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001867{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001868 struct weston_input_device *wd = (struct weston_input_device *) device;
Tiago Vignattibe143262012-04-16 17:31:41 +03001869 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001870 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001871 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001872
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001873 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001874 if (!focus)
1875 return;
1876
Alex Wu21858432012-04-01 20:13:08 +08001877 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001878 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001879
Alex Wu9c35e6b2012-03-05 14:13:13 +08001880 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001881 activate(shell, focus, wd);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001882}
1883
1884static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001885lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001886{
Tiago Vignattibe143262012-04-16 17:31:41 +03001887 struct desktop_shell *shell =
1888 container_of(listener, struct desktop_shell, lock_listener);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001889 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001890 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001891 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001892
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001893 if (shell->locked) {
1894 wl_list_for_each(output, &shell->compositor->output_list, link)
1895 /* TODO: find a way to jump to other DPMS levels */
1896 if (output->set_dpms)
1897 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001898 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001899 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001900
1901 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001902
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001903 /* Hide all surfaces by removing the fullscreen, panel and
1904 * toplevel layers. This way nothing else can show or receive
1905 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001906
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001907 wl_list_remove(&shell->panel_layer.link);
1908 wl_list_remove(&shell->toplevel_layer.link);
1909 wl_list_remove(&shell->fullscreen_layer.link);
1910 wl_list_insert(&shell->compositor->cursor_layer.link,
1911 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001912
Pekka Paalanen77346a62011-11-30 16:26:35 +02001913 launch_screensaver(shell);
1914
1915 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1916 show_screensaver(shell, shsurf);
1917
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001918 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001919 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001920 weston_compositor_wake(shell->compositor);
1921 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001922 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001923
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001924 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001925 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001926
1927 /* reset keyboard foci */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001928 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1929 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001930 NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001931 }
1932
1933 /* TODO: disable bindings that should not work while locked. */
1934
1935 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001936}
1937
1938static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001939unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001940{
Tiago Vignattibe143262012-04-16 17:31:41 +03001941 struct desktop_shell *shell =
1942 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001943
Pekka Paalanend81c2162011-11-16 13:47:34 +02001944 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001945 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001946 return;
1947 }
1948
1949 /* If desktop-shell client has gone away, unlock immediately. */
1950 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001951 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001952 return;
1953 }
1954
1955 if (shell->prepare_event_sent)
1956 return;
1957
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001958 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001959 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001960}
1961
1962static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001963center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001964{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001965 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001966 GLfloat x = (mode->width - surface->geometry.width) / 2;
1967 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001968
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001969 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001970}
1971
1972static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001973map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001974 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001975{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001976 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001977 struct shell_surface *shsurf;
1978 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001979 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001980 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001981
Pekka Paalanen77346a62011-11-30 16:26:35 +02001982 shsurf = get_shell_surface(surface);
1983 if (shsurf)
1984 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001985
Pekka Paalanen60921e52012-01-25 15:55:43 +02001986 surface->geometry.width = width;
1987 surface->geometry.height = height;
1988 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001989
1990 /* initial positioning, see also configure() */
1991 switch (surface_type) {
1992 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001993 weston_surface_set_position(surface, 10 + random() % 400,
1994 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001995 break;
1996 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001997 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001998 break;
Alex Wu4539b082012-03-01 12:57:46 +08001999 case SHELL_SURFACE_FULLSCREEN:
2000 shell_map_fullscreen(shsurf);
2001 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002002 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002003 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08002004 panel_height = get_output_panel_height(shell,surface->output);
2005 weston_surface_set_position(surface, surface->output->x,
2006 surface->output->y + panel_height);
2007 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002008 case SHELL_SURFACE_LOCK:
2009 center_on_output(surface, get_default_output(compositor));
2010 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02002011 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04002012 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02002013 case SHELL_SURFACE_NONE:
2014 weston_surface_set_position(surface,
2015 surface->geometry.x + sx,
2016 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02002017 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002018 default:
2019 ;
2020 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04002021
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002022 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02002023 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002024 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002025 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002026 wl_list_insert(&shell->background_layer.surface_list,
2027 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002028 break;
2029 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002030 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002031 wl_list_insert(&shell->panel_layer.surface_list,
2032 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002033 break;
2034 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02002035 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002036 wl_list_insert(&shell->lock_layer.surface_list,
2037 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002038 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002039 break;
2040 case SHELL_SURFACE_SCREENSAVER:
2041 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002042 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02002043 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02002044 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002045 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02002046 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002047 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02002048 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002049 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05002050 case SHELL_SURFACE_POPUP:
2051 case SHELL_SURFACE_TRANSIENT:
2052 parent = shsurf->parent->surface;
2053 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2054 break;
Alex Wu4539b082012-03-01 12:57:46 +08002055 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02002056 case SHELL_SURFACE_NONE:
2057 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02002058 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002059 wl_list_insert(&shell->toplevel_layer.surface_list,
2060 &surface->layer_link);
2061 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002062 }
2063
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002064 if (surface_type != SHELL_SURFACE_NONE) {
2065 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02002066 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2067 surface->output = shsurf->output;
2068 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002069
Juan Zhao7bb92f02011-12-15 11:31:51 -05002070 switch (surface_type) {
2071 case SHELL_SURFACE_TOPLEVEL:
2072 case SHELL_SURFACE_TRANSIENT:
2073 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08002074 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05002075 if (!shell->locked)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002076 activate(shell, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002077 (struct weston_input_device *)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002078 compositor->input_device);
Juan Zhao7bb92f02011-12-15 11:31:51 -05002079 break;
2080 default:
2081 break;
2082 }
2083
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05002084 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Juan Zhaoe10d2792012-04-25 19:09:52 +08002085 {
2086 switch (shell->win_animation_type) {
2087 case ANIMATION_FADE:
2088 weston_fade_run(surface, NULL, NULL);
2089 break;
2090 case ANIMATION_ZOOM:
2091 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2092 break;
2093 default:
2094 break;
2095 }
2096 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002097}
2098
2099static void
Tiago Vignattibe143262012-04-16 17:31:41 +03002100configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02002101 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002102{
Pekka Paalanen77346a62011-11-30 16:26:35 +02002103 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2104 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002105
Pekka Paalanen77346a62011-11-30 16:26:35 +02002106 shsurf = get_shell_surface(surface);
2107 if (shsurf)
2108 surface_type = shsurf->type;
2109
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002110 surface->geometry.x = x;
2111 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02002112 surface->geometry.width = width;
2113 surface->geometry.height = height;
2114 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002115
2116 switch (surface_type) {
2117 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002118 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002119 break;
Alex Wu4539b082012-03-01 12:57:46 +08002120 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08002121 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002122 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002123 break;
Juan Zhao96879df2012-02-07 08:45:41 +08002124 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08002125 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002126 surface->geometry.x = surface->output->x;
2127 surface->geometry.y = surface->output->y +
2128 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002129 break;
Alex Wu4539b082012-03-01 12:57:46 +08002130 case SHELL_SURFACE_TOPLEVEL:
2131 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002132 default:
2133 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002134 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002135
Alex Wu4539b082012-03-01 12:57:46 +08002136 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002137 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002138 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002139
2140 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2141 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002142 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2143 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002144 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002145}
2146
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002147static void
2148shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2149{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002150 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002151 struct desktop_shell *shell = shsurf->shell;
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002152 int type_changed;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002153
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002154 if (shsurf->next_type != SHELL_SURFACE_NONE &&
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002155 shsurf->type != shsurf->next_type) {
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002156 set_surface_type(shsurf);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002157 type_changed = 1;
2158 }
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002159
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002160 if (!weston_surface_is_mapped(es)) {
2161 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Kristian Høgsbergf7e23d52012-04-27 17:51:59 -04002162 } else if (type_changed || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002163 es->geometry.width != es->buffer->width ||
2164 es->geometry.height != es->buffer->height) {
2165 GLfloat from_x, from_y;
2166 GLfloat to_x, to_y;
2167
2168 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2169 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2170 configure(shell, es,
2171 es->geometry.x + to_x - from_x,
2172 es->geometry.y + to_y - from_y,
2173 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002174 }
2175}
2176
Tiago Vignattibe143262012-04-16 17:31:41 +03002177static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002178
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002179static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002180desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002181{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002182 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002183 struct desktop_shell *shell =
2184 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002185
2186 shell->child.process.pid = 0;
2187 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002188
2189 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2190 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002191 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002192 shell->child.deathstamp = time;
2193 shell->child.deathcount = 0;
2194 }
2195
2196 shell->child.deathcount++;
2197 if (shell->child.deathcount > 5) {
2198 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2199 return;
2200 }
2201
2202 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2203 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002204}
2205
2206static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002207launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002208{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002209 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002210
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002211 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002212 &shell->child.process,
2213 shell_exe,
2214 desktop_shell_sigchld);
2215
2216 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002217 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002218 return 0;
2219}
2220
2221static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002222bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2223{
Tiago Vignattibe143262012-04-16 17:31:41 +03002224 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002225
2226 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002227 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002228}
2229
Kristian Høgsberg75840622011-09-06 13:48:16 -04002230static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002231unbind_desktop_shell(struct wl_resource *resource)
2232{
Tiago Vignattibe143262012-04-16 17:31:41 +03002233 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002234
2235 if (shell->locked)
2236 resume_desktop(shell);
2237
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002238 shell->child.desktop_shell = NULL;
2239 shell->prepare_event_sent = false;
2240 free(resource);
2241}
2242
2243static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002244bind_desktop_shell(struct wl_client *client,
2245 void *data, uint32_t version, uint32_t id)
2246{
Tiago Vignattibe143262012-04-16 17:31:41 +03002247 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002248 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002249
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002250 resource = wl_client_add_object(client, &desktop_shell_interface,
2251 &desktop_shell_implementation,
2252 id, shell);
2253
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002254 if (client == shell->child.client) {
2255 resource->destroy = unbind_desktop_shell;
2256 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002257 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002258 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002259
2260 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2261 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002262 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002263}
2264
Pekka Paalanen6e168112011-11-24 11:34:05 +02002265static void
2266screensaver_set_surface(struct wl_client *client,
2267 struct wl_resource *resource,
2268 struct wl_resource *shell_surface_resource,
2269 struct wl_resource *output_resource)
2270{
Tiago Vignattibe143262012-04-16 17:31:41 +03002271 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002272 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002273 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002274
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002275 surface->next_type = SHELL_SURFACE_SCREENSAVER;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002276
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002277 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002278 surface->output = output;
2279 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002280}
2281
2282static const struct screensaver_interface screensaver_implementation = {
2283 screensaver_set_surface
2284};
2285
2286static void
2287unbind_screensaver(struct wl_resource *resource)
2288{
Tiago Vignattibe143262012-04-16 17:31:41 +03002289 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002290
Pekka Paalanen77346a62011-11-30 16:26:35 +02002291 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002292 free(resource);
2293}
2294
2295static void
2296bind_screensaver(struct wl_client *client,
2297 void *data, uint32_t version, uint32_t id)
2298{
Tiago Vignattibe143262012-04-16 17:31:41 +03002299 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002300 struct wl_resource *resource;
2301
2302 resource = wl_client_add_object(client, &screensaver_interface,
2303 &screensaver_implementation,
2304 id, shell);
2305
Pekka Paalanen77346a62011-11-30 16:26:35 +02002306 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002307 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002308 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002309 return;
2310 }
2311
2312 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2313 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002314 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002315}
2316
Kristian Høgsberg07045392012-02-19 18:52:44 -05002317struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002318 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002319 struct weston_surface *current;
2320 struct wl_listener listener;
2321 struct wl_keyboard_grab grab;
2322};
2323
2324static void
2325switcher_next(struct switcher *switcher)
2326{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002327 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002328 struct weston_surface *surface;
2329 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002330 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002331
2332 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002333 switch (get_shell_surface_type(surface)) {
2334 case SHELL_SURFACE_TOPLEVEL:
2335 case SHELL_SURFACE_FULLSCREEN:
2336 case SHELL_SURFACE_MAXIMIZED:
2337 if (first == NULL)
2338 first = surface;
2339 if (prev == switcher->current)
2340 next = surface;
2341 prev = surface;
2342 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002343 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002344 weston_surface_damage(surface);
2345 break;
2346 default:
2347 break;
2348 }
Alex Wu1659daa2012-04-01 20:13:09 +08002349
2350 if (is_black_surface(surface, NULL)) {
2351 surface->alpha = 64;
2352 surface->geometry.dirty = 1;
2353 weston_surface_damage(surface);
2354 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002355 }
2356
2357 if (next == NULL)
2358 next = first;
2359
Alex Wu07b26062012-03-12 16:06:01 +08002360 if (next == NULL)
2361 return;
2362
Kristian Høgsberg07045392012-02-19 18:52:44 -05002363 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002364 wl_signal_add(&next->surface.resource.destroy_signal,
2365 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002366
2367 switcher->current = next;
2368 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002369
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002370 shsurf = get_shell_surface(switcher->current);
2371 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2372 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002373}
2374
2375static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002376switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002377{
2378 struct switcher *switcher =
2379 container_of(listener, struct switcher, listener);
2380
2381 switcher_next(switcher);
2382}
2383
2384static void
2385switcher_destroy(struct switcher *switcher, uint32_t time)
2386{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002387 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002388 struct weston_surface *surface;
2389 struct weston_input_device *device =
2390 (struct weston_input_device *) switcher->grab.input_device;
2391
2392 wl_list_for_each(surface, &compositor->surface_list, link) {
2393 surface->alpha = 255;
2394 weston_surface_damage(surface);
2395 }
2396
Alex Wu07b26062012-03-12 16:06:01 +08002397 if (switcher->current)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002398 activate(switcher->shell, switcher->current, device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002399 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002400 wl_input_device_end_keyboard_grab(&device->input_device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002401 free(switcher);
2402}
2403
2404static void
2405switcher_key(struct wl_keyboard_grab *grab,
Daniel Stoneda5b93c2012-05-04 11:21:54 +01002406 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002407{
2408 struct switcher *switcher = container_of(grab, struct switcher, grab);
2409 struct weston_input_device *device =
2410 (struct weston_input_device *) grab->input_device;
2411
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002412 if ((device->modifier_state & switcher->shell->binding_modifier) == 0) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002413 switcher_destroy(switcher, time);
2414 } else if (key == KEY_TAB && state) {
2415 switcher_next(switcher);
2416 }
2417};
2418
2419static const struct wl_keyboard_grab_interface switcher_grab = {
2420 switcher_key
2421};
2422
2423static void
2424switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002425 uint32_t key, uint32_t button, uint32_t axis,
2426 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002427{
Tiago Vignattibe143262012-04-16 17:31:41 +03002428 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002429 struct switcher *switcher;
2430
2431 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002432 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002433 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002434 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002435 wl_list_init(&switcher->listener.link);
2436
2437 switcher->grab.interface = &switcher_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002438 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2439 wl_input_device_set_keyboard_focus(device, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002440 switcher_next(switcher);
2441}
2442
Pekka Paalanen3c647232011-12-22 13:43:43 +02002443static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002444backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002445 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002446{
2447 struct weston_compositor *compositor = data;
2448 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002449 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002450
2451 /* TODO: we're limiting to simple use cases, where we assume just
2452 * control on the primary display. We'd have to extend later if we
2453 * ever get support for setting backlights on random desktop LCD
2454 * panels though */
2455 output = get_default_output(compositor);
2456 if (!output)
2457 return;
2458
2459 if (!output->set_backlight)
2460 return;
2461
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002462 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2463 backlight_new = output->backlight_current - 25;
2464 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2465 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002466
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002467 if (backlight_new < 5)
2468 backlight_new = 5;
2469 if (backlight_new > 255)
2470 backlight_new = 255;
2471
2472 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002473 output->set_backlight(output, output->backlight_current);
2474}
2475
2476static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002477debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002478 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002479{
Tiago Vignattibe143262012-04-16 17:31:41 +03002480 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002481 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002482 struct weston_surface *surface;
2483
2484 if (shell->debug_repaint_surface) {
2485 weston_surface_destroy(shell->debug_repaint_surface);
2486 shell->debug_repaint_surface = NULL;
2487 } else {
2488 surface = weston_surface_create(compositor);
2489 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2490 weston_surface_configure(surface, 0, 0, 8192, 8192);
2491 wl_list_insert(&compositor->fade_layer.surface_list,
2492 &surface->layer_link);
2493 weston_surface_assign_output(surface);
2494 pixman_region32_init(&surface->input);
2495
2496 /* Here's the dirty little trick that makes the
2497 * repaint debugging work: we force an
2498 * update_transform first to update dependent state
2499 * and clear the geometry.dirty bit. Then we clear
2500 * the surface damage so it only gets repainted
2501 * piecewise as we repaint other things. */
2502
2503 weston_surface_update_transform(surface);
2504 pixman_region32_fini(&surface->damage);
2505 pixman_region32_init(&surface->damage);
2506 shell->debug_repaint_surface = surface;
2507 }
2508}
2509
2510static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002511shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002512{
Tiago Vignattibe143262012-04-16 17:31:41 +03002513 struct desktop_shell *shell =
2514 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002515
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002516 if (shell->child.client)
2517 wl_client_destroy(shell->child.client);
2518
Pekka Paalanen3c647232011-12-22 13:43:43 +02002519 free(shell->screensaver.path);
2520 free(shell);
2521}
2522
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002523static void
2524shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2525{
2526 uint32_t mod;
2527
2528 /* fixed bindings */
2529 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2530 MODIFIER_CTRL | MODIFIER_ALT,
2531 terminate_binding, ec);
2532 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2533 click_to_activate_binding, shell);
2534 weston_compositor_add_binding(ec, 0, 0,
2535 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2536 MODIFIER_SUPER | MODIFIER_ALT,
2537 surface_opacity_binding, NULL);
2538 weston_compositor_add_binding(ec, 0, 0,
2539 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2540 MODIFIER_SUPER, zoom_binding, NULL);
2541
2542 /* configurable bindings */
2543 mod = shell->binding_modifier;
2544 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, mod,
2545 move_binding, shell);
2546 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, mod,
2547 resize_binding, shell);
2548 weston_compositor_add_binding(ec, 0, BTN_RIGHT, 0, mod,
2549 rotate_binding, NULL);
2550 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, mod,
2551 switcher_binding, shell);
2552 weston_compositor_add_binding(ec, KEY_F9, 0, 0, mod,
2553 backlight_binding, ec);
2554 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2555 backlight_binding, ec);
2556 weston_compositor_add_binding(ec, KEY_F10, 0, 0, mod,
2557 backlight_binding, ec);
2558 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2559 backlight_binding, ec);
2560 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, mod,
2561 debug_repaint_binding, shell);
2562}
2563
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002564int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002565shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002566
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002567WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002568shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002569{
Tiago Vignattibe143262012-04-16 17:31:41 +03002570 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002571
2572 shell = malloc(sizeof *shell);
2573 if (shell == NULL)
2574 return -1;
2575
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002576 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002577 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002578
2579 shell->destroy_listener.notify = shell_destroy;
2580 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2581 shell->lock_listener.notify = lock;
2582 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2583 shell->unlock_listener.notify = unlock;
2584 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002585 ec->ping_handler = ping_handler;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002586 ec->shell_interface.create_shell_surface = create_shell_surface;
2587 ec->shell_interface.set_toplevel = set_toplevel;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002588
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002589 wl_list_init(&shell->backgrounds);
2590 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002591 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002592
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002593 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2594 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2595 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2596 weston_layer_init(&shell->background_layer,
2597 &shell->toplevel_layer.link);
2598 wl_list_init(&shell->lock_layer.surface_list);
2599
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002600 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002601
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002602 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2603 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002604 return -1;
2605
Kristian Høgsberg75840622011-09-06 13:48:16 -04002606 if (wl_display_add_global(ec->wl_display,
2607 &desktop_shell_interface,
2608 shell, bind_desktop_shell) == NULL)
2609 return -1;
2610
Pekka Paalanen6e168112011-11-24 11:34:05 +02002611 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2612 shell, bind_screensaver) == NULL)
2613 return -1;
2614
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002615 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002616 if (launch_desktop_shell_process(shell) != 0)
2617 return -1;
2618
Tiago Vignatti0b52d482012-04-20 18:54:25 +03002619 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002620
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002621 return 0;
2622}