blob: 9f67a2b57b9063efc2efe991b337e51607dd34c2 [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;
100 uint32_t pong_received;
101 uint32_t serial;
102};
103
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200104struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200105 struct wl_resource resource;
106
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500107 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200108 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500109 struct shell_surface *parent;
Tiago Vignattibe143262012-04-16 17:31:41 +0300110 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200111
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500112 enum shell_surface_type type;
113 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800114 bool saved_position_valid;
Scott Moreauff1db4a2012-04-17 19:06:18 -0600115 int unresponsive;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100116
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500117 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200118 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500119 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200120 } rotation;
121
122 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700123 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500124 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200125 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500126 int32_t initial_up;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400127 struct wl_input_device *device;
128 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500129 } popup;
130
Alex Wu4539b082012-03-01 12:57:46 +0800131 struct {
132 enum wl_shell_surface_fullscreen_method type;
133 struct weston_transform transform; /* matrix from x, y */
134 uint32_t framerate;
135 struct weston_surface *black_surface;
136 } fullscreen;
137
Scott Moreauff1db4a2012-04-17 19:06:18 -0600138 struct ping_timer *ping_timer;
139
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600140 struct {
141 struct weston_animation current;
142 int exists;
143 int fading_in;
144 uint32_t timestamp;
145 } unresponsive_animation;
146
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500147 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500148 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100149 struct wl_list link;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300150
151 int force_configure;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200152};
153
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300154struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700155 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300156 struct shell_surface *shsurf;
157 struct wl_listener shsurf_destroy_listener;
158};
159
160struct weston_move_grab {
161 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500162 int32_t dx, dy;
163};
164
Pekka Paalanen460099f2012-01-20 16:48:25 +0200165struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300166 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500167 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200168 struct {
169 int32_t x;
170 int32_t y;
171 } center;
172};
173
Alex Wubd3354b2012-04-17 17:20:49 +0800174static struct shell_surface *
175get_shell_surface(struct weston_surface *surface);
176
177static struct desktop_shell *
178shell_surface_get_shell(struct shell_surface *shsurf);
179
180static bool
181shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
182{
183 struct desktop_shell *shell;
184 struct weston_surface *top_fs_es;
185
186 shell = shell_surface_get_shell(shsurf);
187
188 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
189 return false;
190
191 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
192 struct weston_surface,
193 layer_link);
194 return (shsurf == get_shell_surface(top_fs_es));
195}
196
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500197static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400198destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300199{
200 struct shell_grab *grab;
201
202 grab = container_of(listener, struct shell_grab,
203 shsurf_destroy_listener);
204
205 grab->shsurf = NULL;
206}
207
208static void
209shell_grab_init(struct shell_grab *grab,
210 const struct wl_pointer_grab_interface *interface,
211 struct shell_surface *shsurf)
212{
213 grab->grab.interface = interface;
214 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400215 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
216 wl_signal_add(&shsurf->resource.destroy_signal,
217 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300218
219}
220
221static void
222shell_grab_finish(struct shell_grab *grab)
223{
224 wl_list_remove(&grab->shsurf_destroy_listener.link);
225}
226
227static void
Alex Wu4539b082012-03-01 12:57:46 +0800228center_on_output(struct weston_surface *surface,
229 struct weston_output *output);
230
231static void
Tiago Vignattibe143262012-04-16 17:31:41 +0300232shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200233{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200234 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200235 char *path = NULL;
236 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200237
238 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200239 { "path", CONFIG_KEY_STRING, &path },
240 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200241 };
242
243 struct config_section cs[] = {
244 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
245 };
246
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200247 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500248 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200249 free(config_file);
250
Pekka Paalanen7296e792011-12-07 16:22:00 +0200251 shell->screensaver.path = path;
252 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200253}
254
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200255static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400256noop_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500257 struct wl_surface *surface, int32_t x, int32_t y)
258{
259 grab->focus = NULL;
260}
261
262static void
Scott Moreau447013d2012-02-18 05:05:29 -0700263move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500264 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500265{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500266 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500267 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300268 struct shell_surface *shsurf = move->base.shsurf;
269 struct weston_surface *es;
270
271 if (!shsurf)
272 return;
273
274 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500275
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500276 weston_surface_configure(es,
277 device->x + move->dx,
278 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200279 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500280}
281
282static void
Scott Moreau447013d2012-02-18 05:05:29 -0700283move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400284 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500285{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300286 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
287 grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500288 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500289
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500290 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300291 shell_grab_finish(shell_grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400292 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500293 free(grab);
294 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500295}
296
Scott Moreau447013d2012-02-18 05:05:29 -0700297static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500298 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500299 move_grab_motion,
300 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500301};
302
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600303static void
304unresponsive_surface_fade(struct shell_surface *shsurf, bool reverse)
305{
306 shsurf->unresponsive_animation.fading_in = reverse ? 0 : 1;
307
308 if(!shsurf->unresponsive_animation.exists) {
309 wl_list_insert(&shsurf->surface->compositor->animation_list,
310 &shsurf->unresponsive_animation.current.link);
311 shsurf->unresponsive_animation.exists = 1;
312 shsurf->unresponsive_animation.timestamp = weston_compositor_get_time();
313 weston_surface_damage(shsurf->surface);
314 }
315}
316
317static void
318ping_timeout_fade_frame(struct weston_animation *animation,
319 struct weston_output *output, uint32_t msecs)
320{
321 struct shell_surface *shsurf =
322 container_of(animation, struct shell_surface, unresponsive_animation.current);
323 struct weston_surface *surface = shsurf->surface;
324 unsigned int step = 32;
325
326 if (!surface || !shsurf)
327 return;
328
329 if (shsurf->unresponsive_animation.fading_in) {
330 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
331 if (surface->saturation > 1)
332 surface->saturation -= 5;
333 if (surface->brightness > 200)
334 surface->brightness--;
335
336 shsurf->unresponsive_animation.timestamp += step;
337 }
338
339 if (surface->saturation <= 1 && surface->brightness <= 200) {
340 wl_list_remove(&shsurf->unresponsive_animation.current.link);
341 shsurf->unresponsive_animation.exists = 0;
342 }
343 }
344 else {
345 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
346 if (surface->saturation < 255)
347 surface->saturation += 5;
348 if (surface->brightness < 255)
349 surface->brightness++;
350
351 shsurf->unresponsive_animation.timestamp += step;
352 }
353
354 if (surface->saturation >= 255 && surface->brightness >= 255) {
355 surface->saturation = surface->brightness = 255;
356 wl_list_remove(&shsurf->unresponsive_animation.current.link);
357 shsurf->unresponsive_animation.exists = 0;
358 }
359 }
360
361 surface->geometry.dirty = 1;
362 weston_surface_damage(surface);
363}
364
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400365static int
Scott Moreauff1db4a2012-04-17 19:06:18 -0600366ping_timeout_handler(void *data)
367{
368 struct shell_surface *shsurf = data;
369
370 if (!shsurf || !shsurf->ping_timer)
371 return 1;
372
373 if (shsurf->ping_timer->pong_received) {
374 free(shsurf->ping_timer);
375 shsurf->ping_timer = NULL;
376 } else {
377 /* Client is not responding */
378 shsurf->unresponsive = 1;
Scott Moreauc3e54eb2012-04-17 19:06:20 -0600379 unresponsive_surface_fade(shsurf, false);
Scott Moreauff1db4a2012-04-17 19:06:18 -0600380 }
381
382 return 1;
383}
384
385static void
386ping_handler(struct weston_surface *surface, uint32_t serial)
387{
388 struct shell_surface *shsurf;
389 shsurf = get_shell_surface(surface);
390 struct wl_event_loop *loop;
391 int ping_timeout = 15000;
392
393 if (!shsurf)
394 return;
395
396 if (!shsurf->ping_timer) {
397 shsurf->ping_timer = malloc(sizeof shsurf->ping_timer);
398 if (!shsurf->ping_timer)
399 return;
400
401 shsurf->ping_timer->serial = serial;
402 shsurf->ping_timer->pong_received = 0;
403 loop = wl_display_get_event_loop(surface->compositor->wl_display);
404 shsurf->ping_timer->source =
405 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
406 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
407
408 wl_shell_surface_send_ping(&shsurf->resource, serial);
409 }
410}
411
412static void
413shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
414 uint32_t serial)
415{
416 struct shell_surface *shsurf = resource->data;
417
418 if (!shsurf || !shsurf->ping_timer)
419 return;
420
421 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->ping_timer->pong_received = 1;
427 shsurf->unresponsive = 0;
428 free(shsurf->ping_timer);
429 shsurf->ping_timer = NULL;
430 }
431}
432
433static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500434weston_surface_move(struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400435 struct weston_input_device *wd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500436{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500437 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300438 struct shell_surface *shsurf = get_shell_surface(es);
439
440 if (!shsurf)
441 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500442
443 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400444 if (!move)
445 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500446
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300447 shell_grab_init(&move->base, &move_grab_interface, shsurf);
448
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200449 move->dx = es->geometry.x - wd->input_device.grab_x;
450 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500451
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300452 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400453 &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500454
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400455 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400456
457 return 0;
458}
459
460static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200461shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400462 struct wl_resource *input_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400463{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500464 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200465 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400466
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500467 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400468 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500469 wd->input_device.pointer_focus != &shsurf->surface->surface)
470 return;
471
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400472 if (weston_surface_move(shsurf->surface, wd) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400473 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500474}
475
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500476struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300477 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500478 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200479 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500480};
481
482static void
Scott Moreau447013d2012-02-18 05:05:29 -0700483resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500484 uint32_t time, int32_t x, int32_t y)
485{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500486 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500487 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500488 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200489 int32_t from_x, from_y;
490 int32_t to_x, to_y;
491
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300492 if (!resize->base.shsurf)
493 return;
494
495 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200496 device->grab_x, device->grab_y,
497 &from_x, &from_y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300498 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200499 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500500
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200501 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200502 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200503 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200504 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500505 } else {
506 width = resize->width;
507 }
508
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200509 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200510 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200511 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200512 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500513 } else {
514 height = resize->height;
515 }
516
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300517 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400518 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500519}
520
521static void
Scott Moreau447013d2012-02-18 05:05:29 -0700522resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400523 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500524{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300525 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500526 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500527
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500528 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300529 shell_grab_finish(&resize->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400530 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500531 free(grab);
532 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500533}
534
Scott Moreau447013d2012-02-18 05:05:29 -0700535static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500536 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500537 resize_grab_motion,
538 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500539};
540
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400541static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500542weston_surface_resize(struct shell_surface *shsurf,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400543 struct weston_input_device *wd, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500544{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500545 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500546
Alex Wu4539b082012-03-01 12:57:46 +0800547 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
548 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500549
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200550 if (edges == 0 || edges > 15 ||
551 (edges & 3) == 3 || (edges & 12) == 12)
552 return 0;
553
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500554 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400555 if (!resize)
556 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500557
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300558 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
559
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500560 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200561 resize->width = shsurf->surface->geometry.width;
562 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400563
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300564 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400565 &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500566
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400567 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400568
569 return 0;
570}
571
572static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200573shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400574 struct wl_resource *input_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200575 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400576{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500577 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200578 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400579
Alex Wu4539b082012-03-01 12:57:46 +0800580 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
581 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400582
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500583 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400584 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500585 wd->input_device.pointer_focus != &shsurf->surface->surface)
586 return;
587
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400588 if (weston_surface_resize(shsurf, wd, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400589 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500590}
591
Juan Zhao96879df2012-02-07 08:45:41 +0800592static struct weston_output *
593get_default_output(struct weston_compositor *compositor)
594{
595 return container_of(compositor->output_list.next,
596 struct weston_output, link);
597}
598
Alex Wu4539b082012-03-01 12:57:46 +0800599static void
600shell_unset_fullscreen(struct shell_surface *shsurf)
601{
602 /* undo all fullscreen things here */
Alex Wubd3354b2012-04-17 17:20:49 +0800603 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
604 shell_surface_is_top_fullscreen(shsurf)) {
605 weston_output_switch_mode(shsurf->fullscreen_output,
606 shsurf->fullscreen_output->origin);
607 }
Alex Wu4539b082012-03-01 12:57:46 +0800608 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
609 shsurf->fullscreen.framerate = 0;
610 wl_list_remove(&shsurf->fullscreen.transform.link);
611 wl_list_init(&shsurf->fullscreen.transform.link);
Alex Wubd3354b2012-04-17 17:20:49 +0800612 if (shsurf->fullscreen.black_surface)
613 weston_surface_destroy(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800614 shsurf->fullscreen.black_surface = NULL;
615 shsurf->fullscreen_output = NULL;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300616 shsurf->force_configure = 1;
Alex Wu4539b082012-03-01 12:57:46 +0800617 weston_surface_set_position(shsurf->surface,
618 shsurf->saved_x, shsurf->saved_y);
619}
620
Pekka Paalanen98262232011-12-01 10:42:22 +0200621static int
622reset_shell_surface_type(struct shell_surface *surface)
623{
624 switch (surface->type) {
625 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800626 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200627 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800628 case SHELL_SURFACE_MAXIMIZED:
629 surface->output = get_default_output(surface->surface->compositor);
630 weston_surface_set_position(surface->surface,
631 surface->saved_x,
632 surface->saved_y);
633 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200634 case SHELL_SURFACE_PANEL:
635 case SHELL_SURFACE_BACKGROUND:
636 wl_list_remove(&surface->link);
637 wl_list_init(&surface->link);
638 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200639 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200640 case SHELL_SURFACE_LOCK:
641 wl_resource_post_error(&surface->resource,
642 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200643 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200644 return -1;
645 case SHELL_SURFACE_NONE:
646 case SHELL_SURFACE_TOPLEVEL:
647 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500648 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200649 break;
650 }
651
652 surface->type = SHELL_SURFACE_NONE;
653 return 0;
654}
655
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500656static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200657shell_surface_set_toplevel(struct wl_client *client,
658 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400659
660{
Pekka Paalanen98262232011-12-01 10:42:22 +0200661 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400662
Pekka Paalanen98262232011-12-01 10:42:22 +0200663 if (reset_shell_surface_type(surface))
664 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400665
Pekka Paalanen98262232011-12-01 10:42:22 +0200666 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400667}
668
669static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200670shell_surface_set_transient(struct wl_client *client,
671 struct wl_resource *resource,
672 struct wl_resource *parent_resource,
673 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400674{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200675 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500676 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200677 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500678 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400679
Pekka Paalanen98262232011-12-01 10:42:22 +0200680 if (reset_shell_surface_type(shsurf))
681 return;
682
Alex Wu4539b082012-03-01 12:57:46 +0800683 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800684 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200685 weston_surface_set_position(es, pes->geometry.x + x,
686 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400687
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200688 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400689}
690
Tiago Vignattibe143262012-04-16 17:31:41 +0300691static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +0800692shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200693{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400694 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800695}
696
697static int
Tiago Vignattibe143262012-04-16 17:31:41 +0300698get_output_panel_height(struct desktop_shell *shell,
699 struct weston_output *output)
Juan Zhao96879df2012-02-07 08:45:41 +0800700{
701 struct shell_surface *priv;
702 int panel_height = 0;
703
704 if (!output)
705 return 0;
706
Tiago Vignattibe143262012-04-16 17:31:41 +0300707 wl_list_for_each(priv, &shell->panels, link) {
Juan Zhao96879df2012-02-07 08:45:41 +0800708 if (priv->output == output) {
709 panel_height = priv->surface->geometry.height;
710 break;
711 }
712 }
713 return panel_height;
714}
715
716static void
717shell_surface_set_maximized(struct wl_client *client,
718 struct wl_resource *resource,
719 struct wl_resource *output_resource )
720{
721 struct shell_surface *shsurf = resource->data;
722 struct weston_surface *es = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300723 struct desktop_shell *shell = NULL;
Juan Zhao96879df2012-02-07 08:45:41 +0800724 uint32_t edges = 0, panel_height = 0;
725
726 /* get the default output, if the client set it as NULL
727 check whether the ouput is available */
728 if (output_resource)
729 shsurf->output = output_resource->data;
730 else
731 shsurf->output = get_default_output(es->compositor);
732
733 if (reset_shell_surface_type(shsurf))
734 return;
735
736 shsurf->saved_x = es->geometry.x;
737 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800738 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800739
Tiago Vignattibe143262012-04-16 17:31:41 +0300740 shell = shell_surface_get_shell(shsurf);
741 panel_height = get_output_panel_height(shell, es->output);
Juan Zhao96879df2012-02-07 08:45:41 +0800742 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500743
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400744 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500745 es->output->current->width,
746 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800747
748 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200749}
750
Alex Wu21858432012-04-01 20:13:08 +0800751static void
752black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
753
Alex Wu4539b082012-03-01 12:57:46 +0800754static struct weston_surface *
755create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800756 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800757 GLfloat x, GLfloat y, int w, int h)
758{
759 struct weston_surface *surface = NULL;
760
761 surface = weston_surface_create(ec);
762 if (surface == NULL) {
763 fprintf(stderr, "no memory\n");
764 return NULL;
765 }
766
Alex Wu21858432012-04-01 20:13:08 +0800767 surface->configure = black_surface_configure;
768 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800769 weston_surface_configure(surface, x, y, w, h);
770 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
771 return surface;
772}
773
774/* Create black surface and append it to the associated fullscreen surface.
775 * Handle size dismatch and positioning according to the method. */
776static void
777shell_configure_fullscreen(struct shell_surface *shsurf)
778{
779 struct weston_output *output = shsurf->fullscreen_output;
780 struct weston_surface *surface = shsurf->surface;
781 struct weston_matrix *matrix;
782 float scale;
783
784 center_on_output(surface, output);
785
786 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500787 shsurf->fullscreen.black_surface =
788 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800789 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500790 output->x, output->y,
791 output->current->width,
792 output->current->height);
793
794 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
795 wl_list_insert(&surface->layer_link,
796 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800797 shsurf->fullscreen.black_surface->output = output;
798
799 switch (shsurf->fullscreen.type) {
800 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
801 break;
802 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
803 matrix = &shsurf->fullscreen.transform.matrix;
804 weston_matrix_init(matrix);
805 scale = (float)output->current->width/(float)surface->geometry.width;
806 weston_matrix_scale(matrix, scale, scale, 1);
807 wl_list_remove(&shsurf->fullscreen.transform.link);
808 wl_list_insert(surface->geometry.transformation_list.prev,
809 &shsurf->fullscreen.transform.link);
810 weston_surface_set_position(surface, output->x, output->y);
811 break;
812 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
Alex Wubd3354b2012-04-17 17:20:49 +0800813 if (shell_surface_is_top_fullscreen(shsurf)) {
814 struct weston_mode mode = {0,
815 surface->geometry.width,
816 surface->geometry.height,
817 shsurf->fullscreen.framerate};
818
819 if (weston_output_switch_mode(output, &mode) == 0) {
820 weston_surface_configure(shsurf->fullscreen.black_surface,
821 output->x, output->y,
822 output->current->width,
823 output->current->height);
824 weston_surface_set_position(surface, output->x, output->y);
825 break;
826 }
827 }
Alex Wu4539b082012-03-01 12:57:46 +0800828 break;
829 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
830 break;
831 default:
832 break;
833 }
834}
835
836/* make the fullscreen and black surface at the top */
837static void
838shell_stack_fullscreen(struct shell_surface *shsurf)
839{
Alex Wubd3354b2012-04-17 17:20:49 +0800840 struct weston_output *output = shsurf->fullscreen_output;
Alex Wu4539b082012-03-01 12:57:46 +0800841 struct weston_surface *surface = shsurf->surface;
Tiago Vignattibe143262012-04-16 17:31:41 +0300842 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800843
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500844 wl_list_remove(&surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500845 wl_list_insert(&shell->fullscreen_layer.surface_list,
846 &surface->layer_link);
Alex Wubd3354b2012-04-17 17:20:49 +0800847 weston_surface_damage(surface);
848
849 if (!shsurf->fullscreen.black_surface)
850 shsurf->fullscreen.black_surface =
851 create_black_surface(surface->compositor,
852 surface,
853 output->x, output->y,
854 output->current->width,
855 output->current->height);
856
857 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500858 wl_list_insert(&surface->layer_link,
859 &shsurf->fullscreen.black_surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500860 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800861}
862
863static void
864shell_map_fullscreen(struct shell_surface *shsurf)
865{
Alex Wu4539b082012-03-01 12:57:46 +0800866 shell_stack_fullscreen(shsurf);
Alex Wubd3354b2012-04-17 17:20:49 +0800867 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800868}
869
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400870static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200871shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500872 struct wl_resource *resource,
873 uint32_t method,
874 uint32_t framerate,
875 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400876{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200877 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500878 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800879
880 if (output_resource)
881 shsurf->output = output_resource->data;
882 else
883 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400884
Pekka Paalanen98262232011-12-01 10:42:22 +0200885 if (reset_shell_surface_type(shsurf))
886 return;
887
Alex Wu4539b082012-03-01 12:57:46 +0800888 shsurf->fullscreen_output = shsurf->output;
889 shsurf->fullscreen.type = method;
890 shsurf->fullscreen.framerate = framerate;
891 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400892
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200893 shsurf->saved_x = es->geometry.x;
894 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800895 shsurf->saved_position_valid = true;
896
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300897 if (weston_surface_is_mapped(es))
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300898 shsurf->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500899
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400900 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500901 shsurf->output->current->width,
902 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400903}
904
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500905static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400906popup_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500907 struct wl_surface *surface, int32_t x, int32_t y)
908{
909 struct wl_input_device *device = grab->input_device;
910 struct shell_surface *priv =
911 container_of(grab, struct shell_surface, popup.grab);
912 struct wl_client *client = priv->surface->surface.resource.client;
913
Pekka Paalanencb108432012-01-19 16:25:40 +0200914 if (surface && surface->resource.client == client) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400915 wl_input_device_set_pointer_focus(device, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500916 grab->focus = surface;
917 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400918 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500919 grab->focus = NULL;
920 }
921}
922
923static void
Scott Moreau447013d2012-02-18 05:05:29 -0700924popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200925 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500926{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500927 struct wl_resource *resource;
928
929 resource = grab->input_device->pointer_focus_resource;
930 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500931 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500932}
933
934static void
Scott Moreau447013d2012-02-18 05:05:29 -0700935popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400936 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500937{
938 struct wl_resource *resource;
939 struct shell_surface *shsurf =
940 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400941 struct wl_display *display;
942 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500943
944 resource = grab->input_device->pointer_focus_resource;
945 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400946 display = wl_client_get_display(resource->client);
947 serial = wl_display_get_serial(display);
948 wl_input_device_send_button(resource, serial,
949 time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500950 } else if (state == 0 &&
951 (shsurf->popup.initial_up ||
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400952 time - shsurf->popup.device->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500953 wl_shell_surface_send_popup_done(&shsurf->resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400954 wl_input_device_end_pointer_grab(grab->input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500955 shsurf->popup.grab.input_device = NULL;
956 }
957
958 if (state == 0)
959 shsurf->popup.initial_up = 1;
960}
961
Scott Moreau447013d2012-02-18 05:05:29 -0700962static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500963 popup_grab_focus,
964 popup_grab_motion,
965 popup_grab_button,
966};
967
968static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400969shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500970{
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400971 struct wl_input_device *device = shsurf->popup.device;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500972 struct weston_surface *es = shsurf->surface;
973 struct weston_surface *parent = shsurf->parent->surface;
974
975 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500976 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500977
Pekka Paalanen938269a2012-02-07 14:19:01 +0200978 weston_surface_update_transform(parent);
979 if (parent->transform.enabled) {
980 shsurf->popup.parent_transform.matrix =
981 parent->transform.matrix;
982 } else {
983 /* construct x, y translation matrix */
984 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
985 shsurf->popup.parent_transform.matrix.d[12] =
986 parent->geometry.x;
987 shsurf->popup.parent_transform.matrix.d[13] =
988 parent->geometry.y;
989 }
990 wl_list_insert(es->geometry.transformation_list.prev,
991 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200992 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500993
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500994 shsurf->popup.initial_up = 0;
995
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400996 /* We don't require the grab to still be active, but if another
997 * grab has started in the meantime, we end the popup now. */
998 if (device->grab_serial == shsurf->popup.serial) {
999 wl_input_device_start_pointer_grab(device,
1000 &shsurf->popup.grab);
1001 } else {
1002 wl_shell_surface_send_popup_done(&shsurf->resource);
1003 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001004}
1005
1006static void
1007shell_surface_set_popup(struct wl_client *client,
1008 struct wl_resource *resource,
1009 struct wl_resource *input_device_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001010 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001011 struct wl_resource *parent_resource,
1012 int32_t x, int32_t y, uint32_t flags)
1013{
1014 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001015
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001016 shsurf->type = SHELL_SURFACE_POPUP;
1017 shsurf->parent = parent_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001018 shsurf->popup.device = input_device_resource->data;
1019 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001020 shsurf->popup.x = x;
1021 shsurf->popup.y = y;
1022}
1023
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001024static const struct wl_shell_surface_interface shell_surface_implementation = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001025 shell_surface_pong,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001026 shell_surface_move,
1027 shell_surface_resize,
1028 shell_surface_set_toplevel,
1029 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001030 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +08001031 shell_surface_set_popup,
1032 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001033};
1034
1035static void
1036destroy_shell_surface(struct wl_resource *resource)
1037{
1038 struct shell_surface *shsurf = resource->data;
1039
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001040 if (shsurf->popup.grab.input_device)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001041 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001042
Alex Wubd3354b2012-04-17 17:20:49 +08001043 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1044 shell_surface_is_top_fullscreen(shsurf)) {
1045 weston_output_switch_mode(shsurf->fullscreen_output,
1046 shsurf->fullscreen_output->origin);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001047 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001048
Alex Wuaa08e2d2012-03-05 11:01:40 +08001049 if (shsurf->fullscreen.black_surface)
1050 weston_surface_destroy(shsurf->fullscreen.black_surface);
1051
Alex Wubd3354b2012-04-17 17:20:49 +08001052 /* As destroy_resource() use wl_list_for_each_safe(),
1053 * we can always remove the listener.
1054 */
1055 wl_list_remove(&shsurf->surface_destroy_listener.link);
1056 shsurf->surface->configure = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001057 if (shsurf->ping_timer)
1058 free(shsurf->ping_timer);
Alex Wubd3354b2012-04-17 17:20:49 +08001059
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001060 wl_list_remove(&shsurf->link);
1061 free(shsurf);
1062}
1063
1064static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001065shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001066{
1067 struct shell_surface *shsurf = container_of(listener,
1068 struct shell_surface,
1069 surface_destroy_listener);
1070
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001071 wl_resource_destroy(&shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001072}
1073
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001074static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001075get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001076{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001077 struct wl_listener *listener;
1078
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001079 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1080 shell_handle_surface_destroy);
1081 if (listener)
1082 return container_of(listener, struct shell_surface,
1083 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02001084
1085 return NULL;
1086}
1087
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001088static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001089shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1090
1091static void
Pekka Paalanen46229672011-11-29 15:49:31 +02001092shell_get_shell_surface(struct wl_client *client,
1093 struct wl_resource *resource,
1094 uint32_t id,
1095 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001096{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001097 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001098 struct shell_surface *shsurf;
1099
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +02001100 if (get_shell_surface(surface)) {
1101 wl_resource_post_error(surface_resource,
1102 WL_DISPLAY_ERROR_INVALID_OBJECT,
Alex Wubd3354b2012-04-17 17:20:49 +08001103 "desktop_shell::get_shell_surface already requested");
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +02001104 return;
1105 }
1106
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001107 if (surface->configure) {
1108 wl_resource_post_error(surface_resource,
1109 WL_DISPLAY_ERROR_INVALID_OBJECT,
1110 "surface->configure already set");
1111 return;
1112 }
1113
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001114 shsurf = calloc(1, sizeof *shsurf);
1115 if (!shsurf) {
1116 wl_resource_post_no_memory(resource);
1117 return;
1118 }
1119
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001120 surface->configure = shell_surface_configure;
1121
Scott Moreauff1db4a2012-04-17 19:06:18 -06001122 shsurf->unresponsive = 0;
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001123 shsurf->unresponsive_animation.exists = 0;
1124 shsurf->unresponsive_animation.fading_in = 0;
1125 shsurf->unresponsive_animation.current.frame = ping_timeout_fade_frame;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001126
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001127 shsurf->resource.destroy = destroy_shell_surface;
1128 shsurf->resource.object.id = id;
1129 shsurf->resource.object.interface = &wl_shell_surface_interface;
1130 shsurf->resource.object.implementation =
1131 (void (**)(void)) &shell_surface_implementation;
1132 shsurf->resource.data = shsurf;
1133
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001134 shsurf->shell = resource->data;
Alex Wu4539b082012-03-01 12:57:46 +08001135 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001136 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +08001137 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1138 shsurf->fullscreen.framerate = 0;
1139 shsurf->fullscreen.black_surface = NULL;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001140 shsurf->ping_timer = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08001141 wl_list_init(&shsurf->fullscreen.transform.link);
1142
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001143 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1144 wl_signal_add(&surface->surface.resource.destroy_signal,
1145 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001146
1147 /* init link so its safe to always remove it in destroy_shell_surface */
1148 wl_list_init(&shsurf->link);
1149
Pekka Paalanen460099f2012-01-20 16:48:25 +02001150 /* empty when not in use */
1151 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001152 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001153
Pekka Paalanen98262232011-12-01 10:42:22 +02001154 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001155
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001156 wl_client_add_resource(client, &shsurf->resource);
1157}
1158
1159static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +02001160 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001161};
1162
Kristian Høgsberg07937562011-04-12 17:25:42 -04001163static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001164handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +02001165{
1166 proc->pid = 0;
1167}
1168
1169static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001170launch_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001171{
1172 if (shell->screensaver.binding)
1173 return;
1174
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001175 if (!shell->screensaver.path)
1176 return;
1177
Kristian Høgsberg32bed572012-03-01 17:11:36 -05001178 if (shell->screensaver.process.pid != 0) {
1179 fprintf(stderr, "old screensaver still running\n");
1180 return;
1181 }
1182
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001183 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001184 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001185 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +02001186 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001187}
1188
1189static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001190terminate_screensaver(struct desktop_shell *shell)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001191{
Pekka Paalanen18027e52011-12-02 16:31:49 +02001192 if (shell->screensaver.process.pid == 0)
1193 return;
1194
1195 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001196}
1197
1198static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001199show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001200{
1201 struct wl_list *list;
1202
1203 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001204 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001205 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001206 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001207
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001208 wl_list_remove(&surface->surface->layer_link);
1209 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001210 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001211 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001212}
1213
1214static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001215hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001216{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001217 wl_list_remove(&surface->surface->layer_link);
1218 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001219 surface->surface->output = NULL;
1220}
1221
1222static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001223desktop_shell_set_background(struct wl_client *client,
1224 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001225 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001226 struct wl_resource *surface_resource)
1227{
Tiago Vignattibe143262012-04-16 17:31:41 +03001228 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001229 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001230 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001231 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001232
Pekka Paalanen98262232011-12-01 10:42:22 +02001233 if (reset_shell_surface_type(shsurf))
1234 return;
1235
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001236 wl_list_for_each(priv, &shell->backgrounds, link) {
1237 if (priv->output == output_resource->data) {
1238 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001239 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001240 wl_list_remove(&priv->link);
1241 break;
1242 }
1243 }
1244
Pekka Paalanen068ae942011-11-28 14:11:15 +02001245 shsurf->type = SHELL_SURFACE_BACKGROUND;
1246 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001247
Pekka Paalanen068ae942011-11-28 14:11:15 +02001248 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001249
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001250 weston_surface_set_position(surface, shsurf->output->x,
1251 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001252
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001253 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001254 surface_resource,
1255 shsurf->output->current->width,
1256 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001257}
1258
1259static void
1260desktop_shell_set_panel(struct wl_client *client,
1261 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001262 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001263 struct wl_resource *surface_resource)
1264{
Tiago Vignattibe143262012-04-16 17:31:41 +03001265 struct desktop_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001266 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001267 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001268 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001269
Pekka Paalanen98262232011-12-01 10:42:22 +02001270 if (reset_shell_surface_type(shsurf))
1271 return;
1272
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001273 wl_list_for_each(priv, &shell->panels, link) {
1274 if (priv->output == output_resource->data) {
1275 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001276 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001277 wl_list_remove(&priv->link);
1278 break;
1279 }
1280 }
1281
Pekka Paalanen068ae942011-11-28 14:11:15 +02001282 shsurf->type = SHELL_SURFACE_PANEL;
1283 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001284
Pekka Paalanen068ae942011-11-28 14:11:15 +02001285 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001286
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001287 weston_surface_set_position(surface, shsurf->output->x,
1288 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001289
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001290 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001291 surface_resource,
1292 shsurf->output->current->width,
1293 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001294}
1295
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001296static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001297handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001298{
Tiago Vignattibe143262012-04-16 17:31:41 +03001299 struct desktop_shell *shell =
1300 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001301
1302 fprintf(stderr, "lock surface gone\n");
1303 shell->lock_surface = NULL;
1304}
1305
1306static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001307desktop_shell_set_lock_surface(struct wl_client *client,
1308 struct wl_resource *resource,
1309 struct wl_resource *surface_resource)
1310{
Tiago Vignattibe143262012-04-16 17:31:41 +03001311 struct desktop_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001312 struct shell_surface *surface = surface_resource->data;
1313
1314 if (reset_shell_surface_type(surface))
1315 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001316
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001317 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001318
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001319 if (!shell->locked)
1320 return;
1321
Pekka Paalanen98262232011-12-01 10:42:22 +02001322 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001323
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001324 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1325 wl_signal_add(&surface_resource->destroy_signal,
1326 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001327
Pekka Paalanen068ae942011-11-28 14:11:15 +02001328 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001329}
1330
1331static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001332resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001333{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001334 struct shell_surface *tmp;
1335
1336 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1337 hide_screensaver(shell, tmp);
1338
1339 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001340
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001341 wl_list_remove(&shell->lock_layer.link);
1342 wl_list_insert(&shell->compositor->cursor_layer.link,
1343 &shell->fullscreen_layer.link);
1344 wl_list_insert(&shell->fullscreen_layer.link,
1345 &shell->panel_layer.link);
1346 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001347
1348 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001349 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001350 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001351 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001352}
1353
1354static void
1355desktop_shell_unlock(struct wl_client *client,
1356 struct wl_resource *resource)
1357{
Tiago Vignattibe143262012-04-16 17:31:41 +03001358 struct desktop_shell *shell = resource->data;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001359
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001360 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001361
1362 if (shell->locked)
1363 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001364}
1365
Kristian Høgsberg75840622011-09-06 13:48:16 -04001366static const struct desktop_shell_interface desktop_shell_implementation = {
1367 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001368 desktop_shell_set_panel,
1369 desktop_shell_set_lock_surface,
1370 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001371};
1372
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001373static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001374get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001375{
1376 struct shell_surface *shsurf;
1377
1378 shsurf = get_shell_surface(surface);
1379 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001380 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001381 return shsurf->type;
1382}
1383
Kristian Høgsberg75840622011-09-06 13:48:16 -04001384static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001385move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001386 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001387{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001388 struct weston_surface *surface =
1389 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001390
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001391 if (surface == NULL)
1392 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001393
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001394 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001395 case SHELL_SURFACE_PANEL:
1396 case SHELL_SURFACE_BACKGROUND:
1397 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001398 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001399 return;
1400 default:
1401 break;
1402 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001403
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001404 weston_surface_move(surface, (struct weston_input_device *) device);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001405}
1406
1407static void
1408resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001409 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001410{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001411 struct weston_surface *surface =
1412 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001413 uint32_t edges = 0;
1414 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001415 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001416
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001417 if (surface == NULL)
1418 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001419
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001420 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001421 if (!shsurf)
1422 return;
1423
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001424 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001425 case SHELL_SURFACE_PANEL:
1426 case SHELL_SURFACE_BACKGROUND:
1427 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001428 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001429 return;
1430 default:
1431 break;
1432 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001433
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001434 weston_surface_from_global(surface,
1435 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001436
Pekka Paalanen60921e52012-01-25 15:55:43 +02001437 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001438 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001439 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001440 edges |= 0;
1441 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001442 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001443
Pekka Paalanen60921e52012-01-25 15:55:43 +02001444 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001445 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001446 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001447 edges |= 0;
1448 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001449 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001450
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001451 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001452 edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001453}
1454
1455static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001456surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1457 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1458{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001459 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001460 struct shell_surface *shsurf;
1461 struct weston_surface *surface =
1462 (struct weston_surface *) device->pointer_focus;
1463
1464 if (surface == NULL)
1465 return;
1466
1467 shsurf = get_shell_surface(surface);
1468 if (!shsurf)
1469 return;
1470
1471 switch (shsurf->type) {
1472 case SHELL_SURFACE_BACKGROUND:
1473 case SHELL_SURFACE_SCREENSAVER:
1474 return;
1475 default:
1476 break;
1477 }
1478
1479 surface->alpha += value * step;
1480
1481 if (surface->alpha > 255)
1482 surface->alpha = 255;
1483 if (surface->alpha < step)
1484 surface->alpha = step;
1485
1486 surface->geometry.dirty = 1;
1487 weston_surface_damage(surface);
1488}
1489
1490static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001491zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001492 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001493{
1494 struct weston_input_device *wd = (struct weston_input_device *) device;
1495 struct weston_compositor *compositor = wd->compositor;
1496 struct weston_output *output;
1497
1498 wl_list_for_each(output, &compositor->output_list, link) {
1499 if (pixman_region32_contains_point(&output->region,
1500 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001501 output->zoom.active = 1;
1502 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001503
1504 if (output->zoom.level >= 1.0) {
1505 output->zoom.active = 0;
1506 output->zoom.level = 1.0;
1507 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001508
1509 if (output->zoom.level < output->zoom.increment)
1510 output->zoom.level = output->zoom.increment;
1511
1512 weston_output_update_zoom(output, device->x, device->y);
1513 }
1514 }
1515}
1516
Scott Moreauccbf29d2012-02-22 14:21:41 -07001517static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001518terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001519 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001520{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001521 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001522
1523 if (state)
1524 wl_display_terminate(compositor->wl_display);
1525}
1526
1527static void
Scott Moreau447013d2012-02-18 05:05:29 -07001528rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001529 uint32_t time, int32_t x, int32_t y)
1530{
1531 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001532 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001533 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001534 struct shell_surface *shsurf = rotate->base.shsurf;
1535 struct weston_surface *surface;
1536 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1537
1538 if (!shsurf)
1539 return;
1540
1541 surface = shsurf->surface;
1542
1543 cx = 0.5f * surface->geometry.width;
1544 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001545
1546 dx = device->x - rotate->center.x;
1547 dy = device->y - rotate->center.y;
1548 r = sqrtf(dx * dx + dy * dy);
1549
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001550 wl_list_remove(&shsurf->rotation.transform.link);
1551 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001552
1553 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001554 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001555 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001556
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001557 weston_matrix_init(&rotate->rotation);
1558 rotate->rotation.d[0] = dx / r;
1559 rotate->rotation.d[4] = -dy / r;
1560 rotate->rotation.d[1] = -rotate->rotation.d[4];
1561 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001562
1563 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001564 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001565 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001566 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001567 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001568
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001569 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001570 &shsurf->surface->geometry.transformation_list,
1571 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001572 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001573 wl_list_init(&shsurf->rotation.transform.link);
1574 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001575 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001576 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001577
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001578 /* We need to adjust the position of the surface
1579 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001580 cposx = surface->geometry.x + cx;
1581 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001582 dposx = rotate->center.x - cposx;
1583 dposy = rotate->center.y - cposy;
1584 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001585 weston_surface_set_position(surface,
1586 surface->geometry.x + dposx,
1587 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001588 }
1589
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001590 /* Repaint implies weston_surface_update_transform(), which
1591 * lazily applies the damage due to rotation update.
1592 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001593 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001594}
1595
1596static void
Scott Moreau447013d2012-02-18 05:05:29 -07001597rotate_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001598 uint32_t time, uint32_t button, int32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001599{
1600 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001601 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001602 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001603 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001604
1605 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001606 if (shsurf)
1607 weston_matrix_multiply(&shsurf->rotation.rotation,
1608 &rotate->rotation);
1609 shell_grab_finish(&rotate->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001610 wl_input_device_end_pointer_grab(device);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001611 free(rotate);
1612 }
1613}
1614
Scott Moreau447013d2012-02-18 05:05:29 -07001615static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001616 noop_grab_focus,
1617 rotate_grab_motion,
1618 rotate_grab_button,
1619};
1620
1621static void
1622rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001623 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001624{
1625 struct weston_surface *base_surface =
1626 (struct weston_surface *) device->pointer_focus;
1627 struct shell_surface *surface;
1628 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001629 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001630 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001631
1632 if (base_surface == NULL)
1633 return;
1634
1635 surface = get_shell_surface(base_surface);
1636 if (!surface)
1637 return;
1638
1639 switch (surface->type) {
1640 case SHELL_SURFACE_PANEL:
1641 case SHELL_SURFACE_BACKGROUND:
1642 case SHELL_SURFACE_FULLSCREEN:
1643 case SHELL_SURFACE_SCREENSAVER:
1644 return;
1645 default:
1646 break;
1647 }
1648
Pekka Paalanen460099f2012-01-20 16:48:25 +02001649 rotate = malloc(sizeof *rotate);
1650 if (!rotate)
1651 return;
1652
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001653 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001654
1655 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001656 surface->surface->geometry.width / 2,
1657 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001658 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001659
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001660 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001661
1662 dx = device->x - rotate->center.x;
1663 dy = device->y - rotate->center.y;
1664 r = sqrtf(dx * dx + dy * dy);
1665 if (r > 20.0f) {
1666 struct weston_matrix inverse;
1667
1668 weston_matrix_init(&inverse);
1669 inverse.d[0] = dx / r;
1670 inverse.d[4] = dy / r;
1671 inverse.d[1] = -inverse.d[4];
1672 inverse.d[5] = inverse.d[0];
1673 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001674
1675 weston_matrix_init(&rotate->rotation);
1676 rotate->rotation.d[0] = dx / r;
1677 rotate->rotation.d[4] = -dy / r;
1678 rotate->rotation.d[1] = -rotate->rotation.d[4];
1679 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001680 } else {
1681 weston_matrix_init(&surface->rotation.rotation);
1682 weston_matrix_init(&rotate->rotation);
1683 }
1684
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001685 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001686}
1687
1688static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001689activate(struct desktop_shell *shell, struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001690 struct weston_input_device *device)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001691{
Alex Wu21858432012-04-01 20:13:08 +08001692 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001693
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001694 weston_surface_activate(es, device);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001695
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001696 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001697 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001698 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001699 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001700 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001701
Pekka Paalanen77346a62011-11-30 16:26:35 +02001702 case SHELL_SURFACE_SCREENSAVER:
1703 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001704 if (shell->lock_surface)
1705 weston_surface_restack(es,
1706 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001707 break;
Alex Wu4539b082012-03-01 12:57:46 +08001708 case SHELL_SURFACE_FULLSCREEN:
1709 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001710 shell_stack_fullscreen(get_shell_surface(es));
Alex Wubd3354b2012-04-17 17:20:49 +08001711 shell_configure_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001712 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001713 default:
Alex Wubd3354b2012-04-17 17:20:49 +08001714 /* move the fullscreen surfaces down into the toplevel layer */
Alex Wu21858432012-04-01 20:13:08 +08001715 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1716 wl_list_for_each_reverse_safe(surf,
1717 prev,
1718 &shell->fullscreen_layer.surface_list,
1719 layer_link)
1720 weston_surface_restack(surf,
1721 &shell->toplevel_layer.surface_list);
1722 }
1723
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001724 weston_surface_restack(es,
1725 &shell->toplevel_layer.surface_list);
1726 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001727 }
1728}
1729
Alex Wu21858432012-04-01 20:13:08 +08001730/* no-op func for checking black surface */
1731static void
1732black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1733{
1734}
1735
1736static bool
1737is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1738{
1739 if (es->configure == black_surface_configure) {
1740 if (fs_surface)
1741 *fs_surface = (struct weston_surface *)es->private;
1742 return true;
1743 }
1744 return false;
1745}
1746
Kristian Høgsberg75840622011-09-06 13:48:16 -04001747static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001748click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001749 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001750 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001751{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001752 struct weston_input_device *wd = (struct weston_input_device *) device;
Tiago Vignattibe143262012-04-16 17:31:41 +03001753 struct desktop_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001754 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001755 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001756
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001757 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001758 if (!focus)
1759 return;
1760
Alex Wu21858432012-04-01 20:13:08 +08001761 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001762 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001763
Alex Wu9c35e6b2012-03-05 14:13:13 +08001764 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001765 activate(shell, focus, wd);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001766}
1767
1768static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001769lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001770{
Tiago Vignattibe143262012-04-16 17:31:41 +03001771 struct desktop_shell *shell =
1772 container_of(listener, struct desktop_shell, lock_listener);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001773 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001774 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001775 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001776
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001777 if (shell->locked) {
1778 wl_list_for_each(output, &shell->compositor->output_list, link)
1779 /* TODO: find a way to jump to other DPMS levels */
1780 if (output->set_dpms)
1781 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001782 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001783 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001784
1785 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001786
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001787 /* Hide all surfaces by removing the fullscreen, panel and
1788 * toplevel layers. This way nothing else can show or receive
1789 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001790
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001791 wl_list_remove(&shell->panel_layer.link);
1792 wl_list_remove(&shell->toplevel_layer.link);
1793 wl_list_remove(&shell->fullscreen_layer.link);
1794 wl_list_insert(&shell->compositor->cursor_layer.link,
1795 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001796
Pekka Paalanen77346a62011-11-30 16:26:35 +02001797 launch_screensaver(shell);
1798
1799 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1800 show_screensaver(shell, shsurf);
1801
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001802 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001803 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001804 weston_compositor_wake(shell->compositor);
1805 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001806 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001807
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001808 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001809 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001810
1811 /* reset keyboard foci */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001812 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1813 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001814 NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001815 }
1816
1817 /* TODO: disable bindings that should not work while locked. */
1818
1819 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001820}
1821
1822static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001823unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001824{
Tiago Vignattibe143262012-04-16 17:31:41 +03001825 struct desktop_shell *shell =
1826 container_of(listener, struct desktop_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001827
Pekka Paalanend81c2162011-11-16 13:47:34 +02001828 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001829 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001830 return;
1831 }
1832
1833 /* If desktop-shell client has gone away, unlock immediately. */
1834 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001835 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001836 return;
1837 }
1838
1839 if (shell->prepare_event_sent)
1840 return;
1841
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001842 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001843 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001844}
1845
1846static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001847center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001848{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001849 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001850 GLfloat x = (mode->width - surface->geometry.width) / 2;
1851 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001852
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001853 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001854}
1855
1856static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001857map(struct desktop_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001858 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001859{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001860 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001861 struct shell_surface *shsurf;
1862 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001863 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001864 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001865
Pekka Paalanen77346a62011-11-30 16:26:35 +02001866 shsurf = get_shell_surface(surface);
1867 if (shsurf)
1868 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001869
Pekka Paalanen60921e52012-01-25 15:55:43 +02001870 surface->geometry.width = width;
1871 surface->geometry.height = height;
1872 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001873
1874 /* initial positioning, see also configure() */
1875 switch (surface_type) {
1876 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001877 weston_surface_set_position(surface, 10 + random() % 400,
1878 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001879 break;
1880 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001881 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001882 break;
Alex Wu4539b082012-03-01 12:57:46 +08001883 case SHELL_SURFACE_FULLSCREEN:
1884 shell_map_fullscreen(shsurf);
1885 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001886 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001887 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001888 panel_height = get_output_panel_height(shell,surface->output);
1889 weston_surface_set_position(surface, surface->output->x,
1890 surface->output->y + panel_height);
1891 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001892 case SHELL_SURFACE_LOCK:
1893 center_on_output(surface, get_default_output(compositor));
1894 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001895 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001896 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001897 case SHELL_SURFACE_NONE:
1898 weston_surface_set_position(surface,
1899 surface->geometry.x + sx,
1900 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001901 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001902 default:
1903 ;
1904 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001905
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001906 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001907 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001908 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001909 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001910 wl_list_insert(&shell->background_layer.surface_list,
1911 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001912 break;
1913 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001914 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001915 wl_list_insert(&shell->panel_layer.surface_list,
1916 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001917 break;
1918 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001919 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001920 wl_list_insert(&shell->lock_layer.surface_list,
1921 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001922 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001923 break;
1924 case SHELL_SURFACE_SCREENSAVER:
1925 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001926 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001927 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001928 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001929 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001930 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001931 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001932 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001933 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001934 case SHELL_SURFACE_POPUP:
1935 case SHELL_SURFACE_TRANSIENT:
1936 parent = shsurf->parent->surface;
1937 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1938 break;
Alex Wu4539b082012-03-01 12:57:46 +08001939 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001940 case SHELL_SURFACE_NONE:
1941 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001942 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001943 wl_list_insert(&shell->toplevel_layer.surface_list,
1944 &surface->layer_link);
1945 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001946 }
1947
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001948 if (surface_type != SHELL_SURFACE_NONE) {
1949 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001950 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1951 surface->output = shsurf->output;
1952 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001953
Juan Zhao7bb92f02011-12-15 11:31:51 -05001954 switch (surface_type) {
1955 case SHELL_SURFACE_TOPLEVEL:
1956 case SHELL_SURFACE_TRANSIENT:
1957 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001958 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001959 if (!shell->locked)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001960 activate(shell, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001961 (struct weston_input_device *)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001962 compositor->input_device);
Juan Zhao7bb92f02011-12-15 11:31:51 -05001963 break;
1964 default:
1965 break;
1966 }
1967
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001968 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001969 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001970}
1971
1972static void
Tiago Vignattibe143262012-04-16 17:31:41 +03001973configure(struct desktop_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001974 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001975{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001976 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1977 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001978
Pekka Paalanen77346a62011-11-30 16:26:35 +02001979 shsurf = get_shell_surface(surface);
1980 if (shsurf)
1981 surface_type = shsurf->type;
1982
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001983 surface->geometry.x = x;
1984 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001985 surface->geometry.width = width;
1986 surface->geometry.height = height;
1987 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001988
1989 switch (surface_type) {
1990 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001991 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001992 break;
Alex Wu4539b082012-03-01 12:57:46 +08001993 case SHELL_SURFACE_FULLSCREEN:
Alex Wubd3354b2012-04-17 17:20:49 +08001994 shell_stack_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001995 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08001996 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001997 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001998 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001999 surface->geometry.x = surface->output->x;
2000 surface->geometry.y = surface->output->y +
2001 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08002002 break;
Alex Wu4539b082012-03-01 12:57:46 +08002003 case SHELL_SURFACE_TOPLEVEL:
2004 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05002005 default:
2006 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04002007 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002008
Alex Wu4539b082012-03-01 12:57:46 +08002009 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05002010 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002011 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002012
2013 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2014 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08002015 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2016 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002017 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04002018}
2019
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002020static void
2021shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2022{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002023 struct shell_surface *shsurf = get_shell_surface(es);
Tiago Vignattibe143262012-04-16 17:31:41 +03002024 struct desktop_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002025
2026 if (!weston_surface_is_mapped(es)) {
2027 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002028 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002029 es->geometry.width != es->buffer->width ||
2030 es->geometry.height != es->buffer->height) {
2031 GLfloat from_x, from_y;
2032 GLfloat to_x, to_y;
2033
2034 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2035 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2036 configure(shell, es,
2037 es->geometry.x + to_x - from_x,
2038 es->geometry.y + to_y - from_y,
2039 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03002040 shsurf->force_configure = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002041 }
2042}
2043
Tiago Vignattibe143262012-04-16 17:31:41 +03002044static int launch_desktop_shell_process(struct desktop_shell *shell);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002045
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002046static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002047desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002048{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002049 uint32_t time;
Tiago Vignattibe143262012-04-16 17:31:41 +03002050 struct desktop_shell *shell =
2051 container_of(process, struct desktop_shell, child.process);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002052
2053 shell->child.process.pid = 0;
2054 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002055
2056 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2057 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002058 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02002059 shell->child.deathstamp = time;
2060 shell->child.deathcount = 0;
2061 }
2062
2063 shell->child.deathcount++;
2064 if (shell->child.deathcount > 5) {
2065 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2066 return;
2067 }
2068
2069 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2070 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002071}
2072
2073static int
Tiago Vignattibe143262012-04-16 17:31:41 +03002074launch_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002075{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05002076 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002077
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002078 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02002079 &shell->child.process,
2080 shell_exe,
2081 desktop_shell_sigchld);
2082
2083 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002084 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002085 return 0;
2086}
2087
2088static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002089bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2090{
Tiago Vignattibe143262012-04-16 17:31:41 +03002091 struct desktop_shell *shell = data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002092
2093 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002094 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002095}
2096
Kristian Høgsberg75840622011-09-06 13:48:16 -04002097static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002098unbind_desktop_shell(struct wl_resource *resource)
2099{
Tiago Vignattibe143262012-04-16 17:31:41 +03002100 struct desktop_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05002101
2102 if (shell->locked)
2103 resume_desktop(shell);
2104
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002105 shell->child.desktop_shell = NULL;
2106 shell->prepare_event_sent = false;
2107 free(resource);
2108}
2109
2110static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002111bind_desktop_shell(struct wl_client *client,
2112 void *data, uint32_t version, uint32_t id)
2113{
Tiago Vignattibe143262012-04-16 17:31:41 +03002114 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002115 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002116
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002117 resource = wl_client_add_object(client, &desktop_shell_interface,
2118 &desktop_shell_implementation,
2119 id, shell);
2120
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002121 if (client == shell->child.client) {
2122 resource->destroy = unbind_desktop_shell;
2123 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002124 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002125 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02002126
2127 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2128 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002129 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002130}
2131
Pekka Paalanen6e168112011-11-24 11:34:05 +02002132static void
2133screensaver_set_surface(struct wl_client *client,
2134 struct wl_resource *resource,
2135 struct wl_resource *shell_surface_resource,
2136 struct wl_resource *output_resource)
2137{
Tiago Vignattibe143262012-04-16 17:31:41 +03002138 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002139 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002140 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002141
Pekka Paalanen98262232011-12-01 10:42:22 +02002142 if (reset_shell_surface_type(surface))
2143 return;
2144
Pekka Paalanen77346a62011-11-30 16:26:35 +02002145 surface->type = SHELL_SURFACE_SCREENSAVER;
2146
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05002147 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002148 surface->output = output;
2149 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002150}
2151
2152static const struct screensaver_interface screensaver_implementation = {
2153 screensaver_set_surface
2154};
2155
2156static void
2157unbind_screensaver(struct wl_resource *resource)
2158{
Tiago Vignattibe143262012-04-16 17:31:41 +03002159 struct desktop_shell *shell = resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002160
Pekka Paalanen77346a62011-11-30 16:26:35 +02002161 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002162 free(resource);
2163}
2164
2165static void
2166bind_screensaver(struct wl_client *client,
2167 void *data, uint32_t version, uint32_t id)
2168{
Tiago Vignattibe143262012-04-16 17:31:41 +03002169 struct desktop_shell *shell = data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002170 struct wl_resource *resource;
2171
2172 resource = wl_client_add_object(client, &screensaver_interface,
2173 &screensaver_implementation,
2174 id, shell);
2175
Pekka Paalanen77346a62011-11-30 16:26:35 +02002176 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02002177 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02002178 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02002179 return;
2180 }
2181
2182 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2183 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002184 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02002185}
2186
Kristian Høgsberg07045392012-02-19 18:52:44 -05002187struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03002188 struct desktop_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002189 struct weston_surface *current;
2190 struct wl_listener listener;
2191 struct wl_keyboard_grab grab;
2192};
2193
2194static void
2195switcher_next(struct switcher *switcher)
2196{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002197 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002198 struct weston_surface *surface;
2199 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002200 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002201
2202 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05002203 switch (get_shell_surface_type(surface)) {
2204 case SHELL_SURFACE_TOPLEVEL:
2205 case SHELL_SURFACE_FULLSCREEN:
2206 case SHELL_SURFACE_MAXIMIZED:
2207 if (first == NULL)
2208 first = surface;
2209 if (prev == switcher->current)
2210 next = surface;
2211 prev = surface;
2212 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002213 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002214 weston_surface_damage(surface);
2215 break;
2216 default:
2217 break;
2218 }
Alex Wu1659daa2012-04-01 20:13:09 +08002219
2220 if (is_black_surface(surface, NULL)) {
2221 surface->alpha = 64;
2222 surface->geometry.dirty = 1;
2223 weston_surface_damage(surface);
2224 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002225 }
2226
2227 if (next == NULL)
2228 next = first;
2229
Alex Wu07b26062012-03-12 16:06:01 +08002230 if (next == NULL)
2231 return;
2232
Kristian Høgsberg07045392012-02-19 18:52:44 -05002233 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002234 wl_signal_add(&next->surface.resource.destroy_signal,
2235 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002236
2237 switcher->current = next;
2238 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002239
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002240 shsurf = get_shell_surface(switcher->current);
2241 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2242 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002243}
2244
2245static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002246switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002247{
2248 struct switcher *switcher =
2249 container_of(listener, struct switcher, listener);
2250
2251 switcher_next(switcher);
2252}
2253
2254static void
2255switcher_destroy(struct switcher *switcher, uint32_t time)
2256{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002257 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002258 struct weston_surface *surface;
2259 struct weston_input_device *device =
2260 (struct weston_input_device *) switcher->grab.input_device;
2261
2262 wl_list_for_each(surface, &compositor->surface_list, link) {
2263 surface->alpha = 255;
2264 weston_surface_damage(surface);
2265 }
2266
Alex Wu07b26062012-03-12 16:06:01 +08002267 if (switcher->current)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002268 activate(switcher->shell, switcher->current, device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002269 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002270 wl_input_device_end_keyboard_grab(&device->input_device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002271 free(switcher);
2272}
2273
2274static void
2275switcher_key(struct wl_keyboard_grab *grab,
2276 uint32_t time, uint32_t key, int32_t state)
2277{
2278 struct switcher *switcher = container_of(grab, struct switcher, grab);
2279 struct weston_input_device *device =
2280 (struct weston_input_device *) grab->input_device;
2281
2282 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
2283 switcher_destroy(switcher, time);
2284 } else if (key == KEY_TAB && state) {
2285 switcher_next(switcher);
2286 }
2287};
2288
2289static const struct wl_keyboard_grab_interface switcher_grab = {
2290 switcher_key
2291};
2292
2293static void
2294switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002295 uint32_t key, uint32_t button, uint32_t axis,
2296 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002297{
Tiago Vignattibe143262012-04-16 17:31:41 +03002298 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002299 struct switcher *switcher;
2300
2301 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002302 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002303 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002304 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002305 wl_list_init(&switcher->listener.link);
2306
2307 switcher->grab.interface = &switcher_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002308 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2309 wl_input_device_set_keyboard_focus(device, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002310 switcher_next(switcher);
2311}
2312
Pekka Paalanen3c647232011-12-22 13:43:43 +02002313static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002314backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002315 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002316{
2317 struct weston_compositor *compositor = data;
2318 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002319 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002320
2321 /* TODO: we're limiting to simple use cases, where we assume just
2322 * control on the primary display. We'd have to extend later if we
2323 * ever get support for setting backlights on random desktop LCD
2324 * panels though */
2325 output = get_default_output(compositor);
2326 if (!output)
2327 return;
2328
2329 if (!output->set_backlight)
2330 return;
2331
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002332 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2333 backlight_new = output->backlight_current - 25;
2334 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2335 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002336
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002337 if (backlight_new < 5)
2338 backlight_new = 5;
2339 if (backlight_new > 255)
2340 backlight_new = 255;
2341
2342 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002343 output->set_backlight(output, output->backlight_current);
2344}
2345
2346static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002347debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002348 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002349{
Tiago Vignattibe143262012-04-16 17:31:41 +03002350 struct desktop_shell *shell = data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002351 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002352 struct weston_surface *surface;
2353
2354 if (shell->debug_repaint_surface) {
2355 weston_surface_destroy(shell->debug_repaint_surface);
2356 shell->debug_repaint_surface = NULL;
2357 } else {
2358 surface = weston_surface_create(compositor);
2359 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2360 weston_surface_configure(surface, 0, 0, 8192, 8192);
2361 wl_list_insert(&compositor->fade_layer.surface_list,
2362 &surface->layer_link);
2363 weston_surface_assign_output(surface);
2364 pixman_region32_init(&surface->input);
2365
2366 /* Here's the dirty little trick that makes the
2367 * repaint debugging work: we force an
2368 * update_transform first to update dependent state
2369 * and clear the geometry.dirty bit. Then we clear
2370 * the surface damage so it only gets repainted
2371 * piecewise as we repaint other things. */
2372
2373 weston_surface_update_transform(surface);
2374 pixman_region32_fini(&surface->damage);
2375 pixman_region32_init(&surface->damage);
2376 shell->debug_repaint_surface = surface;
2377 }
2378}
2379
2380static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002381shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002382{
Scott Moreauff1db4a2012-04-17 19:06:18 -06002383 struct weston_surface *surface;
2384 struct shell_surface *shsurf;
Tiago Vignattibe143262012-04-16 17:31:41 +03002385 struct desktop_shell *shell =
2386 container_of(listener, struct desktop_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002387
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002388 if (shell->child.client)
2389 wl_client_destroy(shell->child.client);
2390
Scott Moreauff1db4a2012-04-17 19:06:18 -06002391 wl_list_for_each(surface, &shell->compositor->surface_list, link) {
2392 shsurf = get_shell_surface(surface);
2393 if (!shsurf)
2394 continue;
2395 if (shsurf->ping_timer)
2396 free(shsurf->ping_timer);
2397 }
2398
Pekka Paalanen3c647232011-12-22 13:43:43 +02002399 free(shell->screensaver.path);
2400 free(shell);
2401}
2402
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002403int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002404shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002405
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002406WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002407shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002408{
Tiago Vignattibe143262012-04-16 17:31:41 +03002409 struct desktop_shell *shell;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002410
2411 shell = malloc(sizeof *shell);
2412 if (shell == NULL)
2413 return -1;
2414
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002415 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002416 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002417
2418 shell->destroy_listener.notify = shell_destroy;
2419 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2420 shell->lock_listener.notify = lock;
2421 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2422 shell->unlock_listener.notify = unlock;
2423 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Scott Moreauff1db4a2012-04-17 19:06:18 -06002424 ec->ping_handler = ping_handler;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002425
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002426 wl_list_init(&shell->backgrounds);
2427 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002428 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002429
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002430 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2431 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2432 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2433 weston_layer_init(&shell->background_layer,
2434 &shell->toplevel_layer.link);
2435 wl_list_init(&shell->lock_layer.surface_list);
2436
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002437 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002438
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002439 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2440 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002441 return -1;
2442
Kristian Høgsberg75840622011-09-06 13:48:16 -04002443 if (wl_display_add_global(ec->wl_display,
2444 &desktop_shell_interface,
2445 shell, bind_desktop_shell) == NULL)
2446 return -1;
2447
Pekka Paalanen6e168112011-11-24 11:34:05 +02002448 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2449 shell, bind_screensaver) == NULL)
2450 return -1;
2451
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002452 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002453 if (launch_desktop_shell_process(shell) != 0)
2454 return -1;
2455
Scott Moreau6a3633d2012-03-20 08:47:59 -06002456 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, MODIFIER_SUPER,
2457 move_binding, shell);
2458 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, MODIFIER_SUPER,
2459 resize_binding, shell);
2460 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2461 MODIFIER_CTRL | MODIFIER_ALT,
2462 terminate_binding, ec);
2463 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002464 click_to_activate_binding, shell);
Scott Moreau1b45a792012-03-22 10:58:23 -06002465 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002466 MODIFIER_SUPER | MODIFIER_ALT,
2467 surface_opacity_binding, NULL);
2468 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreau1b45a792012-03-22 10:58:23 -06002469 MODIFIER_SUPER, zoom_binding, NULL);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002470 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002471 MODIFIER_SUPER | MODIFIER_ALT,
2472 rotate_binding, NULL);
2473 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, MODIFIER_SUPER,
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002474 switcher_binding, shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002475
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002476 /* brightness */
Scott Moreau6a3633d2012-03-20 08:47:59 -06002477 weston_compositor_add_binding(ec, KEY_F9, 0, 0, MODIFIER_CTRL,
2478 backlight_binding, ec);
2479 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2480 backlight_binding, ec);
2481 weston_compositor_add_binding(ec, KEY_F10, 0, 0, MODIFIER_CTRL,
2482 backlight_binding, ec);
2483 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2484 backlight_binding, ec);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002485
Scott Moreau6a3633d2012-03-20 08:47:59 -06002486 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, MODIFIER_SUPER,
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002487 debug_repaint_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002488
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002489 return 0;
2490}