blob: 70a8e62fd8e8d28e6b970516a4c42d53db8cb821 [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
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040041struct wl_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050042 struct weston_compositor *compositor;
43 struct weston_shell shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020044
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050045 struct weston_layer fullscreen_layer;
46 struct weston_layer panel_layer;
47 struct weston_layer toplevel_layer;
48 struct weston_layer background_layer;
49 struct weston_layer lock_layer;
50
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020051 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050052 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020053 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020054 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020055
56 unsigned deathcount;
57 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020058 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020059
60 bool locked;
61 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020062
Pekka Paalanen068ae942011-11-28 14:11:15 +020063 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050064 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010065
66 struct wl_list backgrounds;
67 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020068
Pekka Paalanen77346a62011-11-30 16:26:35 +020069 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020070 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020071 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020072 struct wl_resource *binding;
73 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050074 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020075 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050076
77 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040078};
79
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050080enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020081 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050082
Pekka Paalanen57da4a82011-11-23 16:42:16 +020083 SHELL_SURFACE_PANEL,
84 SHELL_SURFACE_BACKGROUND,
85 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020086 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050087
88 SHELL_SURFACE_TOPLEVEL,
89 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050090 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +080091 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050092 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +020093};
94
Pekka Paalanen56cdea92011-11-23 16:14:12 +020095struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020096 struct wl_resource resource;
97
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050098 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020099 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500100 struct shell_surface *parent;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200101
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500102 enum shell_surface_type type;
103 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800104 bool saved_position_valid;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100105
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500106 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200107 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500108 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200109 } rotation;
110
111 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700112 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500113 uint32_t time;
114 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200115 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500116 int32_t initial_up;
117 } popup;
118
Alex Wu4539b082012-03-01 12:57:46 +0800119 struct {
120 enum wl_shell_surface_fullscreen_method type;
121 struct weston_transform transform; /* matrix from x, y */
122 uint32_t framerate;
123 struct weston_surface *black_surface;
124 } fullscreen;
125
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500126 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500127 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100128 struct wl_list link;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300129
130 int force_configure;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200131};
132
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300133struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700134 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300135 struct shell_surface *shsurf;
136 struct wl_listener shsurf_destroy_listener;
137};
138
139struct weston_move_grab {
140 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500141 int32_t dx, dy;
142};
143
Pekka Paalanen460099f2012-01-20 16:48:25 +0200144struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300145 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500146 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200147 struct {
148 int32_t x;
149 int32_t y;
150 } center;
151};
152
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500153static void
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300154destroy_shell_grab_shsurf(struct wl_listener *listener,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400155 struct wl_resource *resource)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300156{
157 struct shell_grab *grab;
158
159 grab = container_of(listener, struct shell_grab,
160 shsurf_destroy_listener);
161
162 grab->shsurf = NULL;
163}
164
165static void
166shell_grab_init(struct shell_grab *grab,
167 const struct wl_pointer_grab_interface *interface,
168 struct shell_surface *shsurf)
169{
170 grab->grab.interface = interface;
171 grab->shsurf = shsurf;
172 grab->shsurf_destroy_listener.func = destroy_shell_grab_shsurf;
173 wl_list_insert(shsurf->resource.destroy_listener_list.prev,
174 &grab->shsurf_destroy_listener.link);
175
176}
177
178static void
179shell_grab_finish(struct shell_grab *grab)
180{
181 wl_list_remove(&grab->shsurf_destroy_listener.link);
182}
183
184static void
Alex Wu4539b082012-03-01 12:57:46 +0800185center_on_output(struct weston_surface *surface,
186 struct weston_output *output);
187
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300188static struct shell_surface *
189get_shell_surface(struct weston_surface *surface);
190
Alex Wu4539b082012-03-01 12:57:46 +0800191static void
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200192shell_configuration(struct wl_shell *shell)
193{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200194 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200195 char *path = NULL;
196 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200197
198 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200199 { "path", CONFIG_KEY_STRING, &path },
200 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200201 };
202
203 struct config_section cs[] = {
204 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
205 };
206
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200207 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500208 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200209 free(config_file);
210
Pekka Paalanen7296e792011-12-07 16:22:00 +0200211 shell->screensaver.path = path;
212 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200213}
214
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200215static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400216noop_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500217 struct wl_surface *surface, int32_t x, int32_t y)
218{
219 grab->focus = NULL;
220}
221
222static void
Scott Moreau447013d2012-02-18 05:05:29 -0700223move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500224 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500225{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500226 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500227 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300228 struct shell_surface *shsurf = move->base.shsurf;
229 struct weston_surface *es;
230
231 if (!shsurf)
232 return;
233
234 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500235
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500236 weston_surface_configure(es,
237 device->x + move->dx,
238 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200239 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500240}
241
242static void
Scott Moreau447013d2012-02-18 05:05:29 -0700243move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400244 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500245{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300246 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
247 grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500248 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500249
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500250 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300251 shell_grab_finish(shell_grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400252 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500253 free(grab);
254 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500255}
256
Scott Moreau447013d2012-02-18 05:05:29 -0700257static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500258 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500259 move_grab_motion,
260 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500261};
262
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400263static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500264weston_surface_move(struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400265 struct weston_input_device *wd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500266{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500267 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300268 struct shell_surface *shsurf = get_shell_surface(es);
269
270 if (!shsurf)
271 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500272
273 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400274 if (!move)
275 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500276
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300277 shell_grab_init(&move->base, &move_grab_interface, shsurf);
278
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200279 move->dx = es->geometry.x - wd->input_device.grab_x;
280 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500281
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300282 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400283 &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500284
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400285 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400286
287 return 0;
288}
289
290static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200291shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400292 struct wl_resource *input_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400293{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500294 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200295 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400296
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500297 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400298 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500299 wd->input_device.pointer_focus != &shsurf->surface->surface)
300 return;
301
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400302 if (weston_surface_move(shsurf->surface, wd) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400303 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500304}
305
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500306struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300307 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500308 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200309 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500310};
311
312static void
Scott Moreau447013d2012-02-18 05:05:29 -0700313resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500314 uint32_t time, int32_t x, int32_t y)
315{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500316 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500317 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500318 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200319 int32_t from_x, from_y;
320 int32_t to_x, to_y;
321
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300322 if (!resize->base.shsurf)
323 return;
324
325 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200326 device->grab_x, device->grab_y,
327 &from_x, &from_y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300328 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200329 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500330
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200331 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200332 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200333 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200334 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500335 } else {
336 width = resize->width;
337 }
338
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200339 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200340 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200341 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200342 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500343 } else {
344 height = resize->height;
345 }
346
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300347 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400348 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500349}
350
351static void
Scott Moreau447013d2012-02-18 05:05:29 -0700352resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400353 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500354{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300355 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500356 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500357
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500358 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300359 shell_grab_finish(&resize->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400360 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500361 free(grab);
362 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500363}
364
Scott Moreau447013d2012-02-18 05:05:29 -0700365static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500366 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500367 resize_grab_motion,
368 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500369};
370
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400371static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500372weston_surface_resize(struct shell_surface *shsurf,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400373 struct weston_input_device *wd, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500374{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500375 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500376
Alex Wu4539b082012-03-01 12:57:46 +0800377 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
378 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500379
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200380 if (edges == 0 || edges > 15 ||
381 (edges & 3) == 3 || (edges & 12) == 12)
382 return 0;
383
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500384 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400385 if (!resize)
386 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500387
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300388 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
389
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500390 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200391 resize->width = shsurf->surface->geometry.width;
392 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400393
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300394 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400395 &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500396
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400397 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400398
399 return 0;
400}
401
402static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200403shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400404 struct wl_resource *input_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200405 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400406{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500407 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200408 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400409
Alex Wu4539b082012-03-01 12:57:46 +0800410 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
411 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400412
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500413 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400414 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500415 wd->input_device.pointer_focus != &shsurf->surface->surface)
416 return;
417
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400418 if (weston_surface_resize(shsurf, wd, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400419 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500420}
421
Juan Zhao96879df2012-02-07 08:45:41 +0800422static struct weston_output *
423get_default_output(struct weston_compositor *compositor)
424{
425 return container_of(compositor->output_list.next,
426 struct weston_output, link);
427}
428
Alex Wu4539b082012-03-01 12:57:46 +0800429static void
430shell_unset_fullscreen(struct shell_surface *shsurf)
431{
432 /* undo all fullscreen things here */
433 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
434 shsurf->fullscreen.framerate = 0;
435 wl_list_remove(&shsurf->fullscreen.transform.link);
436 wl_list_init(&shsurf->fullscreen.transform.link);
437 weston_surface_destroy(shsurf->fullscreen.black_surface);
438 shsurf->fullscreen.black_surface = NULL;
439 shsurf->fullscreen_output = NULL;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300440 shsurf->force_configure = 1;
Alex Wu4539b082012-03-01 12:57:46 +0800441 weston_surface_set_position(shsurf->surface,
442 shsurf->saved_x, shsurf->saved_y);
443}
444
Pekka Paalanen98262232011-12-01 10:42:22 +0200445static int
446reset_shell_surface_type(struct shell_surface *surface)
447{
448 switch (surface->type) {
449 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800450 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200451 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800452 case SHELL_SURFACE_MAXIMIZED:
453 surface->output = get_default_output(surface->surface->compositor);
454 weston_surface_set_position(surface->surface,
455 surface->saved_x,
456 surface->saved_y);
457 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200458 case SHELL_SURFACE_PANEL:
459 case SHELL_SURFACE_BACKGROUND:
460 wl_list_remove(&surface->link);
461 wl_list_init(&surface->link);
462 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200463 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200464 case SHELL_SURFACE_LOCK:
465 wl_resource_post_error(&surface->resource,
466 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200467 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200468 return -1;
469 case SHELL_SURFACE_NONE:
470 case SHELL_SURFACE_TOPLEVEL:
471 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500472 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200473 break;
474 }
475
476 surface->type = SHELL_SURFACE_NONE;
477 return 0;
478}
479
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500480static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200481shell_surface_set_toplevel(struct wl_client *client,
482 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400483
484{
Pekka Paalanen98262232011-12-01 10:42:22 +0200485 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400486
Pekka Paalanen98262232011-12-01 10:42:22 +0200487 if (reset_shell_surface_type(surface))
488 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400489
Pekka Paalanen98262232011-12-01 10:42:22 +0200490 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400491}
492
493static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200494shell_surface_set_transient(struct wl_client *client,
495 struct wl_resource *resource,
496 struct wl_resource *parent_resource,
497 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400498{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200499 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500500 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200501 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500502 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400503
Pekka Paalanen98262232011-12-01 10:42:22 +0200504 if (reset_shell_surface_type(shsurf))
505 return;
506
Alex Wu4539b082012-03-01 12:57:46 +0800507 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800508 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200509 weston_surface_set_position(es, pes->geometry.x + x,
510 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400511
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200512 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400513}
514
Juan Zhao96879df2012-02-07 08:45:41 +0800515static struct wl_shell *
516shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200517{
Juan Zhao96879df2012-02-07 08:45:41 +0800518 struct weston_surface *es = shsurf->surface;
519 struct weston_shell *shell = es->compositor->shell;
520
521 return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
522}
523
524static int
525get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
526{
527 struct shell_surface *priv;
528 int panel_height = 0;
529
530 if (!output)
531 return 0;
532
533 wl_list_for_each(priv, &wlshell->panels, link) {
534 if (priv->output == output) {
535 panel_height = priv->surface->geometry.height;
536 break;
537 }
538 }
539 return panel_height;
540}
541
542static void
543shell_surface_set_maximized(struct wl_client *client,
544 struct wl_resource *resource,
545 struct wl_resource *output_resource )
546{
547 struct shell_surface *shsurf = resource->data;
548 struct weston_surface *es = shsurf->surface;
549 struct wl_shell *wlshell = NULL;
550 uint32_t edges = 0, panel_height = 0;
551
552 /* get the default output, if the client set it as NULL
553 check whether the ouput is available */
554 if (output_resource)
555 shsurf->output = output_resource->data;
556 else
557 shsurf->output = get_default_output(es->compositor);
558
559 if (reset_shell_surface_type(shsurf))
560 return;
561
562 shsurf->saved_x = es->geometry.x;
563 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800564 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800565
566 wlshell = shell_surface_get_shell(shsurf);
567 panel_height = get_output_panel_height(wlshell, es->output);
568 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500569
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400570 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500571 es->output->current->width,
572 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800573
574 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200575}
576
Alex Wu21858432012-04-01 20:13:08 +0800577static void
578black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
579
Alex Wu4539b082012-03-01 12:57:46 +0800580static struct weston_surface *
581create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800582 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800583 GLfloat x, GLfloat y, int w, int h)
584{
585 struct weston_surface *surface = NULL;
586
587 surface = weston_surface_create(ec);
588 if (surface == NULL) {
589 fprintf(stderr, "no memory\n");
590 return NULL;
591 }
592
Alex Wu21858432012-04-01 20:13:08 +0800593 surface->configure = black_surface_configure;
594 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800595 weston_surface_configure(surface, x, y, w, h);
596 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
597 return surface;
598}
599
600/* Create black surface and append it to the associated fullscreen surface.
601 * Handle size dismatch and positioning according to the method. */
602static void
603shell_configure_fullscreen(struct shell_surface *shsurf)
604{
605 struct weston_output *output = shsurf->fullscreen_output;
606 struct weston_surface *surface = shsurf->surface;
607 struct weston_matrix *matrix;
608 float scale;
609
610 center_on_output(surface, output);
611
612 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500613 shsurf->fullscreen.black_surface =
614 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800615 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500616 output->x, output->y,
617 output->current->width,
618 output->current->height);
619
620 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
621 wl_list_insert(&surface->layer_link,
622 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800623 shsurf->fullscreen.black_surface->output = output;
624
625 switch (shsurf->fullscreen.type) {
626 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
627 break;
628 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
629 matrix = &shsurf->fullscreen.transform.matrix;
630 weston_matrix_init(matrix);
631 scale = (float)output->current->width/(float)surface->geometry.width;
632 weston_matrix_scale(matrix, scale, scale, 1);
633 wl_list_remove(&shsurf->fullscreen.transform.link);
634 wl_list_insert(surface->geometry.transformation_list.prev,
635 &shsurf->fullscreen.transform.link);
636 weston_surface_set_position(surface, output->x, output->y);
637 break;
638 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
639 break;
640 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
641 break;
642 default:
643 break;
644 }
645}
646
647/* make the fullscreen and black surface at the top */
648static void
649shell_stack_fullscreen(struct shell_surface *shsurf)
650{
651 struct weston_surface *surface = shsurf->surface;
652 struct wl_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800653
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500654 wl_list_remove(&surface->layer_link);
655 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800656
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500657 wl_list_insert(&shell->fullscreen_layer.surface_list,
658 &surface->layer_link);
659 wl_list_insert(&surface->layer_link,
660 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800661
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500662 weston_surface_damage(surface);
663 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800664}
665
666static void
667shell_map_fullscreen(struct shell_surface *shsurf)
668{
669 shell_configure_fullscreen(shsurf);
670 shell_stack_fullscreen(shsurf);
671}
672
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400673static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200674shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500675 struct wl_resource *resource,
676 uint32_t method,
677 uint32_t framerate,
678 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400679{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200680 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500681 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800682
683 if (output_resource)
684 shsurf->output = output_resource->data;
685 else
686 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400687
Pekka Paalanen98262232011-12-01 10:42:22 +0200688 if (reset_shell_surface_type(shsurf))
689 return;
690
Alex Wu4539b082012-03-01 12:57:46 +0800691 shsurf->fullscreen_output = shsurf->output;
692 shsurf->fullscreen.type = method;
693 shsurf->fullscreen.framerate = framerate;
694 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400695
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200696 shsurf->saved_x = es->geometry.x;
697 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800698 shsurf->saved_position_valid = true;
699
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300700 if (weston_surface_is_mapped(es))
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300701 shsurf->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500702
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400703 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500704 shsurf->output->current->width,
705 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400706}
707
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500708static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400709popup_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500710 struct wl_surface *surface, int32_t x, int32_t y)
711{
712 struct wl_input_device *device = grab->input_device;
713 struct shell_surface *priv =
714 container_of(grab, struct shell_surface, popup.grab);
715 struct wl_client *client = priv->surface->surface.resource.client;
716
Pekka Paalanencb108432012-01-19 16:25:40 +0200717 if (surface && surface->resource.client == client) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400718 wl_input_device_set_pointer_focus(device, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500719 grab->focus = surface;
720 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400721 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500722 grab->focus = NULL;
723 }
724}
725
726static void
Scott Moreau447013d2012-02-18 05:05:29 -0700727popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200728 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500729{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500730 struct wl_resource *resource;
731
732 resource = grab->input_device->pointer_focus_resource;
733 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500734 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500735}
736
737static void
Scott Moreau447013d2012-02-18 05:05:29 -0700738popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400739 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500740{
741 struct wl_resource *resource;
742 struct shell_surface *shsurf =
743 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400744 struct wl_display *display;
745 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500746
747 resource = grab->input_device->pointer_focus_resource;
748 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400749 display = wl_client_get_display(resource->client);
750 serial = wl_display_get_serial(display);
751 wl_input_device_send_button(resource, serial,
752 time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500753 } else if (state == 0 &&
754 (shsurf->popup.initial_up ||
755 time - shsurf->popup.time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500756 wl_shell_surface_send_popup_done(&shsurf->resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400757 wl_input_device_end_pointer_grab(grab->input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500758 shsurf->popup.grab.input_device = NULL;
759 }
760
761 if (state == 0)
762 shsurf->popup.initial_up = 1;
763}
764
Scott Moreau447013d2012-02-18 05:05:29 -0700765static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500766 popup_grab_focus,
767 popup_grab_motion,
768 popup_grab_button,
769};
770
771static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400772shell_map_popup(struct shell_surface *shsurf, uint32_t serial)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500773{
774 struct wl_input_device *device;
775 struct weston_surface *es = shsurf->surface;
776 struct weston_surface *parent = shsurf->parent->surface;
777
778 es->output = parent->output;
779
780 shsurf->popup.grab.interface = &popup_grab_interface;
781 device = es->compositor->input_device;
782
Pekka Paalanen938269a2012-02-07 14:19:01 +0200783 weston_surface_update_transform(parent);
784 if (parent->transform.enabled) {
785 shsurf->popup.parent_transform.matrix =
786 parent->transform.matrix;
787 } else {
788 /* construct x, y translation matrix */
789 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
790 shsurf->popup.parent_transform.matrix.d[12] =
791 parent->geometry.x;
792 shsurf->popup.parent_transform.matrix.d[13] =
793 parent->geometry.y;
794 }
795 wl_list_insert(es->geometry.transformation_list.prev,
796 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200797 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500798
799 shsurf->popup.grab.input_device = device;
800 shsurf->popup.time = device->grab_time;
801 shsurf->popup.initial_up = 0;
802
Scott Moreau447013d2012-02-18 05:05:29 -0700803 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400804 &shsurf->popup.grab);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500805}
806
807static void
808shell_surface_set_popup(struct wl_client *client,
809 struct wl_resource *resource,
810 struct wl_resource *input_device_resource,
811 uint32_t time,
812 struct wl_resource *parent_resource,
813 int32_t x, int32_t y, uint32_t flags)
814{
815 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500816
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500817 shsurf->type = SHELL_SURFACE_POPUP;
818 shsurf->parent = parent_resource->data;
819 shsurf->popup.x = x;
820 shsurf->popup.y = y;
821}
822
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200823static const struct wl_shell_surface_interface shell_surface_implementation = {
824 shell_surface_move,
825 shell_surface_resize,
826 shell_surface_set_toplevel,
827 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500828 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +0800829 shell_surface_set_popup,
830 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200831};
832
833static void
834destroy_shell_surface(struct wl_resource *resource)
835{
836 struct shell_surface *shsurf = resource->data;
837
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500838 if (shsurf->popup.grab.input_device)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400839 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500840
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200841 /* in case cleaning up a dead client destroys shell_surface first */
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300842 if (shsurf->surface) {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200843 wl_list_remove(&shsurf->surface_destroy_listener.link);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300844 shsurf->surface->configure = NULL;
845 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200846
Alex Wuaa08e2d2012-03-05 11:01:40 +0800847 if (shsurf->fullscreen.black_surface)
848 weston_surface_destroy(shsurf->fullscreen.black_surface);
849
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200850 wl_list_remove(&shsurf->link);
851 free(shsurf);
852}
853
854static void
855shell_handle_surface_destroy(struct wl_listener *listener,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400856 struct wl_resource *resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200857{
858 struct shell_surface *shsurf = container_of(listener,
859 struct shell_surface,
860 surface_destroy_listener);
861
862 shsurf->surface = NULL;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400863 wl_resource_destroy(&shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200864}
865
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200866static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500867get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200868{
869 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
870 struct wl_listener *listener;
871
872 /* search the destroy listener list for our callback */
873 wl_list_for_each(listener, lst, link) {
874 if (listener->func == shell_handle_surface_destroy) {
875 return container_of(listener, struct shell_surface,
876 surface_destroy_listener);
877 }
878 }
879
880 return NULL;
881}
882
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200883static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300884shell_surface_configure(struct weston_surface *, int32_t, int32_t);
885
886static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200887shell_get_shell_surface(struct wl_client *client,
888 struct wl_resource *resource,
889 uint32_t id,
890 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200891{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500892 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200893 struct shell_surface *shsurf;
894
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200895 if (get_shell_surface(surface)) {
896 wl_resource_post_error(surface_resource,
897 WL_DISPLAY_ERROR_INVALID_OBJECT,
898 "wl_shell::get_shell_surface already requested");
899 return;
900 }
901
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300902 if (surface->configure) {
903 wl_resource_post_error(surface_resource,
904 WL_DISPLAY_ERROR_INVALID_OBJECT,
905 "surface->configure already set");
906 return;
907 }
908
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200909 shsurf = calloc(1, sizeof *shsurf);
910 if (!shsurf) {
911 wl_resource_post_no_memory(resource);
912 return;
913 }
914
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300915 surface->configure = shell_surface_configure;
916
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200917 shsurf->resource.destroy = destroy_shell_surface;
918 shsurf->resource.object.id = id;
919 shsurf->resource.object.interface = &wl_shell_surface_interface;
920 shsurf->resource.object.implementation =
921 (void (**)(void)) &shell_surface_implementation;
922 shsurf->resource.data = shsurf;
923
Alex Wu4539b082012-03-01 12:57:46 +0800924 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200925 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +0800926 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
927 shsurf->fullscreen.framerate = 0;
928 shsurf->fullscreen.black_surface = NULL;
929 wl_list_init(&shsurf->fullscreen.transform.link);
930
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200931 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
932 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
933 &shsurf->surface_destroy_listener.link);
934
935 /* init link so its safe to always remove it in destroy_shell_surface */
936 wl_list_init(&shsurf->link);
937
Pekka Paalanen460099f2012-01-20 16:48:25 +0200938 /* empty when not in use */
939 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500940 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +0200941
Pekka Paalanen98262232011-12-01 10:42:22 +0200942 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200943
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200944 wl_client_add_resource(client, &shsurf->resource);
945}
946
947static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200948 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500949};
950
Kristian Høgsberg07937562011-04-12 17:25:42 -0400951static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500952handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200953{
954 proc->pid = 0;
955}
956
957static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200958launch_screensaver(struct wl_shell *shell)
959{
960 if (shell->screensaver.binding)
961 return;
962
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200963 if (!shell->screensaver.path)
964 return;
965
Kristian Høgsberg32bed572012-03-01 17:11:36 -0500966 if (shell->screensaver.process.pid != 0) {
967 fprintf(stderr, "old screensaver still running\n");
968 return;
969 }
970
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500971 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200972 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200973 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200974 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200975}
976
977static void
978terminate_screensaver(struct wl_shell *shell)
979{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200980 if (shell->screensaver.process.pid == 0)
981 return;
982
983 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200984}
985
986static void
987show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
988{
989 struct wl_list *list;
990
991 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500992 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200993 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500994 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200995
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500996 wl_list_remove(&surface->surface->layer_link);
997 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200998 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200999 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001000}
1001
1002static void
1003hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
1004{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001005 wl_list_remove(&surface->surface->layer_link);
1006 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001007 surface->surface->output = NULL;
1008}
1009
1010static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001011desktop_shell_set_background(struct wl_client *client,
1012 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001013 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001014 struct wl_resource *surface_resource)
1015{
1016 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001017 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001018 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001019 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001020
Pekka Paalanen98262232011-12-01 10:42:22 +02001021 if (reset_shell_surface_type(shsurf))
1022 return;
1023
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001024 wl_list_for_each(priv, &shell->backgrounds, link) {
1025 if (priv->output == output_resource->data) {
1026 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001027 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001028 wl_list_remove(&priv->link);
1029 break;
1030 }
1031 }
1032
Pekka Paalanen068ae942011-11-28 14:11:15 +02001033 shsurf->type = SHELL_SURFACE_BACKGROUND;
1034 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001035
Pekka Paalanen068ae942011-11-28 14:11:15 +02001036 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001037
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001038 weston_surface_set_position(surface, shsurf->output->x,
1039 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001040
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001041 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001042 surface_resource,
1043 shsurf->output->current->width,
1044 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001045}
1046
1047static void
1048desktop_shell_set_panel(struct wl_client *client,
1049 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001050 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001051 struct wl_resource *surface_resource)
1052{
1053 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001054 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001055 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001056 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001057
Pekka Paalanen98262232011-12-01 10:42:22 +02001058 if (reset_shell_surface_type(shsurf))
1059 return;
1060
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001061 wl_list_for_each(priv, &shell->panels, link) {
1062 if (priv->output == output_resource->data) {
1063 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001064 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001065 wl_list_remove(&priv->link);
1066 break;
1067 }
1068 }
1069
Pekka Paalanen068ae942011-11-28 14:11:15 +02001070 shsurf->type = SHELL_SURFACE_PANEL;
1071 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001072
Pekka Paalanen068ae942011-11-28 14:11:15 +02001073 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001074
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001075 weston_surface_set_position(surface, shsurf->output->x,
1076 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001077
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001078 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001079 surface_resource,
1080 shsurf->output->current->width,
1081 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001082}
1083
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001084static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001085handle_lock_surface_destroy(struct wl_listener *listener,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001086 struct wl_resource *resource)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001087{
1088 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +02001089 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001090
1091 fprintf(stderr, "lock surface gone\n");
1092 shell->lock_surface = NULL;
1093}
1094
1095static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001096desktop_shell_set_lock_surface(struct wl_client *client,
1097 struct wl_resource *resource,
1098 struct wl_resource *surface_resource)
1099{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001100 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001101 struct shell_surface *surface = surface_resource->data;
1102
1103 if (reset_shell_surface_type(surface))
1104 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001105
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001106 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001107
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001108 if (!shell->locked)
1109 return;
1110
Pekka Paalanen98262232011-12-01 10:42:22 +02001111 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001112
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001113 shell->lock_surface_listener.func = handle_lock_surface_destroy;
1114 wl_list_insert(&surface_resource->destroy_listener_list,
1115 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001116
Pekka Paalanen068ae942011-11-28 14:11:15 +02001117 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001118}
1119
1120static void
1121resume_desktop(struct wl_shell *shell)
1122{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001123 struct shell_surface *tmp;
1124
1125 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1126 hide_screensaver(shell, tmp);
1127
1128 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001129
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001130 wl_list_remove(&shell->lock_layer.link);
1131 wl_list_insert(&shell->compositor->cursor_layer.link,
1132 &shell->fullscreen_layer.link);
1133 wl_list_insert(&shell->fullscreen_layer.link,
1134 &shell->panel_layer.link);
1135 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001136
1137 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001138 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001139 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001140 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001141}
1142
1143static void
1144desktop_shell_unlock(struct wl_client *client,
1145 struct wl_resource *resource)
1146{
1147 struct wl_shell *shell = resource->data;
1148
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001149 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001150
1151 if (shell->locked)
1152 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001153}
1154
Kristian Høgsberg75840622011-09-06 13:48:16 -04001155static const struct desktop_shell_interface desktop_shell_implementation = {
1156 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001157 desktop_shell_set_panel,
1158 desktop_shell_set_lock_surface,
1159 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001160};
1161
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001162static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001163get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001164{
1165 struct shell_surface *shsurf;
1166
1167 shsurf = get_shell_surface(surface);
1168 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001169 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001170 return shsurf->type;
1171}
1172
Kristian Høgsberg75840622011-09-06 13:48:16 -04001173static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001174move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001175 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001176{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001177 struct weston_surface *surface =
1178 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001179
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001180 if (surface == NULL)
1181 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001182
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001183 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001184 case SHELL_SURFACE_PANEL:
1185 case SHELL_SURFACE_BACKGROUND:
1186 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001187 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001188 return;
1189 default:
1190 break;
1191 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001192
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001193 weston_surface_move(surface, (struct weston_input_device *) device);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001194}
1195
1196static void
1197resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001198 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001199{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001200 struct weston_surface *surface =
1201 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001202 uint32_t edges = 0;
1203 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001204 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001205
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001206 if (surface == NULL)
1207 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001208
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001209 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001210 if (!shsurf)
1211 return;
1212
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001213 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001214 case SHELL_SURFACE_PANEL:
1215 case SHELL_SURFACE_BACKGROUND:
1216 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001217 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001218 return;
1219 default:
1220 break;
1221 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001222
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001223 weston_surface_from_global(surface,
1224 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001225
Pekka Paalanen60921e52012-01-25 15:55:43 +02001226 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001227 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001228 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001229 edges |= 0;
1230 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001231 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001232
Pekka Paalanen60921e52012-01-25 15:55:43 +02001233 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001234 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001235 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001236 edges |= 0;
1237 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001238 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001239
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001240 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001241 edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001242}
1243
1244static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001245surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1246 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1247{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001248 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001249 struct shell_surface *shsurf;
1250 struct weston_surface *surface =
1251 (struct weston_surface *) device->pointer_focus;
1252
1253 if (surface == NULL)
1254 return;
1255
1256 shsurf = get_shell_surface(surface);
1257 if (!shsurf)
1258 return;
1259
1260 switch (shsurf->type) {
1261 case SHELL_SURFACE_BACKGROUND:
1262 case SHELL_SURFACE_SCREENSAVER:
1263 return;
1264 default:
1265 break;
1266 }
1267
1268 surface->alpha += value * step;
1269
1270 if (surface->alpha > 255)
1271 surface->alpha = 255;
1272 if (surface->alpha < step)
1273 surface->alpha = step;
1274
1275 surface->geometry.dirty = 1;
1276 weston_surface_damage(surface);
1277}
1278
1279static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001280zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001281 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001282{
1283 struct weston_input_device *wd = (struct weston_input_device *) device;
1284 struct weston_compositor *compositor = wd->compositor;
1285 struct weston_output *output;
1286
1287 wl_list_for_each(output, &compositor->output_list, link) {
1288 if (pixman_region32_contains_point(&output->region,
1289 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001290 output->zoom.active = 1;
1291 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001292
1293 if (output->zoom.level >= 1.0) {
1294 output->zoom.active = 0;
1295 output->zoom.level = 1.0;
1296 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001297
1298 if (output->zoom.level < output->zoom.increment)
1299 output->zoom.level = output->zoom.increment;
1300
1301 weston_output_update_zoom(output, device->x, device->y);
1302 }
1303 }
1304}
1305
Scott Moreauccbf29d2012-02-22 14:21:41 -07001306static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001307terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001308 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001309{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001310 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001311
1312 if (state)
1313 wl_display_terminate(compositor->wl_display);
1314}
1315
1316static void
Scott Moreau447013d2012-02-18 05:05:29 -07001317rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001318 uint32_t time, int32_t x, int32_t y)
1319{
1320 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001321 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001322 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001323 struct shell_surface *shsurf = rotate->base.shsurf;
1324 struct weston_surface *surface;
1325 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1326
1327 if (!shsurf)
1328 return;
1329
1330 surface = shsurf->surface;
1331
1332 cx = 0.5f * surface->geometry.width;
1333 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001334
1335 dx = device->x - rotate->center.x;
1336 dy = device->y - rotate->center.y;
1337 r = sqrtf(dx * dx + dy * dy);
1338
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001339 wl_list_remove(&shsurf->rotation.transform.link);
1340 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001341
1342 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001343 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001344 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001345
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001346 weston_matrix_init(&rotate->rotation);
1347 rotate->rotation.d[0] = dx / r;
1348 rotate->rotation.d[4] = -dy / r;
1349 rotate->rotation.d[1] = -rotate->rotation.d[4];
1350 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001351
1352 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001353 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001354 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001355 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001356 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001357
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001358 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001359 &shsurf->surface->geometry.transformation_list,
1360 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001361 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001362 wl_list_init(&shsurf->rotation.transform.link);
1363 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001364 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001365 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001366
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001367 /* We need to adjust the position of the surface
1368 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001369 cposx = surface->geometry.x + cx;
1370 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001371 dposx = rotate->center.x - cposx;
1372 dposy = rotate->center.y - cposy;
1373 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001374 weston_surface_set_position(surface,
1375 surface->geometry.x + dposx,
1376 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001377 }
1378
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001379 /* Repaint implies weston_surface_update_transform(), which
1380 * lazily applies the damage due to rotation update.
1381 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001382 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001383}
1384
1385static void
Scott Moreau447013d2012-02-18 05:05:29 -07001386rotate_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001387 uint32_t time, uint32_t button, int32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001388{
1389 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001390 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001391 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001392 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001393
1394 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001395 if (shsurf)
1396 weston_matrix_multiply(&shsurf->rotation.rotation,
1397 &rotate->rotation);
1398 shell_grab_finish(&rotate->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001399 wl_input_device_end_pointer_grab(device);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001400 free(rotate);
1401 }
1402}
1403
Scott Moreau447013d2012-02-18 05:05:29 -07001404static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001405 noop_grab_focus,
1406 rotate_grab_motion,
1407 rotate_grab_button,
1408};
1409
1410static void
1411rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001412 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001413{
1414 struct weston_surface *base_surface =
1415 (struct weston_surface *) device->pointer_focus;
1416 struct shell_surface *surface;
1417 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001418 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001419 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001420
1421 if (base_surface == NULL)
1422 return;
1423
1424 surface = get_shell_surface(base_surface);
1425 if (!surface)
1426 return;
1427
1428 switch (surface->type) {
1429 case SHELL_SURFACE_PANEL:
1430 case SHELL_SURFACE_BACKGROUND:
1431 case SHELL_SURFACE_FULLSCREEN:
1432 case SHELL_SURFACE_SCREENSAVER:
1433 return;
1434 default:
1435 break;
1436 }
1437
Pekka Paalanen460099f2012-01-20 16:48:25 +02001438 rotate = malloc(sizeof *rotate);
1439 if (!rotate)
1440 return;
1441
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001442 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001443
1444 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001445 surface->surface->geometry.width / 2,
1446 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001447 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001448
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001449 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001450
1451 dx = device->x - rotate->center.x;
1452 dy = device->y - rotate->center.y;
1453 r = sqrtf(dx * dx + dy * dy);
1454 if (r > 20.0f) {
1455 struct weston_matrix inverse;
1456
1457 weston_matrix_init(&inverse);
1458 inverse.d[0] = dx / r;
1459 inverse.d[4] = dy / r;
1460 inverse.d[1] = -inverse.d[4];
1461 inverse.d[5] = inverse.d[0];
1462 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001463
1464 weston_matrix_init(&rotate->rotation);
1465 rotate->rotation.d[0] = dx / r;
1466 rotate->rotation.d[4] = -dy / r;
1467 rotate->rotation.d[1] = -rotate->rotation.d[4];
1468 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001469 } else {
1470 weston_matrix_init(&surface->rotation.rotation);
1471 weston_matrix_init(&rotate->rotation);
1472 }
1473
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001474 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001475}
1476
1477static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001478activate(struct weston_shell *base, struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001479 struct weston_input_device *device)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001480{
1481 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001482 struct weston_compositor *compositor = shell->compositor;
Alex Wu21858432012-04-01 20:13:08 +08001483 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001484
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001485 weston_surface_activate(es, device);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001486
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001487 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001488 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001489
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001490 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001491 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001492 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001493 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001494 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001495
Pekka Paalanen77346a62011-11-30 16:26:35 +02001496 case SHELL_SURFACE_SCREENSAVER:
1497 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001498 if (shell->lock_surface)
1499 weston_surface_restack(es,
1500 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001501 break;
Alex Wu4539b082012-03-01 12:57:46 +08001502 case SHELL_SURFACE_FULLSCREEN:
1503 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001504 shell_stack_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001505 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001506 default:
Alex Wu21858432012-04-01 20:13:08 +08001507 /* move the fullscreen surfaces down into the toplevel layer */
1508 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1509 wl_list_for_each_reverse_safe(surf,
1510 prev,
1511 &shell->fullscreen_layer.surface_list,
1512 layer_link)
1513 weston_surface_restack(surf,
1514 &shell->toplevel_layer.surface_list);
1515 }
1516
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001517 weston_surface_restack(es,
1518 &shell->toplevel_layer.surface_list);
1519 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001520 }
1521}
1522
Alex Wu21858432012-04-01 20:13:08 +08001523/* no-op func for checking black surface */
1524static void
1525black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1526{
1527}
1528
1529static bool
1530is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1531{
1532 if (es->configure == black_surface_configure) {
1533 if (fs_surface)
1534 *fs_surface = (struct weston_surface *)es->private;
1535 return true;
1536 }
1537 return false;
1538}
1539
Kristian Høgsberg75840622011-09-06 13:48:16 -04001540static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001541click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001542 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001543 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001544{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001545 struct weston_input_device *wd = (struct weston_input_device *) device;
1546 struct weston_compositor *compositor = data;
1547 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001548 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001549
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001550 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001551 if (!focus)
1552 return;
1553
Alex Wu21858432012-04-01 20:13:08 +08001554 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001555 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001556
Alex Wu9c35e6b2012-03-05 14:13:13 +08001557 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001558 activate(compositor->shell, focus, wd);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001559}
1560
1561static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001562lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001563{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001564 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001565 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001566 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001567 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001568
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001569 if (shell->locked) {
1570 wl_list_for_each(output, &shell->compositor->output_list, link)
1571 /* TODO: find a way to jump to other DPMS levels */
1572 if (output->set_dpms)
1573 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001574 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001575 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001576
1577 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001578
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001579 /* Hide all surfaces by removing the fullscreen, panel and
1580 * toplevel layers. This way nothing else can show or receive
1581 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001582
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001583 wl_list_remove(&shell->panel_layer.link);
1584 wl_list_remove(&shell->toplevel_layer.link);
1585 wl_list_remove(&shell->fullscreen_layer.link);
1586 wl_list_insert(&shell->compositor->cursor_layer.link,
1587 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001588
Pekka Paalanen77346a62011-11-30 16:26:35 +02001589 launch_screensaver(shell);
1590
1591 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1592 show_screensaver(shell, shsurf);
1593
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001594 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001595 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001596 weston_compositor_wake(shell->compositor);
1597 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001598 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001599
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001600 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001601 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001602
1603 /* reset keyboard foci */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001604 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1605 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001606 NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001607 }
1608
1609 /* TODO: disable bindings that should not work while locked. */
1610
1611 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001612}
1613
1614static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001615unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001616{
1617 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1618
Pekka Paalanend81c2162011-11-16 13:47:34 +02001619 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001620 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001621 return;
1622 }
1623
1624 /* If desktop-shell client has gone away, unlock immediately. */
1625 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001626 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001627 return;
1628 }
1629
1630 if (shell->prepare_event_sent)
1631 return;
1632
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001633 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001634 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001635}
1636
1637static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001638center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001639{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001640 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001641 GLfloat x = (mode->width - surface->geometry.width) / 2;
1642 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001643
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001644 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001645}
1646
1647static void
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001648map(struct weston_shell *base, struct weston_surface *surface,
1649 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001650{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001651 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001652 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001653 struct shell_surface *shsurf;
1654 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001655 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001656 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001657
Pekka Paalanen77346a62011-11-30 16:26:35 +02001658 shsurf = get_shell_surface(surface);
1659 if (shsurf)
1660 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001661
Pekka Paalanen60921e52012-01-25 15:55:43 +02001662 surface->geometry.width = width;
1663 surface->geometry.height = height;
1664 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001665
1666 /* initial positioning, see also configure() */
1667 switch (surface_type) {
1668 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001669 weston_surface_set_position(surface, 10 + random() % 400,
1670 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001671 break;
1672 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001673 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001674 break;
Alex Wu4539b082012-03-01 12:57:46 +08001675 case SHELL_SURFACE_FULLSCREEN:
1676 shell_map_fullscreen(shsurf);
1677 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001678 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001679 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001680 panel_height = get_output_panel_height(shell,surface->output);
1681 weston_surface_set_position(surface, surface->output->x,
1682 surface->output->y + panel_height);
1683 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001684 case SHELL_SURFACE_LOCK:
1685 center_on_output(surface, get_default_output(compositor));
1686 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001687 case SHELL_SURFACE_POPUP:
1688 shell_map_popup(shsurf, shsurf->popup.time);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001689 case SHELL_SURFACE_NONE:
1690 weston_surface_set_position(surface,
1691 surface->geometry.x + sx,
1692 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001693 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001694 default:
1695 ;
1696 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001697
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001698 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001699 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001700 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001701 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001702 wl_list_insert(&shell->background_layer.surface_list,
1703 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001704 break;
1705 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001706 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001707 wl_list_insert(&shell->panel_layer.surface_list,
1708 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001709 break;
1710 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001711 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001712 wl_list_insert(&shell->lock_layer.surface_list,
1713 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001714 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001715 break;
1716 case SHELL_SURFACE_SCREENSAVER:
1717 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001718 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001719 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001720 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001721 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001722 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001723 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001724 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001725 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001726 case SHELL_SURFACE_POPUP:
1727 case SHELL_SURFACE_TRANSIENT:
1728 parent = shsurf->parent->surface;
1729 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1730 break;
Alex Wu4539b082012-03-01 12:57:46 +08001731 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001732 case SHELL_SURFACE_NONE:
1733 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001734 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001735 wl_list_insert(&shell->toplevel_layer.surface_list,
1736 &surface->layer_link);
1737 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001738 }
1739
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001740 if (surface_type != SHELL_SURFACE_NONE) {
1741 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001742 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1743 surface->output = shsurf->output;
1744 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001745
Juan Zhao7bb92f02011-12-15 11:31:51 -05001746 switch (surface_type) {
1747 case SHELL_SURFACE_TOPLEVEL:
1748 case SHELL_SURFACE_TRANSIENT:
1749 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001750 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001751 if (!shell->locked)
1752 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001753 (struct weston_input_device *)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001754 compositor->input_device);
Juan Zhao7bb92f02011-12-15 11:31:51 -05001755 break;
1756 default:
1757 break;
1758 }
1759
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001760 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001761 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001762}
1763
1764static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001765configure(struct weston_shell *base, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001766 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001767{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001768 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001769 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Alex Wu4539b082012-03-01 12:57:46 +08001770 enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001771 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001772
Pekka Paalanen77346a62011-11-30 16:26:35 +02001773 shsurf = get_shell_surface(surface);
1774 if (shsurf)
1775 surface_type = shsurf->type;
1776
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001777 surface->geometry.x = x;
1778 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001779 surface->geometry.width = width;
1780 surface->geometry.height = height;
1781 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001782
1783 switch (surface_type) {
1784 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001785 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001786 break;
Alex Wu4539b082012-03-01 12:57:46 +08001787 case SHELL_SURFACE_FULLSCREEN:
1788 shell_configure_fullscreen(shsurf);
1789 if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
1790 shell_stack_fullscreen(shsurf);
1791 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001792 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001793 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001794 surface->geometry.x = surface->output->x;
1795 surface->geometry.y = surface->output->y +
1796 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001797 break;
Alex Wu4539b082012-03-01 12:57:46 +08001798 case SHELL_SURFACE_TOPLEVEL:
1799 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001800 default:
1801 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001802 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001803
Alex Wu4539b082012-03-01 12:57:46 +08001804 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001805 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001806 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001807
1808 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1809 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001810 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1811 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001812 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001813}
1814
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001815static void
1816shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1817{
1818 struct weston_shell *shell = es->compositor->shell;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001819 struct shell_surface *shsurf = get_shell_surface(es);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001820
1821 if (!weston_surface_is_mapped(es)) {
1822 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001823 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001824 es->geometry.width != es->buffer->width ||
1825 es->geometry.height != es->buffer->height) {
1826 GLfloat from_x, from_y;
1827 GLfloat to_x, to_y;
1828
1829 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
1830 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
1831 configure(shell, es,
1832 es->geometry.x + to_x - from_x,
1833 es->geometry.y + to_y - from_y,
1834 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001835 shsurf->force_configure = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001836 }
1837}
1838
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001839static int launch_desktop_shell_process(struct wl_shell *shell);
1840
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001841static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001842desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001843{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001844 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001845 struct wl_shell *shell =
1846 container_of(process, struct wl_shell, child.process);
1847
1848 shell->child.process.pid = 0;
1849 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001850
1851 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1852 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001853 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001854 shell->child.deathstamp = time;
1855 shell->child.deathcount = 0;
1856 }
1857
1858 shell->child.deathcount++;
1859 if (shell->child.deathcount > 5) {
1860 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1861 return;
1862 }
1863
1864 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1865 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001866}
1867
1868static int
1869launch_desktop_shell_process(struct wl_shell *shell)
1870{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001871 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001872
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001873 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001874 &shell->child.process,
1875 shell_exe,
1876 desktop_shell_sigchld);
1877
1878 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001879 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001880 return 0;
1881}
1882
1883static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001884bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1885{
1886 struct wl_shell *shell = data;
1887
1888 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001889 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001890}
1891
Kristian Høgsberg75840622011-09-06 13:48:16 -04001892static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001893unbind_desktop_shell(struct wl_resource *resource)
1894{
1895 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001896
1897 if (shell->locked)
1898 resume_desktop(shell);
1899
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001900 shell->child.desktop_shell = NULL;
1901 shell->prepare_event_sent = false;
1902 free(resource);
1903}
1904
1905static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001906bind_desktop_shell(struct wl_client *client,
1907 void *data, uint32_t version, uint32_t id)
1908{
1909 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001910 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001911
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001912 resource = wl_client_add_object(client, &desktop_shell_interface,
1913 &desktop_shell_implementation,
1914 id, shell);
1915
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001916 if (client == shell->child.client) {
1917 resource->destroy = unbind_desktop_shell;
1918 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001919 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001920 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001921
1922 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1923 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001924 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001925}
1926
Pekka Paalanen6e168112011-11-24 11:34:05 +02001927static void
1928screensaver_set_surface(struct wl_client *client,
1929 struct wl_resource *resource,
1930 struct wl_resource *shell_surface_resource,
1931 struct wl_resource *output_resource)
1932{
1933 struct wl_shell *shell = resource->data;
1934 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001935 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001936
Pekka Paalanen98262232011-12-01 10:42:22 +02001937 if (reset_shell_surface_type(surface))
1938 return;
1939
Pekka Paalanen77346a62011-11-30 16:26:35 +02001940 surface->type = SHELL_SURFACE_SCREENSAVER;
1941
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001942 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001943 surface->output = output;
1944 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001945}
1946
1947static const struct screensaver_interface screensaver_implementation = {
1948 screensaver_set_surface
1949};
1950
1951static void
1952unbind_screensaver(struct wl_resource *resource)
1953{
1954 struct wl_shell *shell = resource->data;
1955
Pekka Paalanen77346a62011-11-30 16:26:35 +02001956 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001957 free(resource);
1958}
1959
1960static void
1961bind_screensaver(struct wl_client *client,
1962 void *data, uint32_t version, uint32_t id)
1963{
1964 struct wl_shell *shell = data;
1965 struct wl_resource *resource;
1966
1967 resource = wl_client_add_object(client, &screensaver_interface,
1968 &screensaver_implementation,
1969 id, shell);
1970
Pekka Paalanen77346a62011-11-30 16:26:35 +02001971 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001972 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001973 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001974 return;
1975 }
1976
1977 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1978 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001979 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001980}
1981
Kristian Høgsberg07045392012-02-19 18:52:44 -05001982struct switcher {
1983 struct weston_compositor *compositor;
1984 struct weston_surface *current;
1985 struct wl_listener listener;
1986 struct wl_keyboard_grab grab;
1987};
1988
1989static void
1990switcher_next(struct switcher *switcher)
1991{
1992 struct weston_compositor *compositor = switcher->compositor;
1993 struct weston_surface *surface;
1994 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04001995 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05001996
1997 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05001998 switch (get_shell_surface_type(surface)) {
1999 case SHELL_SURFACE_TOPLEVEL:
2000 case SHELL_SURFACE_FULLSCREEN:
2001 case SHELL_SURFACE_MAXIMIZED:
2002 if (first == NULL)
2003 first = surface;
2004 if (prev == switcher->current)
2005 next = surface;
2006 prev = surface;
2007 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002008 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002009 weston_surface_damage(surface);
2010 break;
2011 default:
2012 break;
2013 }
Alex Wu1659daa2012-04-01 20:13:09 +08002014
2015 if (is_black_surface(surface, NULL)) {
2016 surface->alpha = 64;
2017 surface->geometry.dirty = 1;
2018 weston_surface_damage(surface);
2019 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002020 }
2021
2022 if (next == NULL)
2023 next = first;
2024
Alex Wu07b26062012-03-12 16:06:01 +08002025 if (next == NULL)
2026 return;
2027
Kristian Høgsberg07045392012-02-19 18:52:44 -05002028 wl_list_remove(&switcher->listener.link);
2029 wl_list_insert(next->surface.resource.destroy_listener_list.prev,
2030 &switcher->listener.link);
2031
2032 switcher->current = next;
2033 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002034
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002035 shsurf = get_shell_surface(switcher->current);
2036 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2037 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002038}
2039
2040static void
2041switcher_handle_surface_destroy(struct wl_listener *listener,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002042 struct wl_resource *resource)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002043{
2044 struct switcher *switcher =
2045 container_of(listener, struct switcher, listener);
2046
2047 switcher_next(switcher);
2048}
2049
2050static void
2051switcher_destroy(struct switcher *switcher, uint32_t time)
2052{
2053 struct weston_compositor *compositor = switcher->compositor;
2054 struct weston_surface *surface;
2055 struct weston_input_device *device =
2056 (struct weston_input_device *) switcher->grab.input_device;
2057
2058 wl_list_for_each(surface, &compositor->surface_list, link) {
2059 surface->alpha = 255;
2060 weston_surface_damage(surface);
2061 }
2062
Alex Wu07b26062012-03-12 16:06:01 +08002063 if (switcher->current)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002064 activate(compositor->shell, switcher->current, device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002065 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002066 wl_input_device_end_keyboard_grab(&device->input_device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002067 free(switcher);
2068}
2069
2070static void
2071switcher_key(struct wl_keyboard_grab *grab,
2072 uint32_t time, uint32_t key, int32_t state)
2073{
2074 struct switcher *switcher = container_of(grab, struct switcher, grab);
2075 struct weston_input_device *device =
2076 (struct weston_input_device *) grab->input_device;
2077
2078 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
2079 switcher_destroy(switcher, time);
2080 } else if (key == KEY_TAB && state) {
2081 switcher_next(switcher);
2082 }
2083};
2084
2085static const struct wl_keyboard_grab_interface switcher_grab = {
2086 switcher_key
2087};
2088
2089static void
2090switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002091 uint32_t key, uint32_t button, uint32_t axis,
2092 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002093{
2094 struct weston_compositor *compositor = data;
2095 struct switcher *switcher;
2096
2097 switcher = malloc(sizeof *switcher);
2098 switcher->compositor = compositor;
2099 switcher->current = NULL;
2100 switcher->listener.func = switcher_handle_surface_destroy;
2101 wl_list_init(&switcher->listener.link);
2102
2103 switcher->grab.interface = &switcher_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002104 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2105 wl_input_device_set_keyboard_focus(device, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002106 switcher_next(switcher);
2107}
2108
Pekka Paalanen3c647232011-12-22 13:43:43 +02002109static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002110backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002111 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002112{
2113 struct weston_compositor *compositor = data;
2114 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002115 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002116
2117 /* TODO: we're limiting to simple use cases, where we assume just
2118 * control on the primary display. We'd have to extend later if we
2119 * ever get support for setting backlights on random desktop LCD
2120 * panels though */
2121 output = get_default_output(compositor);
2122 if (!output)
2123 return;
2124
2125 if (!output->set_backlight)
2126 return;
2127
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002128 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2129 backlight_new = output->backlight_current - 25;
2130 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2131 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002132
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002133 if (backlight_new < 5)
2134 backlight_new = 5;
2135 if (backlight_new > 255)
2136 backlight_new = 255;
2137
2138 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002139 output->set_backlight(output, output->backlight_current);
2140}
2141
2142static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002143debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002144 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002145{
2146 struct weston_compositor *compositor = data;
2147 struct wl_shell *shell =
2148 container_of(compositor->shell, struct wl_shell, shell);
2149 struct weston_surface *surface;
2150
2151 if (shell->debug_repaint_surface) {
2152 weston_surface_destroy(shell->debug_repaint_surface);
2153 shell->debug_repaint_surface = NULL;
2154 } else {
2155 surface = weston_surface_create(compositor);
2156 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2157 weston_surface_configure(surface, 0, 0, 8192, 8192);
2158 wl_list_insert(&compositor->fade_layer.surface_list,
2159 &surface->layer_link);
2160 weston_surface_assign_output(surface);
2161 pixman_region32_init(&surface->input);
2162
2163 /* Here's the dirty little trick that makes the
2164 * repaint debugging work: we force an
2165 * update_transform first to update dependent state
2166 * and clear the geometry.dirty bit. Then we clear
2167 * the surface damage so it only gets repainted
2168 * piecewise as we repaint other things. */
2169
2170 weston_surface_update_transform(surface);
2171 pixman_region32_fini(&surface->damage);
2172 pixman_region32_init(&surface->damage);
2173 shell->debug_repaint_surface = surface;
2174 }
2175}
2176
2177static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05002178shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002179{
2180 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
2181
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002182 if (shell->child.client)
2183 wl_client_destroy(shell->child.client);
2184
Pekka Paalanen3c647232011-12-22 13:43:43 +02002185 free(shell->screensaver.path);
2186 free(shell);
2187}
2188
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002189int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002190shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002191
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002192WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002193shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002194{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002195 struct wl_shell *shell;
2196
2197 shell = malloc(sizeof *shell);
2198 if (shell == NULL)
2199 return -1;
2200
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002201 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002202 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002203 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002204 shell->shell.unlock = unlock;
Pekka Paalanen3c647232011-12-22 13:43:43 +02002205 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002206
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002207 wl_list_init(&shell->backgrounds);
2208 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002209 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002210
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002211 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2212 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2213 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2214 weston_layer_init(&shell->background_layer,
2215 &shell->toplevel_layer.link);
2216 wl_list_init(&shell->lock_layer.surface_list);
2217
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002218 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002219
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002220 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2221 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002222 return -1;
2223
Kristian Høgsberg75840622011-09-06 13:48:16 -04002224 if (wl_display_add_global(ec->wl_display,
2225 &desktop_shell_interface,
2226 shell, bind_desktop_shell) == NULL)
2227 return -1;
2228
Pekka Paalanen6e168112011-11-24 11:34:05 +02002229 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2230 shell, bind_screensaver) == NULL)
2231 return -1;
2232
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002233 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002234 if (launch_desktop_shell_process(shell) != 0)
2235 return -1;
2236
Scott Moreau6a3633d2012-03-20 08:47:59 -06002237 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, MODIFIER_SUPER,
2238 move_binding, shell);
2239 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, MODIFIER_SUPER,
2240 resize_binding, shell);
2241 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2242 MODIFIER_CTRL | MODIFIER_ALT,
2243 terminate_binding, ec);
2244 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2245 click_to_activate_binding, ec);
Scott Moreau1b45a792012-03-22 10:58:23 -06002246 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002247 MODIFIER_SUPER | MODIFIER_ALT,
2248 surface_opacity_binding, NULL);
2249 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreau1b45a792012-03-22 10:58:23 -06002250 MODIFIER_SUPER, zoom_binding, NULL);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002251 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002252 MODIFIER_SUPER | MODIFIER_ALT,
2253 rotate_binding, NULL);
2254 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, MODIFIER_SUPER,
2255 switcher_binding, ec);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002256
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002257 /* brightness */
Scott Moreau6a3633d2012-03-20 08:47:59 -06002258 weston_compositor_add_binding(ec, KEY_F9, 0, 0, MODIFIER_CTRL,
2259 backlight_binding, ec);
2260 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2261 backlight_binding, ec);
2262 weston_compositor_add_binding(ec, KEY_F10, 0, 0, MODIFIER_CTRL,
2263 backlight_binding, ec);
2264 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2265 backlight_binding, ec);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002266
Scott Moreau6a3633d2012-03-20 08:47:59 -06002267 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, MODIFIER_SUPER,
2268 debug_repaint_binding, ec);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002269
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002270 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002271
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002272 return 0;
2273}