blob: e5870b47a6999d1e40b03ca972d5f1828afc059e [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
Pekka Paalanen068ae942011-11-28 14:11:15 +020039struct shell_surface;
40
Tiago Vignattibe143262012-04-16 17:31:41 +030041struct desktop_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050042 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040043
44 struct wl_listener lock_listener;
45 struct wl_listener unlock_listener;
46 struct wl_listener destroy_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020047
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050048 struct weston_layer fullscreen_layer;
49 struct weston_layer panel_layer;
50 struct weston_layer toplevel_layer;
51 struct weston_layer background_layer;
52 struct weston_layer lock_layer;
53
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020054 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050055 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020056 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020057 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020058
59 unsigned deathcount;
60 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020061 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020062
63 bool locked;
64 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020065
Pekka Paalanen068ae942011-11-28 14:11:15 +020066 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050067 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010068
69 struct wl_list backgrounds;
70 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020071
Pekka Paalanen77346a62011-11-30 16:26:35 +020072 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020073 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020074 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020075 struct wl_resource *binding;
76 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050077 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020078 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050079
80 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040081};
82
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050083enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020084 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050085
Pekka Paalanen57da4a82011-11-23 16:42:16 +020086 SHELL_SURFACE_PANEL,
87 SHELL_SURFACE_BACKGROUND,
88 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020089 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050090
91 SHELL_SURFACE_TOPLEVEL,
92 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050093 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +080094 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050095 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +020096};
97
Scott Moreauff1db4a2012-04-17 19:06:18 -060098struct ping_timer {
99 struct wl_event_source *source;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600100 uint32_t serial;
101};
102
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200103struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200104 struct wl_resource resource;
105
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500106 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200107 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500108 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300109 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200110
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500111 enum shell_surface_type type;
112 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800113 bool saved_position_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600114 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100115
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500116 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200117 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500118 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200119 } rotation;
120
121 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700122 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500123 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200124 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500125 int32_t initial_up;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400126 struct wl_input_device *device;
127 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500128 } popup;
129
Alex Wu4539b082012-03-01 12:57:46 +0800130 struct {
131 enum wl_shell_surface_fullscreen_method type;
132 struct weston_transform transform; /* matrix from x, y */
133 uint32_t framerate;
134 struct weston_surface *black_surface;
135 } fullscreen;
136
Scott Moreauff1db4a2012-04-17 19:06:18 -0600137 struct ping_timer *ping_timer;
138
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600139 struct {
140 struct weston_animation current;
141 int exists;
142 int fading_in;
143 uint32_t timestamp;
144 } unresponsive_animation;
145
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500146 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500147 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100148 struct wl_list link;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300149
150 int force_configure;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200151};
152
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300153struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700154 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300155 struct shell_surface *shsurf;
156 struct wl_listener shsurf_destroy_listener;
157};
158
159struct weston_move_grab {
160 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500161 int32_t dx, dy;
162};
163
Pekka Paalanen460099f2012-01-20 16:48:25 +0200164struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300165 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500166 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200167 struct {
168 int32_t x;
169 int32_t y;
170 } center;
171};
172
Alex Wubd3354b2012-04-17 17:20:49 +0800173static struct shell_surface *
174get_shell_surface(struct weston_surface *surface);
175
176static struct desktop_shell *
177shell_surface_get_shell(struct shell_surface *shsurf);
178
179static bool
180shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
181{
182 struct desktop_shell *shell;
183 struct weston_surface *top_fs_es;
184
185 shell = shell_surface_get_shell(shsurf);
186
187 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
188 return false;
189
190 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
191 struct weston_surface,
192 layer_link);
193 return (shsurf == get_shell_surface(top_fs_es));
194}
195
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500196static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400197destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300198{
199 struct shell_grab *grab;
200
201 grab = container_of(listener, struct shell_grab,
202 shsurf_destroy_listener);
203
204 grab->shsurf = NULL;
205}
206
207static void
208shell_grab_init(struct shell_grab *grab,
209 const struct wl_pointer_grab_interface *interface,
210 struct shell_surface *shsurf)
211{
212 grab->grab.interface = interface;
213 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400214 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
215 wl_signal_add(&shsurf->resource.destroy_signal,
216 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300217
218}
219
220static void
221shell_grab_finish(struct shell_grab *grab)
222{
223 wl_list_remove(&grab->shsurf_destroy_listener.link);
224}
225
226static void
Alex Wu4539b082012-03-01 12:57:46 +0800227center_on_output(struct weston_surface *surface,
228 struct weston_output *output);
229
230static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300231shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200232{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200233 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200234 char *path = NULL;
235 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200236
237 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200238 { "path", CONFIG_KEY_STRING, &path },
239 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200240 };
241
242 struct config_section cs[] = {
243 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
244 };
245
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200246 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500247 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200248 free(config_file);
249
Pekka Paalanen7296e792011-12-07 16:22:00 +0200250 shell->screensaver.path = path;
251 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200252}
253
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200254static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400255noop_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500256 struct wl_surface *surface, int32_t x, int32_t y)
257{
258 grab->focus = NULL;
259}
260
261static void
Scott Moreau447013d2012-02-18 05:05:29 -0700262move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500263 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500264{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500265 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500266 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300267 struct shell_surface *shsurf = move->base.shsurf;
268 struct weston_surface *es;
269
270 if (!shsurf)
271 return;
272
273 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500274
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500275 weston_surface_configure(es,
276 device->x + move->dx,
277 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200278 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500279}
280
281static void
Scott Moreau447013d2012-02-18 05:05:29 -0700282move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400283 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500284{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300285 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
286 grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500287 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500288
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500289 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300290 shell_grab_finish(shell_grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400291 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500292 free(grab);
293 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500294}
295
Scott Moreau447013d2012-02-18 05:05:29 -0700296static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500297 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500298 move_grab_motion,
299 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500300};
301
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600302static void
303unresponsive_surface_fade(struct shell_surface *shsurf, bool reverse)
304{
305 shsurf->unresponsive_animation.fading_in = reverse ? 0 : 1;
306
307 if(!shsurf->unresponsive_animation.exists) {
308 wl_list_insert(&shsurf->surface->compositor->animation_list,
309 &shsurf->unresponsive_animation.current.link);
310 shsurf->unresponsive_animation.exists = 1;
311 shsurf->unresponsive_animation.timestamp = weston_compositor_get_time();
312 weston_surface_damage(shsurf->surface);
313 }
314}
315
316static void
Scott Moreau9521d5e2012-04-19 13:06:17 -0600317unresponsive_fade_frame(struct weston_animation *animation,
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600318 struct weston_output *output, uint32_t msecs)
319{
320 struct shell_surface *shsurf =
321 container_of(animation, struct shell_surface, unresponsive_animation.current);
322 struct weston_surface *surface = shsurf->surface;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600323 unsigned int step = 8;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600324
325 if (!surface || !shsurf)
326 return;
327
328 if (shsurf->unresponsive_animation.fading_in) {
329 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
330 if (surface->saturation > 1)
331 surface->saturation -= 5;
332 if (surface->brightness > 200)
333 surface->brightness--;
334
335 shsurf->unresponsive_animation.timestamp += step;
336 }
337
338 if (surface->saturation <= 1 && surface->brightness <= 200) {
339 wl_list_remove(&shsurf->unresponsive_animation.current.link);
340 shsurf->unresponsive_animation.exists = 0;
341 }
342 }
343 else {
344 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
345 if (surface->saturation < 255)
346 surface->saturation += 5;
347 if (surface->brightness < 255)
348 surface->brightness++;
349
350 shsurf->unresponsive_animation.timestamp += step;
351 }
352
353 if (surface->saturation >= 255 && surface->brightness >= 255) {
354 surface->saturation = surface->brightness = 255;
355 wl_list_remove(&shsurf->unresponsive_animation.current.link);
356 shsurf->unresponsive_animation.exists = 0;
357 }
358 }
359
360 surface->geometry.dirty = 1;
361 weston_surface_damage(surface);
362}
363
Scott Moreau9521d5e2012-04-19 13:06:17 -0600364static void
365ping_timer_destroy(struct shell_surface *shsurf)
366{
367 if (!shsurf || !shsurf->ping_timer)
368 return;
369
370 if (shsurf->ping_timer->source)
371 wl_event_source_remove(shsurf->ping_timer->source);
372
373 free(shsurf->ping_timer);
374 shsurf->ping_timer = NULL;
375}
376
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400377static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600378ping_timeout_handler(void *data)
379{
380 struct shell_surface *shsurf = data;
381
Scott Moreau9521d5e2012-04-19 13:06:17 -0600382 /* Client is not responding */
383 shsurf->unresponsive = 1;
384 unresponsive_surface_fade(shsurf, false);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600385
386 return 1;
387}
388
389static void
390ping_handler(struct weston_surface *surface, uint32_t serial)
391{
392 struct shell_surface *shsurf;
393 shsurf = get_shell_surface(surface);
394 struct wl_event_loop *loop;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600395 int ping_timeout = 2500;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600396
397 if (!shsurf)
398 return;
399
400 if (!shsurf->ping_timer) {
401 shsurf->ping_timer = malloc(sizeof shsurf->ping_timer);
402 if (!shsurf->ping_timer)
403 return;
404
405 shsurf->ping_timer->serial = serial;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600406 loop = wl_display_get_event_loop(surface->compositor->wl_display);
407 shsurf->ping_timer->source =
408 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
409 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
410
411 wl_shell_surface_send_ping(&shsurf->resource, serial);
412 }
413}
414
415static void
416shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
417 uint32_t serial)
418{
419 struct shell_surface *shsurf = resource->data;
420
Scott Moreauff1db4a2012-04-17 19:06:18 -0600421 if (shsurf->ping_timer->serial == serial) {
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600422 if (shsurf->unresponsive) {
423 /* Received pong from previously unresponsive client */
424 unresponsive_surface_fade(shsurf, true);
425 }
Scott Moreauff1db4a2012-04-17 19:06:18 -0600426 shsurf->unresponsive = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -0600427 ping_timer_destroy(shsurf);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600428 }
429}
430
431static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500432weston_surface_move(struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400433 struct weston_input_device *wd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500434{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500435 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300436 struct shell_surface *shsurf = get_shell_surface(es);
437
438 if (!shsurf)
439 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500440
441 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400442 if (!move)
443 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500444
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300445 shell_grab_init(&move->base, &move_grab_interface, shsurf);
446
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200447 move->dx = es->geometry.x - wd->input_device.grab_x;
448 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500449
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300450 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400451 &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500452
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400453 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400454
455 return 0;
456}
457
458static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200459shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400460 struct wl_resource *input_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400461{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500462 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200463 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400464
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500465 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400466 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500467 wd->input_device.pointer_focus != &shsurf->surface->surface)
468 return;
469
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400470 if (weston_surface_move(shsurf->surface, wd) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400471 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500472}
473
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500474struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300475 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500476 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200477 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500478};
479
480static void
Scott Moreau447013d2012-02-18 05:05:29 -0700481resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500482 uint32_t time, int32_t x, int32_t y)
483{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500484 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500485 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500486 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200487 int32_t from_x, from_y;
488 int32_t to_x, to_y;
489
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300490 if (!resize->base.shsurf)
491 return;
492
493 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200494 device->grab_x, device->grab_y,
495 &from_x, &from_y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300496 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200497 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500498
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200499 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200500 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200501 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200502 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500503 } else {
504 width = resize->width;
505 }
506
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200507 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200508 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200509 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200510 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500511 } else {
512 height = resize->height;
513 }
514
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300515 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400516 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500517}
518
519static void
Scott Moreau447013d2012-02-18 05:05:29 -0700520resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400521 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500522{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300523 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500524 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500525
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500526 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300527 shell_grab_finish(&resize->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400528 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500529 free(grab);
530 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500531}
532
Scott Moreau447013d2012-02-18 05:05:29 -0700533static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500534 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500535 resize_grab_motion,
536 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500537};
538
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400539static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500540weston_surface_resize(struct shell_surface *shsurf,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400541 struct weston_input_device *wd, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500542{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500543 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500544
Alex Wu4539b082012-03-01 12:57:46 +0800545 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
546 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500547
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200548 if (edges == 0 || edges > 15 ||
549 (edges & 3) == 3 || (edges & 12) == 12)
550 return 0;
551
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500552 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400553 if (!resize)
554 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500555
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300556 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
557
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500558 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200559 resize->width = shsurf->surface->geometry.width;
560 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400561
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300562 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400563 &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500564
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400565 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400566
567 return 0;
568}
569
570static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200571shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400572 struct wl_resource *input_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200573 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400574{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500575 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200576 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400577
Alex Wu4539b082012-03-01 12:57:46 +0800578 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
579 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400580
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500581 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400582 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500583 wd->input_device.pointer_focus != &shsurf->surface->surface)
584 return;
585
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400586 if (weston_surface_resize(shsurf, wd, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400587 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500588}
589
Juan Zhao96879df2012-02-07 08:45:41 +0800590static struct weston_output *
591get_default_output(struct weston_compositor *compositor)
592{
593 return container_of(compositor->output_list.next,
594 struct weston_output, link);
595}
596
Alex Wu4539b082012-03-01 12:57:46 +0800597static void
598shell_unset_fullscreen(struct shell_surface *shsurf)
599{
600 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800601 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
602 shell_surface_is_top_fullscreen(shsurf)) {
603 weston_output_switch_mode(shsurf->fullscreen_output,
604 shsurf->fullscreen_output->origin);
605 }
Alex Wu4539b082012-03-01 12:57:46 +0800606 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
607 shsurf->fullscreen.framerate = 0;
608 wl_list_remove(&shsurf->fullscreen.transform.link);
609 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800610 if (shsurf->fullscreen.black_surface)
611 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800612 shsurf->fullscreen.black_surface = NULL;
613 shsurf->fullscreen_output = NULL;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300614 shsurf->force_configure = 1;
Alex Wu4539b082012-03-01 12:57:46 +0800615 weston_surface_set_position(shsurf->surface,
616 shsurf->saved_x, shsurf->saved_y);
617}
618
Pekka Paalanen98262232011-12-01 10:42:22 +0200619static int
620reset_shell_surface_type(struct shell_surface *surface)
621{
622 switch (surface->type) {
623 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800624 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200625 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800626 case SHELL_SURFACE_MAXIMIZED:
627 surface->output = get_default_output(surface->surface->compositor);
628 weston_surface_set_position(surface->surface,
629 surface->saved_x,
630 surface->saved_y);
631 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200632 case SHELL_SURFACE_PANEL:
633 case SHELL_SURFACE_BACKGROUND:
634 wl_list_remove(&surface->link);
635 wl_list_init(&surface->link);
636 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200637 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200638 case SHELL_SURFACE_LOCK:
639 wl_resource_post_error(&surface->resource,
640 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200641 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200642 return -1;
643 case SHELL_SURFACE_NONE:
644 case SHELL_SURFACE_TOPLEVEL:
645 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500646 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200647 break;
648 }
649
650 surface->type = SHELL_SURFACE_NONE;
651 return 0;
652}
653
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500654static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200655shell_surface_set_toplevel(struct wl_client *client,
656 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400657
658{
Pekka Paalanen98262232011-12-01 10:42:22 +0200659 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400660
Pekka Paalanen98262232011-12-01 10:42:22 +0200661 if (reset_shell_surface_type(surface))
662 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400663
Pekka Paalanen98262232011-12-01 10:42:22 +0200664 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400665}
666
667static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200668shell_surface_set_transient(struct wl_client *client,
669 struct wl_resource *resource,
670 struct wl_resource *parent_resource,
671 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400672{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200673 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500674 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200675 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500676 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400677
Pekka Paalanen98262232011-12-01 10:42:22 +0200678 if (reset_shell_surface_type(shsurf))
679 return;
680
Alex Wu4539b082012-03-01 12:57:46 +0800681 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800682 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200683 weston_surface_set_position(es, pes->geometry.x + x,
684 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400685
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200686 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400687}
688
Tiago Vignattibe143262012-04-16 17:31:41 +0300689static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800690shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200691{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400692 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800693}
694
695static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300696get_output_panel_height(struct desktop_shell *shell,
697 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800698{
699 struct shell_surface *priv;
700 int panel_height = 0;
701
702 if (!output)
703 return 0;
704
Tiago Vignattibe143262012-04-16 17:31:41 +0300705 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800706 if (priv->output == output) {
707 panel_height = priv->surface->geometry.height;
708 break;
709 }
710 }
711 return panel_height;
712}
713
714static void
715shell_surface_set_maximized(struct wl_client *client,
716 struct wl_resource *resource,
717 struct wl_resource *output_resource )
718{
719 struct shell_surface *shsurf = resource->data;
720 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300721 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800722 uint32_t edges = 0, panel_height = 0;
723
724 /* get the default output, if the client set it as NULL
725 check whether the ouput is available */
726 if (output_resource)
727 shsurf->output = output_resource->data;
728 else
729 shsurf->output = get_default_output(es->compositor);
730
731 if (reset_shell_surface_type(shsurf))
732 return;
733
734 shsurf->saved_x = es->geometry.x;
735 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800736 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800737
Tiago Vignattibe143262012-04-16 17:31:41 +0300738 shell = shell_surface_get_shell(shsurf);
739 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800740 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500741
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400742 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500743 es->output->current->width,
744 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800745
746 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200747}
748
Alex Wu21858432012-04-01 20:13:08 +0800749static void
750black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
751
Alex Wu4539b082012-03-01 12:57:46 +0800752static struct weston_surface *
753create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800754 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800755 GLfloat x, GLfloat y, int w, int h)
756{
757 struct weston_surface *surface = NULL;
758
759 surface = weston_surface_create(ec);
760 if (surface == NULL) {
761 fprintf(stderr, "no memory\n");
762 return NULL;
763 }
764
Alex Wu21858432012-04-01 20:13:08 +0800765 surface->configure = black_surface_configure;
766 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800767 weston_surface_configure(surface, x, y, w, h);
768 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
769 return surface;
770}
771
772/* Create black surface and append it to the associated fullscreen surface.
773 * Handle size dismatch and positioning according to the method. */
774static void
775shell_configure_fullscreen(struct shell_surface *shsurf)
776{
777 struct weston_output *output = shsurf->fullscreen_output;
778 struct weston_surface *surface = shsurf->surface;
779 struct weston_matrix *matrix;
780 float scale;
781
782 center_on_output(surface, output);
783
784 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500785 shsurf->fullscreen.black_surface =
786 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800787 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500788 output->x, output->y,
789 output->current->width,
790 output->current->height);
791
792 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
793 wl_list_insert(&surface->layer_link,
794 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800795 shsurf->fullscreen.black_surface->output = output;
796
797 switch (shsurf->fullscreen.type) {
798 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
799 break;
800 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
801 matrix = &shsurf->fullscreen.transform.matrix;
802 weston_matrix_init(matrix);
803 scale = (float)output->current->width/(float)surface->geometry.width;
804 weston_matrix_scale(matrix, scale, scale, 1);
805 wl_list_remove(&shsurf->fullscreen.transform.link);
806 wl_list_insert(surface->geometry.transformation_list.prev,
807 &shsurf->fullscreen.transform.link);
808 weston_surface_set_position(surface, output->x, output->y);
809 break;
810 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +0800811 if (shell_surface_is_top_fullscreen(shsurf)) {
812 struct weston_mode mode = {0,
813 surface->geometry.width,
814 surface->geometry.height,
815 shsurf->fullscreen.framerate};
816
817 if (weston_output_switch_mode(output, &mode) == 0) {
818 weston_surface_configure(shsurf->fullscreen.black_surface,
819 output->x, output->y,
820 output->current->width,
821 output->current->height);
822 weston_surface_set_position(surface, output->x, output->y);
823 break;
824 }
825 }
Alex Wu4539b082012-03-01 12:57:46 +0800826 break;
827 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
828 break;
829 default:
830 break;
831 }
832}
833
834/* make the fullscreen and black surface at the top */
835static void
836shell_stack_fullscreen(struct shell_surface *shsurf)
837{
Alex Wubd3354b2012-04-17 17:20:49 +0800838 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +0800839 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300840 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800841
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500842 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500843 wl_list_insert(&shell->fullscreen_layer.surface_list,
844 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +0800845 weston_surface_damage(surface);
846
847 if (!shsurf->fullscreen.black_surface)
848 shsurf->fullscreen.black_surface =
849 create_black_surface(surface->compositor,
850 surface,
851 output->x, output->y,
852 output->current->width,
853 output->current->height);
854
855 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500856 wl_list_insert(&surface->layer_link,
857 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500858 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800859}
860
861static void
862shell_map_fullscreen(struct shell_surface *shsurf)
863{
Alex Wu4539b082012-03-01 12:57:46 +0800864 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800865 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800866}
867
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400868static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200869shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500870 struct wl_resource *resource,
871 uint32_t method,
872 uint32_t framerate,
873 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400874{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200875 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500876 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800877
878 if (output_resource)
879 shsurf->output = output_resource->data;
880 else
881 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400882
Pekka Paalanen98262232011-12-01 10:42:22 +0200883 if (reset_shell_surface_type(shsurf))
884 return;
885
Alex Wu4539b082012-03-01 12:57:46 +0800886 shsurf->fullscreen_output = shsurf->output;
887 shsurf->fullscreen.type = method;
888 shsurf->fullscreen.framerate = framerate;
889 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400890
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200891 shsurf->saved_x = es->geometry.x;
892 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800893 shsurf->saved_position_valid = true;
894
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300895 if (weston_surface_is_mapped(es))
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300896 shsurf->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500897
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400898 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500899 shsurf->output->current->width,
900 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400901}
902
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500903static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400904popup_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500905 struct wl_surface *surface, int32_t x, int32_t y)
906{
907 struct wl_input_device *device = grab->input_device;
908 struct shell_surface *priv =
909 container_of(grab, struct shell_surface, popup.grab);
910 struct wl_client *client = priv->surface->surface.resource.client;
911
Pekka Paalanencb108432012-01-19 16:25:40 +0200912 if (surface && surface->resource.client == client) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400913 wl_input_device_set_pointer_focus(device, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500914 grab->focus = surface;
915 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400916 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500917 grab->focus = NULL;
918 }
919}
920
921static void
Scott Moreau447013d2012-02-18 05:05:29 -0700922popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200923 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500924{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500925 struct wl_resource *resource;
926
927 resource = grab->input_device->pointer_focus_resource;
928 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500929 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500930}
931
932static void
Scott Moreau447013d2012-02-18 05:05:29 -0700933popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400934 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500935{
936 struct wl_resource *resource;
937 struct shell_surface *shsurf =
938 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400939 struct wl_display *display;
940 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500941
942 resource = grab->input_device->pointer_focus_resource;
943 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400944 display = wl_client_get_display(resource->client);
945 serial = wl_display_get_serial(display);
946 wl_input_device_send_button(resource, serial,
947 time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500948 } else if (state == 0 &&
949 (shsurf->popup.initial_up ||
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400950 time - shsurf->popup.device->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500951 wl_shell_surface_send_popup_done(&shsurf->resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400952 wl_input_device_end_pointer_grab(grab->input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500953 shsurf->popup.grab.input_device = NULL;
954 }
955
956 if (state == 0)
957 shsurf->popup.initial_up = 1;
958}
959
Scott Moreau447013d2012-02-18 05:05:29 -0700960static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500961 popup_grab_focus,
962 popup_grab_motion,
963 popup_grab_button,
964};
965
966static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400967shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500968{
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400969 struct wl_input_device *device = shsurf->popup.device;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500970 struct weston_surface *es = shsurf->surface;
971 struct weston_surface *parent = shsurf->parent->surface;
972
973 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500974 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500975
Pekka Paalanen938269a2012-02-07 14:19:01 +0200976 weston_surface_update_transform(parent);
977 if (parent->transform.enabled) {
978 shsurf->popup.parent_transform.matrix =
979 parent->transform.matrix;
980 } else {
981 /* construct x, y translation matrix */
982 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
983 shsurf->popup.parent_transform.matrix.d[12] =
984 parent->geometry.x;
985 shsurf->popup.parent_transform.matrix.d[13] =
986 parent->geometry.y;
987 }
988 wl_list_insert(es->geometry.transformation_list.prev,
989 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200990 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500991
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500992 shsurf->popup.initial_up = 0;
993
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400994 /* We don't require the grab to still be active, but if another
995 * grab has started in the meantime, we end the popup now. */
996 if (device->grab_serial == shsurf->popup.serial) {
997 wl_input_device_start_pointer_grab(device,
998 &shsurf->popup.grab);
999 } else {
1000 wl_shell_surface_send_popup_done(&shsurf->resource);
1001 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001002}
1003
1004static void
1005shell_surface_set_popup(struct wl_client *client,
1006 struct wl_resource *resource,
1007 struct wl_resource *input_device_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001008 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001009 struct wl_resource *parent_resource,
1010 int32_t x, int32_t y, uint32_t flags)
1011{
1012 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001013
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001014 shsurf->type = SHELL_SURFACE_POPUP;
1015 shsurf->parent = parent_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001016 shsurf->popup.device = input_device_resource->data;
1017 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001018 shsurf->popup.x = x;
1019 shsurf->popup.y = y;
1020}
1021
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001022static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001023 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001024 shell_surface_move,
1025 shell_surface_resize,
1026 shell_surface_set_toplevel,
1027 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001028 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001029 shell_surface_set_popup,
1030 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001031};
1032
1033static void
1034destroy_shell_surface(struct wl_resource *resource)
1035{
1036 struct shell_surface *shsurf = resource->data;
1037
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001038 if (shsurf->popup.grab.input_device)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001039 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001040
Alex Wubd3354b2012-04-17 17:20:49 +08001041 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1042 shell_surface_is_top_fullscreen(shsurf)) {
1043 weston_output_switch_mode(shsurf->fullscreen_output,
1044 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001045 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001046
Alex Wuaa08e2d2012-03-05 11:01:40 +08001047 if (shsurf->fullscreen.black_surface)
1048 weston_surface_destroy(shsurf->fullscreen.black_surface);
1049
Alex Wubd3354b2012-04-17 17:20:49 +08001050 /* As destroy_resource() use wl_list_for_each_safe(),
1051 * we can always remove the listener.
1052 */
1053 wl_list_remove(&shsurf->surface_destroy_listener.link);
1054 shsurf->surface->configure = NULL;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001055 ping_timer_destroy(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +08001056
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001057 wl_list_remove(&shsurf->link);
1058 free(shsurf);
1059}
1060
1061static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001062shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001063{
1064 struct shell_surface *shsurf = container_of(listener,
1065 struct shell_surface,
1066 surface_destroy_listener);
1067
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001068 wl_resource_destroy(&shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001069}
1070
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001071static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001072get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001073{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001074 struct wl_listener *listener;
1075
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001076 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1077 shell_handle_surface_destroy);
1078 if (listener)
1079 return container_of(listener, struct shell_surface,
1080 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001081
1082 return NULL;
1083}
1084
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001085static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001086shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1087
1088static void
Pekka Paalanen46229672011-11-29 15:49:31 +02001089shell_get_shell_surface(struct wl_client *client,
1090 struct wl_resource *resource,
1091 uint32_t id,
1092 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001093{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001094 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001095 struct shell_surface *shsurf;
1096
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +02001097 if (get_shell_surface(surface)) {
1098 wl_resource_post_error(surface_resource,
1099 WL_DISPLAY_ERROR_INVALID_OBJECT,
Alex Wubd3354b2012-04-17 17:20:49 +08001100 "desktop_shell::get_shell_surface already requested");
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +02001101 return;
1102 }
1103
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001104 if (surface->configure) {
1105 wl_resource_post_error(surface_resource,
1106 WL_DISPLAY_ERROR_INVALID_OBJECT,
1107 "surface->configure already set");
1108 return;
1109 }
1110
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001111 shsurf = calloc(1, sizeof *shsurf);
1112 if (!shsurf) {
1113 wl_resource_post_no_memory(resource);
1114 return;
1115 }
1116
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001117 surface->configure = shell_surface_configure;
1118
Scott Moreauff1db4a2012-04-17 19:06:18 -06001119 shsurf->unresponsive = 0;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001120 shsurf->unresponsive_animation.exists = 0;
1121 shsurf->unresponsive_animation.fading_in = 0;
Scott Moreau9521d5e2012-04-19 13:06:17 -06001122 shsurf->unresponsive_animation.current.frame = unresponsive_fade_frame;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001123
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001124 shsurf->resource.destroy = destroy_shell_surface;
1125 shsurf->resource.object.id = id;
1126 shsurf->resource.object.interface = &wl_shell_surface_interface;
1127 shsurf->resource.object.implementation =
1128 (void (**)(void)) &shell_surface_implementation;
1129 shsurf->resource.data = shsurf;
1130
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001131 shsurf->shell = resource->data;
Alex Wu4539b082012-03-01 12:57:46 +08001132 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001133 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001134 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1135 shsurf->fullscreen.framerate = 0;
1136 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001137 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001138 wl_list_init(&shsurf->fullscreen.transform.link);
1139
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001140 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1141 wl_signal_add(&surface->surface.resource.destroy_signal,
1142 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001143
1144 /* init link so its safe to always remove it in destroy_shell_surface */
1145 wl_list_init(&shsurf->link);
1146
Pekka Paalanen460099f2012-01-20 16:48:25 +02001147 /* empty when not in use */
1148 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001149 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001150
Pekka Paalanen98262232011-12-01 10:42:22 +02001151 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001152
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001153 wl_client_add_resource(client, &shsurf->resource);
1154}
1155
1156static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001157 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001158};
1159
Kristian Høgsberg07937562011-04-12 17:25:42 -04001160static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001161handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001162{
1163 proc->pid = 0;
1164}
1165
1166static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001167launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001168{
1169 if (shell->screensaver.binding)
1170 return;
1171
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001172 if (!shell->screensaver.path)
1173 return;
1174
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001175 if (shell->screensaver.process.pid != 0) {
1176 fprintf(stderr, "old screensaver still running\n");
1177 return;
1178 }
1179
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001180 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001181 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001182 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001183 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001184}
1185
1186static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001187terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001188{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001189 if (shell->screensaver.process.pid == 0)
1190 return;
1191
1192 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001193}
1194
1195static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001196show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001197{
1198 struct wl_list *list;
1199
1200 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001201 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001202 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001203 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001204
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001205 wl_list_remove(&surface->surface->layer_link);
1206 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001207 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001208 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001209}
1210
1211static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001212hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001213{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001214 wl_list_remove(&surface->surface->layer_link);
1215 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001216 surface->surface->output = NULL;
1217}
1218
1219static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001220desktop_shell_set_background(struct wl_client *client,
1221 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001222 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001223 struct wl_resource *surface_resource)
1224{
Tiago Vignattibe143262012-04-16 17:31:41 +03001225 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001226 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001227 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001228 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001229
Pekka Paalanen98262232011-12-01 10:42:22 +02001230 if (reset_shell_surface_type(shsurf))
1231 return;
1232
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001233 wl_list_for_each(priv, &shell->backgrounds, link) {
1234 if (priv->output == output_resource->data) {
1235 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001236 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001237 wl_list_remove(&priv->link);
1238 break;
1239 }
1240 }
1241
Pekka Paalanen068ae942011-11-28 14:11:15 +02001242 shsurf->type = SHELL_SURFACE_BACKGROUND;
1243 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001244
Pekka Paalanen068ae942011-11-28 14:11:15 +02001245 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001246
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001247 weston_surface_set_position(surface, shsurf->output->x,
1248 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001249
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001250 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001251 surface_resource,
1252 shsurf->output->current->width,
1253 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001254}
1255
1256static void
1257desktop_shell_set_panel(struct wl_client *client,
1258 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001259 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001260 struct wl_resource *surface_resource)
1261{
Tiago Vignattibe143262012-04-16 17:31:41 +03001262 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001263 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001264 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001265 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001266
Pekka Paalanen98262232011-12-01 10:42:22 +02001267 if (reset_shell_surface_type(shsurf))
1268 return;
1269
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001270 wl_list_for_each(priv, &shell->panels, link) {
1271 if (priv->output == output_resource->data) {
1272 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001273 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001274 wl_list_remove(&priv->link);
1275 break;
1276 }
1277 }
1278
Pekka Paalanen068ae942011-11-28 14:11:15 +02001279 shsurf->type = SHELL_SURFACE_PANEL;
1280 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001281
Pekka Paalanen068ae942011-11-28 14:11:15 +02001282 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001283
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001284 weston_surface_set_position(surface, shsurf->output->x,
1285 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001286
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001287 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001288 surface_resource,
1289 shsurf->output->current->width,
1290 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001291}
1292
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001293static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001294handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001295{
Tiago Vignattibe143262012-04-16 17:31:41 +03001296 struct desktop_shell *shell =
1297 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001298
1299 fprintf(stderr, "lock surface gone\n");
1300 shell->lock_surface = NULL;
1301}
1302
1303static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001304desktop_shell_set_lock_surface(struct wl_client *client,
1305 struct wl_resource *resource,
1306 struct wl_resource *surface_resource)
1307{
Tiago Vignattibe143262012-04-16 17:31:41 +03001308 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001309 struct shell_surface *surface = surface_resource->data;
1310
1311 if (reset_shell_surface_type(surface))
1312 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001313
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001314 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001315
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001316 if (!shell->locked)
1317 return;
1318
Pekka Paalanen98262232011-12-01 10:42:22 +02001319 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001320
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001321 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1322 wl_signal_add(&surface_resource->destroy_signal,
1323 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001324
Pekka Paalanen068ae942011-11-28 14:11:15 +02001325 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001326}
1327
1328static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001329resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001330{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001331 struct shell_surface *tmp;
1332
1333 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1334 hide_screensaver(shell, tmp);
1335
1336 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001337
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001338 wl_list_remove(&shell->lock_layer.link);
1339 wl_list_insert(&shell->compositor->cursor_layer.link,
1340 &shell->fullscreen_layer.link);
1341 wl_list_insert(&shell->fullscreen_layer.link,
1342 &shell->panel_layer.link);
1343 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001344
1345 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001346 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001347 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001348 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001349}
1350
1351static void
1352desktop_shell_unlock(struct wl_client *client,
1353 struct wl_resource *resource)
1354{
Tiago Vignattibe143262012-04-16 17:31:41 +03001355 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001356
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001357 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001358
1359 if (shell->locked)
1360 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001361}
1362
Kristian Høgsberg75840622011-09-06 13:48:16 -04001363static const struct desktop_shell_interface desktop_shell_implementation = {
1364 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001365 desktop_shell_set_panel,
1366 desktop_shell_set_lock_surface,
1367 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001368};
1369
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001370static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001371get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001372{
1373 struct shell_surface *shsurf;
1374
1375 shsurf = get_shell_surface(surface);
1376 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001377 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001378 return shsurf->type;
1379}
1380
Kristian Høgsberg75840622011-09-06 13:48:16 -04001381static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001382move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001383 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001384{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001385 struct weston_surface *surface =
1386 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001387
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001388 if (surface == NULL)
1389 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001390
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001391 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001392 case SHELL_SURFACE_PANEL:
1393 case SHELL_SURFACE_BACKGROUND:
1394 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001395 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001396 return;
1397 default:
1398 break;
1399 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001400
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001401 weston_surface_move(surface, (struct weston_input_device *) device);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001402}
1403
1404static void
1405resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001406 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001407{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001408 struct weston_surface *surface =
1409 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001410 uint32_t edges = 0;
1411 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001412 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001413
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001414 if (surface == NULL)
1415 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001416
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001417 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001418 if (!shsurf)
1419 return;
1420
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001421 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001422 case SHELL_SURFACE_PANEL:
1423 case SHELL_SURFACE_BACKGROUND:
1424 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001425 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001426 return;
1427 default:
1428 break;
1429 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001430
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001431 weston_surface_from_global(surface,
1432 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001433
Pekka Paalanen60921e52012-01-25 15:55:43 +02001434 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001435 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001436 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001437 edges |= 0;
1438 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001439 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001440
Pekka Paalanen60921e52012-01-25 15:55:43 +02001441 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001442 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001443 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001444 edges |= 0;
1445 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001446 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001447
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001448 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001449 edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001450}
1451
1452static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001453surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1454 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1455{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001456 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001457 struct shell_surface *shsurf;
1458 struct weston_surface *surface =
1459 (struct weston_surface *) device->pointer_focus;
1460
1461 if (surface == NULL)
1462 return;
1463
1464 shsurf = get_shell_surface(surface);
1465 if (!shsurf)
1466 return;
1467
1468 switch (shsurf->type) {
1469 case SHELL_SURFACE_BACKGROUND:
1470 case SHELL_SURFACE_SCREENSAVER:
1471 return;
1472 default:
1473 break;
1474 }
1475
1476 surface->alpha += value * step;
1477
1478 if (surface->alpha > 255)
1479 surface->alpha = 255;
1480 if (surface->alpha < step)
1481 surface->alpha = step;
1482
1483 surface->geometry.dirty = 1;
1484 weston_surface_damage(surface);
1485}
1486
1487static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001488zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001489 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001490{
1491 struct weston_input_device *wd = (struct weston_input_device *) device;
1492 struct weston_compositor *compositor = wd->compositor;
1493 struct weston_output *output;
1494
1495 wl_list_for_each(output, &compositor->output_list, link) {
1496 if (pixman_region32_contains_point(&output->region,
1497 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001498 output->zoom.active = 1;
1499 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001500
1501 if (output->zoom.level >= 1.0) {
1502 output->zoom.active = 0;
1503 output->zoom.level = 1.0;
1504 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001505
1506 if (output->zoom.level < output->zoom.increment)
1507 output->zoom.level = output->zoom.increment;
1508
1509 weston_output_update_zoom(output, device->x, device->y);
1510 }
1511 }
1512}
1513
Scott Moreauccbf29d2012-02-22 14:21:41 -07001514static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001515terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001516 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001517{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001518 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001519
1520 if (state)
1521 wl_display_terminate(compositor->wl_display);
1522}
1523
1524static void
Scott Moreau447013d2012-02-18 05:05:29 -07001525rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001526 uint32_t time, int32_t x, int32_t y)
1527{
1528 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001529 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001530 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001531 struct shell_surface *shsurf = rotate->base.shsurf;
1532 struct weston_surface *surface;
1533 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1534
1535 if (!shsurf)
1536 return;
1537
1538 surface = shsurf->surface;
1539
1540 cx = 0.5f * surface->geometry.width;
1541 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001542
1543 dx = device->x - rotate->center.x;
1544 dy = device->y - rotate->center.y;
1545 r = sqrtf(dx * dx + dy * dy);
1546
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001547 wl_list_remove(&shsurf->rotation.transform.link);
1548 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001549
1550 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001551 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001552 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001553
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001554 weston_matrix_init(&rotate->rotation);
1555 rotate->rotation.d[0] = dx / r;
1556 rotate->rotation.d[4] = -dy / r;
1557 rotate->rotation.d[1] = -rotate->rotation.d[4];
1558 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001559
1560 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001561 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001562 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001563 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001564 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001565
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001566 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001567 &shsurf->surface->geometry.transformation_list,
1568 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001569 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001570 wl_list_init(&shsurf->rotation.transform.link);
1571 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001572 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001573 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001574
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001575 /* We need to adjust the position of the surface
1576 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001577 cposx = surface->geometry.x + cx;
1578 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001579 dposx = rotate->center.x - cposx;
1580 dposy = rotate->center.y - cposy;
1581 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001582 weston_surface_set_position(surface,
1583 surface->geometry.x + dposx,
1584 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001585 }
1586
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001587 /* Repaint implies weston_surface_update_transform(), which
1588 * lazily applies the damage due to rotation update.
1589 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001590 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001591}
1592
1593static void
Scott Moreau447013d2012-02-18 05:05:29 -07001594rotate_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001595 uint32_t time, uint32_t button, int32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001596{
1597 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001598 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001599 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001600 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001601
1602 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001603 if (shsurf)
1604 weston_matrix_multiply(&shsurf->rotation.rotation,
1605 &rotate->rotation);
1606 shell_grab_finish(&rotate->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001607 wl_input_device_end_pointer_grab(device);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001608 free(rotate);
1609 }
1610}
1611
Scott Moreau447013d2012-02-18 05:05:29 -07001612static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001613 noop_grab_focus,
1614 rotate_grab_motion,
1615 rotate_grab_button,
1616};
1617
1618static void
1619rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001620 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001621{
1622 struct weston_surface *base_surface =
1623 (struct weston_surface *) device->pointer_focus;
1624 struct shell_surface *surface;
1625 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001626 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001627 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001628
1629 if (base_surface == NULL)
1630 return;
1631
1632 surface = get_shell_surface(base_surface);
1633 if (!surface)
1634 return;
1635
1636 switch (surface->type) {
1637 case SHELL_SURFACE_PANEL:
1638 case SHELL_SURFACE_BACKGROUND:
1639 case SHELL_SURFACE_FULLSCREEN:
1640 case SHELL_SURFACE_SCREENSAVER:
1641 return;
1642 default:
1643 break;
1644 }
1645
Pekka Paalanen460099f2012-01-20 16:48:25 +02001646 rotate = malloc(sizeof *rotate);
1647 if (!rotate)
1648 return;
1649
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001650 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001651
1652 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001653 surface->surface->geometry.width / 2,
1654 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001655 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001656
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001657 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001658
1659 dx = device->x - rotate->center.x;
1660 dy = device->y - rotate->center.y;
1661 r = sqrtf(dx * dx + dy * dy);
1662 if (r > 20.0f) {
1663 struct weston_matrix inverse;
1664
1665 weston_matrix_init(&inverse);
1666 inverse.d[0] = dx / r;
1667 inverse.d[4] = dy / r;
1668 inverse.d[1] = -inverse.d[4];
1669 inverse.d[5] = inverse.d[0];
1670 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001671
1672 weston_matrix_init(&rotate->rotation);
1673 rotate->rotation.d[0] = dx / r;
1674 rotate->rotation.d[4] = -dy / r;
1675 rotate->rotation.d[1] = -rotate->rotation.d[4];
1676 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001677 } else {
1678 weston_matrix_init(&surface->rotation.rotation);
1679 weston_matrix_init(&rotate->rotation);
1680 }
1681
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001682 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001683}
1684
1685static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001686activate(struct desktop_shell *shell, struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001687 struct weston_input_device *device)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001688{
Alex Wu21858432012-04-01 20:13:08 +08001689 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001690
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001691 weston_surface_activate(es, device);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001692
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001693 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001694 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001695 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001696 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001697 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001698
Pekka Paalanen77346a62011-11-30 16:26:35 +02001699 case SHELL_SURFACE_SCREENSAVER:
1700 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001701 if (shell->lock_surface)
1702 weston_surface_restack(es,
1703 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001704 break;
Alex Wu4539b082012-03-01 12:57:46 +08001705 case SHELL_SURFACE_FULLSCREEN:
1706 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001707 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001708 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001709 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001710 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001711 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001712 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1713 wl_list_for_each_reverse_safe(surf,
1714 prev,
1715 &shell->fullscreen_layer.surface_list,
1716 layer_link)
1717 weston_surface_restack(surf,
1718 &shell->toplevel_layer.surface_list);
1719 }
1720
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001721 weston_surface_restack(es,
1722 &shell->toplevel_layer.surface_list);
1723 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001724 }
1725}
1726
Alex Wu21858432012-04-01 20:13:08 +08001727/* no-op func for checking black surface */
1728static void
1729black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1730{
1731}
1732
1733static bool
1734is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1735{
1736 if (es->configure == black_surface_configure) {
1737 if (fs_surface)
1738 *fs_surface = (struct weston_surface *)es->private;
1739 return true;
1740 }
1741 return false;
1742}
1743
Kristian Høgsberg75840622011-09-06 13:48:16 -04001744static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001745click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001746 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001747 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001748{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001749 struct weston_input_device *wd = (struct weston_input_device *) device;
Tiago Vignattibe143262012-04-16 17:31:41 +03001750 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001751 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001752 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001753
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001754 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001755 if (!focus)
1756 return;
1757
Alex Wu21858432012-04-01 20:13:08 +08001758 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001759 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001760
Alex Wu9c35e6b2012-03-05 14:13:13 +08001761 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001762 activate(shell, focus, wd);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001763}
1764
1765static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001766lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001767{
Tiago Vignattibe143262012-04-16 17:31:41 +03001768 struct desktop_shell *shell =
1769 container_of(listener, struct desktop_shell, lock_listener);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001770 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001771 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001772 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001773
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001774 if (shell->locked) {
1775 wl_list_for_each(output, &shell->compositor->output_list, link)
1776 /* TODO: find a way to jump to other DPMS levels */
1777 if (output->set_dpms)
1778 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001779 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001780 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001781
1782 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001783
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001784 /* Hide all surfaces by removing the fullscreen, panel and
1785 * toplevel layers. This way nothing else can show or receive
1786 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001787
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001788 wl_list_remove(&shell->panel_layer.link);
1789 wl_list_remove(&shell->toplevel_layer.link);
1790 wl_list_remove(&shell->fullscreen_layer.link);
1791 wl_list_insert(&shell->compositor->cursor_layer.link,
1792 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001793
Pekka Paalanen77346a62011-11-30 16:26:35 +02001794 launch_screensaver(shell);
1795
1796 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1797 show_screensaver(shell, shsurf);
1798
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001799 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001800 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001801 weston_compositor_wake(shell->compositor);
1802 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001803 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001804
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001805 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001806 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001807
1808 /* reset keyboard foci */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001809 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1810 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001811 NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001812 }
1813
1814 /* TODO: disable bindings that should not work while locked. */
1815
1816 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001817}
1818
1819static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001820unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001821{
Tiago Vignattibe143262012-04-16 17:31:41 +03001822 struct desktop_shell *shell =
1823 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001824
Pekka Paalanend81c2162011-11-16 13:47:34 +02001825 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001826 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001827 return;
1828 }
1829
1830 /* If desktop-shell client has gone away, unlock immediately. */
1831 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001832 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001833 return;
1834 }
1835
1836 if (shell->prepare_event_sent)
1837 return;
1838
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001839 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001840 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001841}
1842
1843static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001844center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001845{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001846 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001847 GLfloat x = (mode->width - surface->geometry.width) / 2;
1848 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001849
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001850 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001851}
1852
1853static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001854map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001855 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001856{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001857 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001858 struct shell_surface *shsurf;
1859 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001860 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001861 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001862
Pekka Paalanen77346a62011-11-30 16:26:35 +02001863 shsurf = get_shell_surface(surface);
1864 if (shsurf)
1865 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001866
Pekka Paalanen60921e52012-01-25 15:55:43 +02001867 surface->geometry.width = width;
1868 surface->geometry.height = height;
1869 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001870
1871 /* initial positioning, see also configure() */
1872 switch (surface_type) {
1873 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001874 weston_surface_set_position(surface, 10 + random() % 400,
1875 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001876 break;
1877 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001878 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001879 break;
Alex Wu4539b082012-03-01 12:57:46 +08001880 case SHELL_SURFACE_FULLSCREEN:
1881 shell_map_fullscreen(shsurf);
1882 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001883 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001884 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001885 panel_height = get_output_panel_height(shell,surface->output);
1886 weston_surface_set_position(surface, surface->output->x,
1887 surface->output->y + panel_height);
1888 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001889 case SHELL_SURFACE_LOCK:
1890 center_on_output(surface, get_default_output(compositor));
1891 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001892 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001893 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001894 case SHELL_SURFACE_NONE:
1895 weston_surface_set_position(surface,
1896 surface->geometry.x + sx,
1897 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001898 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001899 default:
1900 ;
1901 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001902
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001903 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001904 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001905 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001906 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001907 wl_list_insert(&shell->background_layer.surface_list,
1908 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001909 break;
1910 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001911 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001912 wl_list_insert(&shell->panel_layer.surface_list,
1913 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001914 break;
1915 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001916 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001917 wl_list_insert(&shell->lock_layer.surface_list,
1918 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001919 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001920 break;
1921 case SHELL_SURFACE_SCREENSAVER:
1922 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001923 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001924 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001925 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001926 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001927 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001928 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001929 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001930 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001931 case SHELL_SURFACE_POPUP:
1932 case SHELL_SURFACE_TRANSIENT:
1933 parent = shsurf->parent->surface;
1934 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1935 break;
Alex Wu4539b082012-03-01 12:57:46 +08001936 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001937 case SHELL_SURFACE_NONE:
1938 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001939 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001940 wl_list_insert(&shell->toplevel_layer.surface_list,
1941 &surface->layer_link);
1942 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001943 }
1944
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001945 if (surface_type != SHELL_SURFACE_NONE) {
1946 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001947 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1948 surface->output = shsurf->output;
1949 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001950
Juan Zhao7bb92f02011-12-15 11:31:51 -05001951 switch (surface_type) {
1952 case SHELL_SURFACE_TOPLEVEL:
1953 case SHELL_SURFACE_TRANSIENT:
1954 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001955 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001956 if (!shell->locked)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001957 activate(shell, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001958 (struct weston_input_device *)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001959 compositor->input_device);
Juan Zhao7bb92f02011-12-15 11:31:51 -05001960 break;
1961 default:
1962 break;
1963 }
1964
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001965 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001966 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001967}
1968
1969static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001970configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001971 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001972{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001973 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1974 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001975
Pekka Paalanen77346a62011-11-30 16:26:35 +02001976 shsurf = get_shell_surface(surface);
1977 if (shsurf)
1978 surface_type = shsurf->type;
1979
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001980 surface->geometry.x = x;
1981 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001982 surface->geometry.width = width;
1983 surface->geometry.height = height;
1984 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001985
1986 switch (surface_type) {
1987 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001988 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001989 break;
Alex Wu4539b082012-03-01 12:57:46 +08001990 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08001991 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001992 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001993 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001994 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001995 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001996 surface->geometry.x = surface->output->x;
1997 surface->geometry.y = surface->output->y +
1998 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001999 break;
Alex Wu4539b082012-03-01 12:57:46 +08002000 case SHELL_SURFACE_TOPLEVEL:
2001 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002002 default:
2003 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002004 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002005
Alex Wu4539b082012-03-01 12:57:46 +08002006 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002007 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002008 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002009
2010 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2011 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002012 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2013 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002014 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002015}
2016
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002017static void
2018shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2019{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002020 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002021 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002022
2023 if (!weston_surface_is_mapped(es)) {
2024 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002025 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002026 es->geometry.width != es->buffer->width ||
2027 es->geometry.height != es->buffer->height) {
2028 GLfloat from_x, from_y;
2029 GLfloat to_x, to_y;
2030
2031 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2032 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2033 configure(shell, es,
2034 es->geometry.x + to_x - from_x,
2035 es->geometry.y + to_y - from_y,
2036 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002037 shsurf->force_configure = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002038 }
2039}
2040
Tiago Vignattibe143262012-04-16 17:31:41 +03002041static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002042
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002043static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002044desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002045{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002046 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002047 struct desktop_shell *shell =
2048 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002049
2050 shell->child.process.pid = 0;
2051 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002052
2053 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2054 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002055 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002056 shell->child.deathstamp = time;
2057 shell->child.deathcount = 0;
2058 }
2059
2060 shell->child.deathcount++;
2061 if (shell->child.deathcount > 5) {
2062 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2063 return;
2064 }
2065
2066 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2067 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002068}
2069
2070static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002071launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002072{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002073 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002074
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002075 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002076 &shell->child.process,
2077 shell_exe,
2078 desktop_shell_sigchld);
2079
2080 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002081 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002082 return 0;
2083}
2084
2085static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002086bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2087{
Tiago Vignattibe143262012-04-16 17:31:41 +03002088 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002089
2090 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002091 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002092}
2093
Kristian Høgsberg75840622011-09-06 13:48:16 -04002094static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002095unbind_desktop_shell(struct wl_resource *resource)
2096{
Tiago Vignattibe143262012-04-16 17:31:41 +03002097 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002098
2099 if (shell->locked)
2100 resume_desktop(shell);
2101
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002102 shell->child.desktop_shell = NULL;
2103 shell->prepare_event_sent = false;
2104 free(resource);
2105}
2106
2107static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002108bind_desktop_shell(struct wl_client *client,
2109 void *data, uint32_t version, uint32_t id)
2110{
Tiago Vignattibe143262012-04-16 17:31:41 +03002111 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002112 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002113
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002114 resource = wl_client_add_object(client, &desktop_shell_interface,
2115 &desktop_shell_implementation,
2116 id, shell);
2117
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002118 if (client == shell->child.client) {
2119 resource->destroy = unbind_desktop_shell;
2120 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002121 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002122 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002123
2124 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2125 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002126 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002127}
2128
Pekka Paalanen6e168112011-11-24 11:34:05 +02002129static void
2130screensaver_set_surface(struct wl_client *client,
2131 struct wl_resource *resource,
2132 struct wl_resource *shell_surface_resource,
2133 struct wl_resource *output_resource)
2134{
Tiago Vignattibe143262012-04-16 17:31:41 +03002135 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002136 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002137 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002138
Pekka Paalanen98262232011-12-01 10:42:22 +02002139 if (reset_shell_surface_type(surface))
2140 return;
2141
Pekka Paalanen77346a62011-11-30 16:26:35 +02002142 surface->type = SHELL_SURFACE_SCREENSAVER;
2143
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002144 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002145 surface->output = output;
2146 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002147}
2148
2149static const struct screensaver_interface screensaver_implementation = {
2150 screensaver_set_surface
2151};
2152
2153static void
2154unbind_screensaver(struct wl_resource *resource)
2155{
Tiago Vignattibe143262012-04-16 17:31:41 +03002156 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002157
Pekka Paalanen77346a62011-11-30 16:26:35 +02002158 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002159 free(resource);
2160}
2161
2162static void
2163bind_screensaver(struct wl_client *client,
2164 void *data, uint32_t version, uint32_t id)
2165{
Tiago Vignattibe143262012-04-16 17:31:41 +03002166 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002167 struct wl_resource *resource;
2168
2169 resource = wl_client_add_object(client, &screensaver_interface,
2170 &screensaver_implementation,
2171 id, shell);
2172
Pekka Paalanen77346a62011-11-30 16:26:35 +02002173 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002174 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002175 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002176 return;
2177 }
2178
2179 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2180 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002181 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002182}
2183
Kristian Høgsberg07045392012-02-19 18:52:44 -05002184struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002185 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002186 struct weston_surface *current;
2187 struct wl_listener listener;
2188 struct wl_keyboard_grab grab;
2189};
2190
2191static void
2192switcher_next(struct switcher *switcher)
2193{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002194 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002195 struct weston_surface *surface;
2196 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002197 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002198
2199 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002200 switch (get_shell_surface_type(surface)) {
2201 case SHELL_SURFACE_TOPLEVEL:
2202 case SHELL_SURFACE_FULLSCREEN:
2203 case SHELL_SURFACE_MAXIMIZED:
2204 if (first == NULL)
2205 first = surface;
2206 if (prev == switcher->current)
2207 next = surface;
2208 prev = surface;
2209 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002210 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002211 weston_surface_damage(surface);
2212 break;
2213 default:
2214 break;
2215 }
Alex Wu1659daa2012-04-01 20:13:09 +08002216
2217 if (is_black_surface(surface, NULL)) {
2218 surface->alpha = 64;
2219 surface->geometry.dirty = 1;
2220 weston_surface_damage(surface);
2221 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002222 }
2223
2224 if (next == NULL)
2225 next = first;
2226
Alex Wu07b26062012-03-12 16:06:01 +08002227 if (next == NULL)
2228 return;
2229
Kristian Høgsberg07045392012-02-19 18:52:44 -05002230 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002231 wl_signal_add(&next->surface.resource.destroy_signal,
2232 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002233
2234 switcher->current = next;
2235 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002236
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002237 shsurf = get_shell_surface(switcher->current);
2238 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2239 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002240}
2241
2242static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002243switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002244{
2245 struct switcher *switcher =
2246 container_of(listener, struct switcher, listener);
2247
2248 switcher_next(switcher);
2249}
2250
2251static void
2252switcher_destroy(struct switcher *switcher, uint32_t time)
2253{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002254 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002255 struct weston_surface *surface;
2256 struct weston_input_device *device =
2257 (struct weston_input_device *) switcher->grab.input_device;
2258
2259 wl_list_for_each(surface, &compositor->surface_list, link) {
2260 surface->alpha = 255;
2261 weston_surface_damage(surface);
2262 }
2263
Alex Wu07b26062012-03-12 16:06:01 +08002264 if (switcher->current)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002265 activate(switcher->shell, switcher->current, device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002266 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002267 wl_input_device_end_keyboard_grab(&device->input_device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002268 free(switcher);
2269}
2270
2271static void
2272switcher_key(struct wl_keyboard_grab *grab,
2273 uint32_t time, uint32_t key, int32_t state)
2274{
2275 struct switcher *switcher = container_of(grab, struct switcher, grab);
2276 struct weston_input_device *device =
2277 (struct weston_input_device *) grab->input_device;
2278
2279 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
2280 switcher_destroy(switcher, time);
2281 } else if (key == KEY_TAB && state) {
2282 switcher_next(switcher);
2283 }
2284};
2285
2286static const struct wl_keyboard_grab_interface switcher_grab = {
2287 switcher_key
2288};
2289
2290static void
2291switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002292 uint32_t key, uint32_t button, uint32_t axis,
2293 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002294{
Tiago Vignattibe143262012-04-16 17:31:41 +03002295 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002296 struct switcher *switcher;
2297
2298 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002299 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002300 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002301 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002302 wl_list_init(&switcher->listener.link);
2303
2304 switcher->grab.interface = &switcher_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002305 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2306 wl_input_device_set_keyboard_focus(device, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002307 switcher_next(switcher);
2308}
2309
Pekka Paalanen3c647232011-12-22 13:43:43 +02002310static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002311backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002312 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002313{
2314 struct weston_compositor *compositor = data;
2315 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002316 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002317
2318 /* TODO: we're limiting to simple use cases, where we assume just
2319 * control on the primary display. We'd have to extend later if we
2320 * ever get support for setting backlights on random desktop LCD
2321 * panels though */
2322 output = get_default_output(compositor);
2323 if (!output)
2324 return;
2325
2326 if (!output->set_backlight)
2327 return;
2328
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002329 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2330 backlight_new = output->backlight_current - 25;
2331 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2332 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002333
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002334 if (backlight_new < 5)
2335 backlight_new = 5;
2336 if (backlight_new > 255)
2337 backlight_new = 255;
2338
2339 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002340 output->set_backlight(output, output->backlight_current);
2341}
2342
2343static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002344debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002345 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002346{
Tiago Vignattibe143262012-04-16 17:31:41 +03002347 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002348 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002349 struct weston_surface *surface;
2350
2351 if (shell->debug_repaint_surface) {
2352 weston_surface_destroy(shell->debug_repaint_surface);
2353 shell->debug_repaint_surface = NULL;
2354 } else {
2355 surface = weston_surface_create(compositor);
2356 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2357 weston_surface_configure(surface, 0, 0, 8192, 8192);
2358 wl_list_insert(&compositor->fade_layer.surface_list,
2359 &surface->layer_link);
2360 weston_surface_assign_output(surface);
2361 pixman_region32_init(&surface->input);
2362
2363 /* Here's the dirty little trick that makes the
2364 * repaint debugging work: we force an
2365 * update_transform first to update dependent state
2366 * and clear the geometry.dirty bit. Then we clear
2367 * the surface damage so it only gets repainted
2368 * piecewise as we repaint other things. */
2369
2370 weston_surface_update_transform(surface);
2371 pixman_region32_fini(&surface->damage);
2372 pixman_region32_init(&surface->damage);
2373 shell->debug_repaint_surface = surface;
2374 }
2375}
2376
2377static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002378shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002379{
Tiago Vignattibe143262012-04-16 17:31:41 +03002380 struct desktop_shell *shell =
2381 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002382
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002383 if (shell->child.client)
2384 wl_client_destroy(shell->child.client);
2385
Pekka Paalanen3c647232011-12-22 13:43:43 +02002386 free(shell->screensaver.path);
2387 free(shell);
2388}
2389
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002390int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002391shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002392
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002393WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002394shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002395{
Tiago Vignattibe143262012-04-16 17:31:41 +03002396 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002397
2398 shell = malloc(sizeof *shell);
2399 if (shell == NULL)
2400 return -1;
2401
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002402 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002403 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002404
2405 shell->destroy_listener.notify = shell_destroy;
2406 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2407 shell->lock_listener.notify = lock;
2408 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2409 shell->unlock_listener.notify = unlock;
2410 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002411 ec->ping_handler = ping_handler;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002412
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002413 wl_list_init(&shell->backgrounds);
2414 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002415 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002416
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002417 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2418 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2419 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2420 weston_layer_init(&shell->background_layer,
2421 &shell->toplevel_layer.link);
2422 wl_list_init(&shell->lock_layer.surface_list);
2423
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002424 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002425
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002426 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2427 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002428 return -1;
2429
Kristian Høgsberg75840622011-09-06 13:48:16 -04002430 if (wl_display_add_global(ec->wl_display,
2431 &desktop_shell_interface,
2432 shell, bind_desktop_shell) == NULL)
2433 return -1;
2434
Pekka Paalanen6e168112011-11-24 11:34:05 +02002435 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2436 shell, bind_screensaver) == NULL)
2437 return -1;
2438
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002439 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002440 if (launch_desktop_shell_process(shell) != 0)
2441 return -1;
2442
Scott Moreau6a3633d2012-03-20 08:47:59 -06002443 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, MODIFIER_SUPER,
2444 move_binding, shell);
2445 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, MODIFIER_SUPER,
2446 resize_binding, shell);
2447 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2448 MODIFIER_CTRL | MODIFIER_ALT,
2449 terminate_binding, ec);
2450 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002451 click_to_activate_binding, shell);
Scott Moreau1b45a792012-03-22 10:58:23 -06002452 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002453 MODIFIER_SUPER | MODIFIER_ALT,
2454 surface_opacity_binding, NULL);
2455 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreau1b45a792012-03-22 10:58:23 -06002456 MODIFIER_SUPER, zoom_binding, NULL);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002457 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002458 MODIFIER_SUPER | MODIFIER_ALT,
2459 rotate_binding, NULL);
2460 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, MODIFIER_SUPER,
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002461 switcher_binding, shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002462
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002463 /* brightness */
Scott Moreau6a3633d2012-03-20 08:47:59 -06002464 weston_compositor_add_binding(ec, KEY_F9, 0, 0, MODIFIER_CTRL,
2465 backlight_binding, ec);
2466 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2467 backlight_binding, ec);
2468 weston_compositor_add_binding(ec, KEY_F10, 0, 0, MODIFIER_CTRL,
2469 backlight_binding, ec);
2470 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2471 backlight_binding, ec);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002472
Scott Moreau6a3633d2012-03-20 08:47:59 -06002473 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, MODIFIER_SUPER,
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002474 debug_repaint_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002475
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002476 return 0;
2477}