blob: b7bd4be104866a8b2d518b2b1bb452ee6a1ae66a [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
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400154destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300155{
156 struct shell_grab *grab;
157
158 grab = container_of(listener, struct shell_grab,
159 shsurf_destroy_listener);
160
161 grab->shsurf = NULL;
162}
163
164static void
165shell_grab_init(struct shell_grab *grab,
166 const struct wl_pointer_grab_interface *interface,
167 struct shell_surface *shsurf)
168{
169 grab->grab.interface = interface;
170 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400171 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
172 wl_signal_add(&shsurf->resource.destroy_signal,
173 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300174
175}
176
177static void
178shell_grab_finish(struct shell_grab *grab)
179{
180 wl_list_remove(&grab->shsurf_destroy_listener.link);
181}
182
183static void
Alex Wu4539b082012-03-01 12:57:46 +0800184center_on_output(struct weston_surface *surface,
185 struct weston_output *output);
186
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300187static struct shell_surface *
188get_shell_surface(struct weston_surface *surface);
189
Alex Wu4539b082012-03-01 12:57:46 +0800190static void
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200191shell_configuration(struct wl_shell *shell)
192{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200193 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200194 char *path = NULL;
195 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200196
197 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200198 { "path", CONFIG_KEY_STRING, &path },
199 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200200 };
201
202 struct config_section cs[] = {
203 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
204 };
205
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200206 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500207 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200208 free(config_file);
209
Pekka Paalanen7296e792011-12-07 16:22:00 +0200210 shell->screensaver.path = path;
211 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200212}
213
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200214static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400215noop_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500216 struct wl_surface *surface, int32_t x, int32_t y)
217{
218 grab->focus = NULL;
219}
220
221static void
Scott Moreau447013d2012-02-18 05:05:29 -0700222move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500223 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500224{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500225 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500226 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300227 struct shell_surface *shsurf = move->base.shsurf;
228 struct weston_surface *es;
229
230 if (!shsurf)
231 return;
232
233 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500234
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500235 weston_surface_configure(es,
236 device->x + move->dx,
237 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200238 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500239}
240
241static void
Scott Moreau447013d2012-02-18 05:05:29 -0700242move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400243 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500244{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300245 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
246 grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500247 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500248
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500249 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300250 shell_grab_finish(shell_grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400251 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500252 free(grab);
253 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500254}
255
Scott Moreau447013d2012-02-18 05:05:29 -0700256static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500257 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500258 move_grab_motion,
259 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500260};
261
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400262static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500263weston_surface_move(struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400264 struct weston_input_device *wd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500265{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500266 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300267 struct shell_surface *shsurf = get_shell_surface(es);
268
269 if (!shsurf)
270 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500271
272 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400273 if (!move)
274 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500275
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300276 shell_grab_init(&move->base, &move_grab_interface, shsurf);
277
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200278 move->dx = es->geometry.x - wd->input_device.grab_x;
279 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500280
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300281 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400282 &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500283
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400284 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400285
286 return 0;
287}
288
289static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200290shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400291 struct wl_resource *input_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400292{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500293 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200294 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400295
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500296 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400297 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500298 wd->input_device.pointer_focus != &shsurf->surface->surface)
299 return;
300
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400301 if (weston_surface_move(shsurf->surface, wd) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400302 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500303}
304
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500305struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300306 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500307 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200308 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500309};
310
311static void
Scott Moreau447013d2012-02-18 05:05:29 -0700312resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500313 uint32_t time, int32_t x, int32_t y)
314{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500315 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500316 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500317 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200318 int32_t from_x, from_y;
319 int32_t to_x, to_y;
320
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300321 if (!resize->base.shsurf)
322 return;
323
324 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200325 device->grab_x, device->grab_y,
326 &from_x, &from_y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300327 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200328 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500329
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200330 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200331 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200332 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200333 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500334 } else {
335 width = resize->width;
336 }
337
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200338 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200339 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200340 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200341 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500342 } else {
343 height = resize->height;
344 }
345
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300346 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400347 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500348}
349
350static void
Scott Moreau447013d2012-02-18 05:05:29 -0700351resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400352 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500353{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300354 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500355 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500356
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500357 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300358 shell_grab_finish(&resize->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400359 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500360 free(grab);
361 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500362}
363
Scott Moreau447013d2012-02-18 05:05:29 -0700364static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500365 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500366 resize_grab_motion,
367 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500368};
369
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400370static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500371weston_surface_resize(struct shell_surface *shsurf,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400372 struct weston_input_device *wd, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500373{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500374 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500375
Alex Wu4539b082012-03-01 12:57:46 +0800376 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
377 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500378
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200379 if (edges == 0 || edges > 15 ||
380 (edges & 3) == 3 || (edges & 12) == 12)
381 return 0;
382
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500383 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400384 if (!resize)
385 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500386
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300387 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
388
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500389 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200390 resize->width = shsurf->surface->geometry.width;
391 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400392
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300393 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400394 &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500395
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400396 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400397
398 return 0;
399}
400
401static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200402shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400403 struct wl_resource *input_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200404 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400405{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500406 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200407 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400408
Alex Wu4539b082012-03-01 12:57:46 +0800409 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
410 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400411
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500412 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400413 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500414 wd->input_device.pointer_focus != &shsurf->surface->surface)
415 return;
416
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400417 if (weston_surface_resize(shsurf, wd, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400418 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500419}
420
Juan Zhao96879df2012-02-07 08:45:41 +0800421static struct weston_output *
422get_default_output(struct weston_compositor *compositor)
423{
424 return container_of(compositor->output_list.next,
425 struct weston_output, link);
426}
427
Alex Wu4539b082012-03-01 12:57:46 +0800428static void
429shell_unset_fullscreen(struct shell_surface *shsurf)
430{
431 /* undo all fullscreen things here */
432 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
433 shsurf->fullscreen.framerate = 0;
434 wl_list_remove(&shsurf->fullscreen.transform.link);
435 wl_list_init(&shsurf->fullscreen.transform.link);
436 weston_surface_destroy(shsurf->fullscreen.black_surface);
437 shsurf->fullscreen.black_surface = NULL;
438 shsurf->fullscreen_output = NULL;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300439 shsurf->force_configure = 1;
Alex Wu4539b082012-03-01 12:57:46 +0800440 weston_surface_set_position(shsurf->surface,
441 shsurf->saved_x, shsurf->saved_y);
442}
443
Pekka Paalanen98262232011-12-01 10:42:22 +0200444static int
445reset_shell_surface_type(struct shell_surface *surface)
446{
447 switch (surface->type) {
448 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800449 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200450 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800451 case SHELL_SURFACE_MAXIMIZED:
452 surface->output = get_default_output(surface->surface->compositor);
453 weston_surface_set_position(surface->surface,
454 surface->saved_x,
455 surface->saved_y);
456 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200457 case SHELL_SURFACE_PANEL:
458 case SHELL_SURFACE_BACKGROUND:
459 wl_list_remove(&surface->link);
460 wl_list_init(&surface->link);
461 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200462 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200463 case SHELL_SURFACE_LOCK:
464 wl_resource_post_error(&surface->resource,
465 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200466 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200467 return -1;
468 case SHELL_SURFACE_NONE:
469 case SHELL_SURFACE_TOPLEVEL:
470 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500471 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200472 break;
473 }
474
475 surface->type = SHELL_SURFACE_NONE;
476 return 0;
477}
478
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500479static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200480shell_surface_set_toplevel(struct wl_client *client,
481 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400482
483{
Pekka Paalanen98262232011-12-01 10:42:22 +0200484 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400485
Pekka Paalanen98262232011-12-01 10:42:22 +0200486 if (reset_shell_surface_type(surface))
487 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400488
Pekka Paalanen98262232011-12-01 10:42:22 +0200489 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400490}
491
492static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200493shell_surface_set_transient(struct wl_client *client,
494 struct wl_resource *resource,
495 struct wl_resource *parent_resource,
496 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400497{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200498 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500499 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200500 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500501 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400502
Pekka Paalanen98262232011-12-01 10:42:22 +0200503 if (reset_shell_surface_type(shsurf))
504 return;
505
Alex Wu4539b082012-03-01 12:57:46 +0800506 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800507 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200508 weston_surface_set_position(es, pes->geometry.x + x,
509 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400510
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200511 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400512}
513
Juan Zhao96879df2012-02-07 08:45:41 +0800514static struct wl_shell *
515shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200516{
Juan Zhao96879df2012-02-07 08:45:41 +0800517 struct weston_surface *es = shsurf->surface;
518 struct weston_shell *shell = es->compositor->shell;
519
520 return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
521}
522
523static int
524get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
525{
526 struct shell_surface *priv;
527 int panel_height = 0;
528
529 if (!output)
530 return 0;
531
532 wl_list_for_each(priv, &wlshell->panels, link) {
533 if (priv->output == output) {
534 panel_height = priv->surface->geometry.height;
535 break;
536 }
537 }
538 return panel_height;
539}
540
541static void
542shell_surface_set_maximized(struct wl_client *client,
543 struct wl_resource *resource,
544 struct wl_resource *output_resource )
545{
546 struct shell_surface *shsurf = resource->data;
547 struct weston_surface *es = shsurf->surface;
548 struct wl_shell *wlshell = NULL;
549 uint32_t edges = 0, panel_height = 0;
550
551 /* get the default output, if the client set it as NULL
552 check whether the ouput is available */
553 if (output_resource)
554 shsurf->output = output_resource->data;
555 else
556 shsurf->output = get_default_output(es->compositor);
557
558 if (reset_shell_surface_type(shsurf))
559 return;
560
561 shsurf->saved_x = es->geometry.x;
562 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800563 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800564
565 wlshell = shell_surface_get_shell(shsurf);
566 panel_height = get_output_panel_height(wlshell, es->output);
567 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500568
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400569 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500570 es->output->current->width,
571 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800572
573 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200574}
575
Alex Wu21858432012-04-01 20:13:08 +0800576static void
577black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
578
Alex Wu4539b082012-03-01 12:57:46 +0800579static struct weston_surface *
580create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800581 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800582 GLfloat x, GLfloat y, int w, int h)
583{
584 struct weston_surface *surface = NULL;
585
586 surface = weston_surface_create(ec);
587 if (surface == NULL) {
588 fprintf(stderr, "no memory\n");
589 return NULL;
590 }
591
Alex Wu21858432012-04-01 20:13:08 +0800592 surface->configure = black_surface_configure;
593 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800594 weston_surface_configure(surface, x, y, w, h);
595 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
596 return surface;
597}
598
599/* Create black surface and append it to the associated fullscreen surface.
600 * Handle size dismatch and positioning according to the method. */
601static void
602shell_configure_fullscreen(struct shell_surface *shsurf)
603{
604 struct weston_output *output = shsurf->fullscreen_output;
605 struct weston_surface *surface = shsurf->surface;
606 struct weston_matrix *matrix;
607 float scale;
608
609 center_on_output(surface, output);
610
611 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500612 shsurf->fullscreen.black_surface =
613 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800614 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500615 output->x, output->y,
616 output->current->width,
617 output->current->height);
618
619 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
620 wl_list_insert(&surface->layer_link,
621 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800622 shsurf->fullscreen.black_surface->output = output;
623
624 switch (shsurf->fullscreen.type) {
625 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
626 break;
627 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
628 matrix = &shsurf->fullscreen.transform.matrix;
629 weston_matrix_init(matrix);
630 scale = (float)output->current->width/(float)surface->geometry.width;
631 weston_matrix_scale(matrix, scale, scale, 1);
632 wl_list_remove(&shsurf->fullscreen.transform.link);
633 wl_list_insert(surface->geometry.transformation_list.prev,
634 &shsurf->fullscreen.transform.link);
635 weston_surface_set_position(surface, output->x, output->y);
636 break;
637 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
638 break;
639 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
640 break;
641 default:
642 break;
643 }
644}
645
646/* make the fullscreen and black surface at the top */
647static void
648shell_stack_fullscreen(struct shell_surface *shsurf)
649{
650 struct weston_surface *surface = shsurf->surface;
651 struct wl_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800652
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500653 wl_list_remove(&surface->layer_link);
654 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800655
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500656 wl_list_insert(&shell->fullscreen_layer.surface_list,
657 &surface->layer_link);
658 wl_list_insert(&surface->layer_link,
659 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800660
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500661 weston_surface_damage(surface);
662 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800663}
664
665static void
666shell_map_fullscreen(struct shell_surface *shsurf)
667{
668 shell_configure_fullscreen(shsurf);
669 shell_stack_fullscreen(shsurf);
670}
671
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400672static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200673shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500674 struct wl_resource *resource,
675 uint32_t method,
676 uint32_t framerate,
677 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400678{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200679 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500680 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800681
682 if (output_resource)
683 shsurf->output = output_resource->data;
684 else
685 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400686
Pekka Paalanen98262232011-12-01 10:42:22 +0200687 if (reset_shell_surface_type(shsurf))
688 return;
689
Alex Wu4539b082012-03-01 12:57:46 +0800690 shsurf->fullscreen_output = shsurf->output;
691 shsurf->fullscreen.type = method;
692 shsurf->fullscreen.framerate = framerate;
693 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400694
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200695 shsurf->saved_x = es->geometry.x;
696 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800697 shsurf->saved_position_valid = true;
698
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300699 if (weston_surface_is_mapped(es))
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300700 shsurf->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500701
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400702 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500703 shsurf->output->current->width,
704 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400705}
706
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500707static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400708popup_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500709 struct wl_surface *surface, int32_t x, int32_t y)
710{
711 struct wl_input_device *device = grab->input_device;
712 struct shell_surface *priv =
713 container_of(grab, struct shell_surface, popup.grab);
714 struct wl_client *client = priv->surface->surface.resource.client;
715
Pekka Paalanencb108432012-01-19 16:25:40 +0200716 if (surface && surface->resource.client == client) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400717 wl_input_device_set_pointer_focus(device, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500718 grab->focus = surface;
719 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400720 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500721 grab->focus = NULL;
722 }
723}
724
725static void
Scott Moreau447013d2012-02-18 05:05:29 -0700726popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200727 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500728{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500729 struct wl_resource *resource;
730
731 resource = grab->input_device->pointer_focus_resource;
732 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500733 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500734}
735
736static void
Scott Moreau447013d2012-02-18 05:05:29 -0700737popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400738 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500739{
740 struct wl_resource *resource;
741 struct shell_surface *shsurf =
742 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400743 struct wl_display *display;
744 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500745
746 resource = grab->input_device->pointer_focus_resource;
747 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400748 display = wl_client_get_display(resource->client);
749 serial = wl_display_get_serial(display);
750 wl_input_device_send_button(resource, serial,
751 time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500752 } else if (state == 0 &&
753 (shsurf->popup.initial_up ||
754 time - shsurf->popup.time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500755 wl_shell_surface_send_popup_done(&shsurf->resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400756 wl_input_device_end_pointer_grab(grab->input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500757 shsurf->popup.grab.input_device = NULL;
758 }
759
760 if (state == 0)
761 shsurf->popup.initial_up = 1;
762}
763
Scott Moreau447013d2012-02-18 05:05:29 -0700764static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500765 popup_grab_focus,
766 popup_grab_motion,
767 popup_grab_button,
768};
769
770static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400771shell_map_popup(struct shell_surface *shsurf, uint32_t serial)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500772{
773 struct wl_input_device *device;
774 struct weston_surface *es = shsurf->surface;
775 struct weston_surface *parent = shsurf->parent->surface;
776
777 es->output = parent->output;
778
779 shsurf->popup.grab.interface = &popup_grab_interface;
780 device = es->compositor->input_device;
781
Pekka Paalanen938269a2012-02-07 14:19:01 +0200782 weston_surface_update_transform(parent);
783 if (parent->transform.enabled) {
784 shsurf->popup.parent_transform.matrix =
785 parent->transform.matrix;
786 } else {
787 /* construct x, y translation matrix */
788 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
789 shsurf->popup.parent_transform.matrix.d[12] =
790 parent->geometry.x;
791 shsurf->popup.parent_transform.matrix.d[13] =
792 parent->geometry.y;
793 }
794 wl_list_insert(es->geometry.transformation_list.prev,
795 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200796 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500797
798 shsurf->popup.grab.input_device = device;
799 shsurf->popup.time = device->grab_time;
800 shsurf->popup.initial_up = 0;
801
Scott Moreau447013d2012-02-18 05:05:29 -0700802 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400803 &shsurf->popup.grab);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500804}
805
806static void
807shell_surface_set_popup(struct wl_client *client,
808 struct wl_resource *resource,
809 struct wl_resource *input_device_resource,
810 uint32_t time,
811 struct wl_resource *parent_resource,
812 int32_t x, int32_t y, uint32_t flags)
813{
814 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500815
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500816 shsurf->type = SHELL_SURFACE_POPUP;
817 shsurf->parent = parent_resource->data;
818 shsurf->popup.x = x;
819 shsurf->popup.y = y;
820}
821
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200822static const struct wl_shell_surface_interface shell_surface_implementation = {
823 shell_surface_move,
824 shell_surface_resize,
825 shell_surface_set_toplevel,
826 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500827 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +0800828 shell_surface_set_popup,
829 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200830};
831
832static void
833destroy_shell_surface(struct wl_resource *resource)
834{
835 struct shell_surface *shsurf = resource->data;
836
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500837 if (shsurf->popup.grab.input_device)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400838 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500839
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200840 /* in case cleaning up a dead client destroys shell_surface first */
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300841 if (shsurf->surface) {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200842 wl_list_remove(&shsurf->surface_destroy_listener.link);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300843 shsurf->surface->configure = NULL;
844 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200845
Alex Wuaa08e2d2012-03-05 11:01:40 +0800846 if (shsurf->fullscreen.black_surface)
847 weston_surface_destroy(shsurf->fullscreen.black_surface);
848
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200849 wl_list_remove(&shsurf->link);
850 free(shsurf);
851}
852
853static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400854shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200855{
856 struct shell_surface *shsurf = container_of(listener,
857 struct shell_surface,
858 surface_destroy_listener);
859
860 shsurf->surface = NULL;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400861 wl_resource_destroy(&shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200862}
863
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200864static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500865get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200866{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200867 struct wl_listener *listener;
868
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400869 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
870 shell_handle_surface_destroy);
871 if (listener)
872 return container_of(listener, struct shell_surface,
873 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200874
875 return NULL;
876}
877
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200878static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300879shell_surface_configure(struct weston_surface *, int32_t, int32_t);
880
881static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200882shell_get_shell_surface(struct wl_client *client,
883 struct wl_resource *resource,
884 uint32_t id,
885 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200886{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500887 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200888 struct shell_surface *shsurf;
889
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200890 if (get_shell_surface(surface)) {
891 wl_resource_post_error(surface_resource,
892 WL_DISPLAY_ERROR_INVALID_OBJECT,
893 "wl_shell::get_shell_surface already requested");
894 return;
895 }
896
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300897 if (surface->configure) {
898 wl_resource_post_error(surface_resource,
899 WL_DISPLAY_ERROR_INVALID_OBJECT,
900 "surface->configure already set");
901 return;
902 }
903
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200904 shsurf = calloc(1, sizeof *shsurf);
905 if (!shsurf) {
906 wl_resource_post_no_memory(resource);
907 return;
908 }
909
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300910 surface->configure = shell_surface_configure;
911
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200912 shsurf->resource.destroy = destroy_shell_surface;
913 shsurf->resource.object.id = id;
914 shsurf->resource.object.interface = &wl_shell_surface_interface;
915 shsurf->resource.object.implementation =
916 (void (**)(void)) &shell_surface_implementation;
917 shsurf->resource.data = shsurf;
918
Alex Wu4539b082012-03-01 12:57:46 +0800919 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200920 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +0800921 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
922 shsurf->fullscreen.framerate = 0;
923 shsurf->fullscreen.black_surface = NULL;
924 wl_list_init(&shsurf->fullscreen.transform.link);
925
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400926 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
927 wl_signal_add(&surface->surface.resource.destroy_signal,
928 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200929
930 /* init link so its safe to always remove it in destroy_shell_surface */
931 wl_list_init(&shsurf->link);
932
Pekka Paalanen460099f2012-01-20 16:48:25 +0200933 /* empty when not in use */
934 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500935 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +0200936
Pekka Paalanen98262232011-12-01 10:42:22 +0200937 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200938
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200939 wl_client_add_resource(client, &shsurf->resource);
940}
941
942static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200943 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500944};
945
Kristian Høgsberg07937562011-04-12 17:25:42 -0400946static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500947handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200948{
949 proc->pid = 0;
950}
951
952static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200953launch_screensaver(struct wl_shell *shell)
954{
955 if (shell->screensaver.binding)
956 return;
957
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200958 if (!shell->screensaver.path)
959 return;
960
Kristian Høgsberg32bed572012-03-01 17:11:36 -0500961 if (shell->screensaver.process.pid != 0) {
962 fprintf(stderr, "old screensaver still running\n");
963 return;
964 }
965
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500966 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200967 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200968 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200969 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200970}
971
972static void
973terminate_screensaver(struct wl_shell *shell)
974{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200975 if (shell->screensaver.process.pid == 0)
976 return;
977
978 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200979}
980
981static void
982show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
983{
984 struct wl_list *list;
985
986 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500987 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200988 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500989 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200990
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500991 wl_list_remove(&surface->surface->layer_link);
992 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200993 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200994 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200995}
996
997static void
998hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
999{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001000 wl_list_remove(&surface->surface->layer_link);
1001 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001002 surface->surface->output = NULL;
1003}
1004
1005static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001006desktop_shell_set_background(struct wl_client *client,
1007 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001008 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001009 struct wl_resource *surface_resource)
1010{
1011 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001012 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001013 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001014 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001015
Pekka Paalanen98262232011-12-01 10:42:22 +02001016 if (reset_shell_surface_type(shsurf))
1017 return;
1018
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001019 wl_list_for_each(priv, &shell->backgrounds, link) {
1020 if (priv->output == output_resource->data) {
1021 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001022 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001023 wl_list_remove(&priv->link);
1024 break;
1025 }
1026 }
1027
Pekka Paalanen068ae942011-11-28 14:11:15 +02001028 shsurf->type = SHELL_SURFACE_BACKGROUND;
1029 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001030
Pekka Paalanen068ae942011-11-28 14:11:15 +02001031 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001032
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001033 weston_surface_set_position(surface, shsurf->output->x,
1034 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001035
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001036 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001037 surface_resource,
1038 shsurf->output->current->width,
1039 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001040}
1041
1042static void
1043desktop_shell_set_panel(struct wl_client *client,
1044 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001045 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001046 struct wl_resource *surface_resource)
1047{
1048 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001049 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001050 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001051 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001052
Pekka Paalanen98262232011-12-01 10:42:22 +02001053 if (reset_shell_surface_type(shsurf))
1054 return;
1055
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001056 wl_list_for_each(priv, &shell->panels, link) {
1057 if (priv->output == output_resource->data) {
1058 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001059 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001060 wl_list_remove(&priv->link);
1061 break;
1062 }
1063 }
1064
Pekka Paalanen068ae942011-11-28 14:11:15 +02001065 shsurf->type = SHELL_SURFACE_PANEL;
1066 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001067
Pekka Paalanen068ae942011-11-28 14:11:15 +02001068 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001069
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001070 weston_surface_set_position(surface, shsurf->output->x,
1071 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001072
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001073 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001074 surface_resource,
1075 shsurf->output->current->width,
1076 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001077}
1078
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001079static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001080handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001081{
1082 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +02001083 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001084
1085 fprintf(stderr, "lock surface gone\n");
1086 shell->lock_surface = NULL;
1087}
1088
1089static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001090desktop_shell_set_lock_surface(struct wl_client *client,
1091 struct wl_resource *resource,
1092 struct wl_resource *surface_resource)
1093{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001094 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001095 struct shell_surface *surface = surface_resource->data;
1096
1097 if (reset_shell_surface_type(surface))
1098 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001099
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001100 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001101
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001102 if (!shell->locked)
1103 return;
1104
Pekka Paalanen98262232011-12-01 10:42:22 +02001105 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001106
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001107 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1108 wl_signal_add(&surface_resource->destroy_signal,
1109 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001110
Pekka Paalanen068ae942011-11-28 14:11:15 +02001111 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001112}
1113
1114static void
1115resume_desktop(struct wl_shell *shell)
1116{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001117 struct shell_surface *tmp;
1118
1119 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1120 hide_screensaver(shell, tmp);
1121
1122 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001123
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001124 wl_list_remove(&shell->lock_layer.link);
1125 wl_list_insert(&shell->compositor->cursor_layer.link,
1126 &shell->fullscreen_layer.link);
1127 wl_list_insert(&shell->fullscreen_layer.link,
1128 &shell->panel_layer.link);
1129 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001130
1131 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001132 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001133 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001134 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001135}
1136
1137static void
1138desktop_shell_unlock(struct wl_client *client,
1139 struct wl_resource *resource)
1140{
1141 struct wl_shell *shell = resource->data;
1142
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001143 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001144
1145 if (shell->locked)
1146 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001147}
1148
Kristian Høgsberg75840622011-09-06 13:48:16 -04001149static const struct desktop_shell_interface desktop_shell_implementation = {
1150 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001151 desktop_shell_set_panel,
1152 desktop_shell_set_lock_surface,
1153 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001154};
1155
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001156static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001157get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001158{
1159 struct shell_surface *shsurf;
1160
1161 shsurf = get_shell_surface(surface);
1162 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001163 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001164 return shsurf->type;
1165}
1166
Kristian Høgsberg75840622011-09-06 13:48:16 -04001167static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001168move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001169 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001170{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001171 struct weston_surface *surface =
1172 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001173
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001174 if (surface == NULL)
1175 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001176
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001177 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001178 case SHELL_SURFACE_PANEL:
1179 case SHELL_SURFACE_BACKGROUND:
1180 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001181 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001182 return;
1183 default:
1184 break;
1185 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001186
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001187 weston_surface_move(surface, (struct weston_input_device *) device);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001188}
1189
1190static void
1191resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001192 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001193{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001194 struct weston_surface *surface =
1195 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001196 uint32_t edges = 0;
1197 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001198 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001199
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001200 if (surface == NULL)
1201 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001202
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001203 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001204 if (!shsurf)
1205 return;
1206
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001207 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001208 case SHELL_SURFACE_PANEL:
1209 case SHELL_SURFACE_BACKGROUND:
1210 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001211 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001212 return;
1213 default:
1214 break;
1215 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001216
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001217 weston_surface_from_global(surface,
1218 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001219
Pekka Paalanen60921e52012-01-25 15:55:43 +02001220 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001221 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001222 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001223 edges |= 0;
1224 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001225 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001226
Pekka Paalanen60921e52012-01-25 15:55:43 +02001227 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001228 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001229 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001230 edges |= 0;
1231 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001232 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001233
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001234 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001235 edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001236}
1237
1238static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001239surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1240 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1241{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001242 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001243 struct shell_surface *shsurf;
1244 struct weston_surface *surface =
1245 (struct weston_surface *) device->pointer_focus;
1246
1247 if (surface == NULL)
1248 return;
1249
1250 shsurf = get_shell_surface(surface);
1251 if (!shsurf)
1252 return;
1253
1254 switch (shsurf->type) {
1255 case SHELL_SURFACE_BACKGROUND:
1256 case SHELL_SURFACE_SCREENSAVER:
1257 return;
1258 default:
1259 break;
1260 }
1261
1262 surface->alpha += value * step;
1263
1264 if (surface->alpha > 255)
1265 surface->alpha = 255;
1266 if (surface->alpha < step)
1267 surface->alpha = step;
1268
1269 surface->geometry.dirty = 1;
1270 weston_surface_damage(surface);
1271}
1272
1273static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001274zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001275 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001276{
1277 struct weston_input_device *wd = (struct weston_input_device *) device;
1278 struct weston_compositor *compositor = wd->compositor;
1279 struct weston_output *output;
1280
1281 wl_list_for_each(output, &compositor->output_list, link) {
1282 if (pixman_region32_contains_point(&output->region,
1283 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001284 output->zoom.active = 1;
1285 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001286
1287 if (output->zoom.level >= 1.0) {
1288 output->zoom.active = 0;
1289 output->zoom.level = 1.0;
1290 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001291
1292 if (output->zoom.level < output->zoom.increment)
1293 output->zoom.level = output->zoom.increment;
1294
1295 weston_output_update_zoom(output, device->x, device->y);
1296 }
1297 }
1298}
1299
Scott Moreauccbf29d2012-02-22 14:21:41 -07001300static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001301terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001302 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001303{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001304 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001305
1306 if (state)
1307 wl_display_terminate(compositor->wl_display);
1308}
1309
1310static void
Scott Moreau447013d2012-02-18 05:05:29 -07001311rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001312 uint32_t time, int32_t x, int32_t y)
1313{
1314 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001315 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001316 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001317 struct shell_surface *shsurf = rotate->base.shsurf;
1318 struct weston_surface *surface;
1319 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1320
1321 if (!shsurf)
1322 return;
1323
1324 surface = shsurf->surface;
1325
1326 cx = 0.5f * surface->geometry.width;
1327 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001328
1329 dx = device->x - rotate->center.x;
1330 dy = device->y - rotate->center.y;
1331 r = sqrtf(dx * dx + dy * dy);
1332
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001333 wl_list_remove(&shsurf->rotation.transform.link);
1334 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001335
1336 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001337 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001338 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001339
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001340 weston_matrix_init(&rotate->rotation);
1341 rotate->rotation.d[0] = dx / r;
1342 rotate->rotation.d[4] = -dy / r;
1343 rotate->rotation.d[1] = -rotate->rotation.d[4];
1344 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001345
1346 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001347 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001348 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001349 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001350 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001351
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001352 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001353 &shsurf->surface->geometry.transformation_list,
1354 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001355 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001356 wl_list_init(&shsurf->rotation.transform.link);
1357 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001358 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001359 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001360
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001361 /* We need to adjust the position of the surface
1362 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001363 cposx = surface->geometry.x + cx;
1364 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001365 dposx = rotate->center.x - cposx;
1366 dposy = rotate->center.y - cposy;
1367 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001368 weston_surface_set_position(surface,
1369 surface->geometry.x + dposx,
1370 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001371 }
1372
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001373 /* Repaint implies weston_surface_update_transform(), which
1374 * lazily applies the damage due to rotation update.
1375 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001376 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001377}
1378
1379static void
Scott Moreau447013d2012-02-18 05:05:29 -07001380rotate_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001381 uint32_t time, uint32_t button, int32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001382{
1383 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001384 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001385 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001386 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001387
1388 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001389 if (shsurf)
1390 weston_matrix_multiply(&shsurf->rotation.rotation,
1391 &rotate->rotation);
1392 shell_grab_finish(&rotate->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001393 wl_input_device_end_pointer_grab(device);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001394 free(rotate);
1395 }
1396}
1397
Scott Moreau447013d2012-02-18 05:05:29 -07001398static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001399 noop_grab_focus,
1400 rotate_grab_motion,
1401 rotate_grab_button,
1402};
1403
1404static void
1405rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001406 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001407{
1408 struct weston_surface *base_surface =
1409 (struct weston_surface *) device->pointer_focus;
1410 struct shell_surface *surface;
1411 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001412 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001413 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001414
1415 if (base_surface == NULL)
1416 return;
1417
1418 surface = get_shell_surface(base_surface);
1419 if (!surface)
1420 return;
1421
1422 switch (surface->type) {
1423 case SHELL_SURFACE_PANEL:
1424 case SHELL_SURFACE_BACKGROUND:
1425 case SHELL_SURFACE_FULLSCREEN:
1426 case SHELL_SURFACE_SCREENSAVER:
1427 return;
1428 default:
1429 break;
1430 }
1431
Pekka Paalanen460099f2012-01-20 16:48:25 +02001432 rotate = malloc(sizeof *rotate);
1433 if (!rotate)
1434 return;
1435
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001436 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001437
1438 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001439 surface->surface->geometry.width / 2,
1440 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001441 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001442
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001443 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001444
1445 dx = device->x - rotate->center.x;
1446 dy = device->y - rotate->center.y;
1447 r = sqrtf(dx * dx + dy * dy);
1448 if (r > 20.0f) {
1449 struct weston_matrix inverse;
1450
1451 weston_matrix_init(&inverse);
1452 inverse.d[0] = dx / r;
1453 inverse.d[4] = dy / r;
1454 inverse.d[1] = -inverse.d[4];
1455 inverse.d[5] = inverse.d[0];
1456 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001457
1458 weston_matrix_init(&rotate->rotation);
1459 rotate->rotation.d[0] = dx / r;
1460 rotate->rotation.d[4] = -dy / r;
1461 rotate->rotation.d[1] = -rotate->rotation.d[4];
1462 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001463 } else {
1464 weston_matrix_init(&surface->rotation.rotation);
1465 weston_matrix_init(&rotate->rotation);
1466 }
1467
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001468 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001469}
1470
1471static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001472activate(struct weston_shell *base, struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001473 struct weston_input_device *device)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001474{
1475 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001476 struct weston_compositor *compositor = shell->compositor;
Alex Wu21858432012-04-01 20:13:08 +08001477 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001478
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001479 weston_surface_activate(es, device);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001480
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001481 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001482 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001483
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001484 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001485 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001486 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001487 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001488 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001489
Pekka Paalanen77346a62011-11-30 16:26:35 +02001490 case SHELL_SURFACE_SCREENSAVER:
1491 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001492 if (shell->lock_surface)
1493 weston_surface_restack(es,
1494 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001495 break;
Alex Wu4539b082012-03-01 12:57:46 +08001496 case SHELL_SURFACE_FULLSCREEN:
1497 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001498 shell_stack_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001499 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001500 default:
Alex Wu21858432012-04-01 20:13:08 +08001501 /* move the fullscreen surfaces down into the toplevel layer */
1502 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1503 wl_list_for_each_reverse_safe(surf,
1504 prev,
1505 &shell->fullscreen_layer.surface_list,
1506 layer_link)
1507 weston_surface_restack(surf,
1508 &shell->toplevel_layer.surface_list);
1509 }
1510
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001511 weston_surface_restack(es,
1512 &shell->toplevel_layer.surface_list);
1513 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001514 }
1515}
1516
Alex Wu21858432012-04-01 20:13:08 +08001517/* no-op func for checking black surface */
1518static void
1519black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1520{
1521}
1522
1523static bool
1524is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1525{
1526 if (es->configure == black_surface_configure) {
1527 if (fs_surface)
1528 *fs_surface = (struct weston_surface *)es->private;
1529 return true;
1530 }
1531 return false;
1532}
1533
Kristian Høgsberg75840622011-09-06 13:48:16 -04001534static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001535click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001536 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001537 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001538{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001539 struct weston_input_device *wd = (struct weston_input_device *) device;
1540 struct weston_compositor *compositor = data;
1541 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001542 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001543
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001544 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001545 if (!focus)
1546 return;
1547
Alex Wu21858432012-04-01 20:13:08 +08001548 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001549 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001550
Alex Wu9c35e6b2012-03-05 14:13:13 +08001551 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001552 activate(compositor->shell, focus, wd);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001553}
1554
1555static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001556lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001557{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001558 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001559 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001560 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001561 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001562
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001563 if (shell->locked) {
1564 wl_list_for_each(output, &shell->compositor->output_list, link)
1565 /* TODO: find a way to jump to other DPMS levels */
1566 if (output->set_dpms)
1567 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001568 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001569 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001570
1571 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001572
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001573 /* Hide all surfaces by removing the fullscreen, panel and
1574 * toplevel layers. This way nothing else can show or receive
1575 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001576
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001577 wl_list_remove(&shell->panel_layer.link);
1578 wl_list_remove(&shell->toplevel_layer.link);
1579 wl_list_remove(&shell->fullscreen_layer.link);
1580 wl_list_insert(&shell->compositor->cursor_layer.link,
1581 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001582
Pekka Paalanen77346a62011-11-30 16:26:35 +02001583 launch_screensaver(shell);
1584
1585 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1586 show_screensaver(shell, shsurf);
1587
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001588 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001589 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001590 weston_compositor_wake(shell->compositor);
1591 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001592 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001593
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001594 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001595 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001596
1597 /* reset keyboard foci */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001598 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1599 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001600 NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001601 }
1602
1603 /* TODO: disable bindings that should not work while locked. */
1604
1605 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001606}
1607
1608static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001609unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001610{
1611 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1612
Pekka Paalanend81c2162011-11-16 13:47:34 +02001613 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001614 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001615 return;
1616 }
1617
1618 /* If desktop-shell client has gone away, unlock immediately. */
1619 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001620 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001621 return;
1622 }
1623
1624 if (shell->prepare_event_sent)
1625 return;
1626
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001627 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001628 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001629}
1630
1631static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001632center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001633{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001634 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001635 GLfloat x = (mode->width - surface->geometry.width) / 2;
1636 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001637
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001638 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001639}
1640
1641static void
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001642map(struct weston_shell *base, struct weston_surface *surface,
1643 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001644{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001645 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001646 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001647 struct shell_surface *shsurf;
1648 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001649 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001650 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001651
Pekka Paalanen77346a62011-11-30 16:26:35 +02001652 shsurf = get_shell_surface(surface);
1653 if (shsurf)
1654 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001655
Pekka Paalanen60921e52012-01-25 15:55:43 +02001656 surface->geometry.width = width;
1657 surface->geometry.height = height;
1658 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001659
1660 /* initial positioning, see also configure() */
1661 switch (surface_type) {
1662 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001663 weston_surface_set_position(surface, 10 + random() % 400,
1664 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001665 break;
1666 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001667 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001668 break;
Alex Wu4539b082012-03-01 12:57:46 +08001669 case SHELL_SURFACE_FULLSCREEN:
1670 shell_map_fullscreen(shsurf);
1671 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001672 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001673 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001674 panel_height = get_output_panel_height(shell,surface->output);
1675 weston_surface_set_position(surface, surface->output->x,
1676 surface->output->y + panel_height);
1677 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001678 case SHELL_SURFACE_LOCK:
1679 center_on_output(surface, get_default_output(compositor));
1680 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001681 case SHELL_SURFACE_POPUP:
1682 shell_map_popup(shsurf, shsurf->popup.time);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001683 case SHELL_SURFACE_NONE:
1684 weston_surface_set_position(surface,
1685 surface->geometry.x + sx,
1686 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001687 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001688 default:
1689 ;
1690 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001691
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001692 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001693 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001694 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001695 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001696 wl_list_insert(&shell->background_layer.surface_list,
1697 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001698 break;
1699 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001700 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001701 wl_list_insert(&shell->panel_layer.surface_list,
1702 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001703 break;
1704 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001705 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001706 wl_list_insert(&shell->lock_layer.surface_list,
1707 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001708 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001709 break;
1710 case SHELL_SURFACE_SCREENSAVER:
1711 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001712 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001713 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001714 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001715 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001716 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001717 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001718 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001719 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001720 case SHELL_SURFACE_POPUP:
1721 case SHELL_SURFACE_TRANSIENT:
1722 parent = shsurf->parent->surface;
1723 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1724 break;
Alex Wu4539b082012-03-01 12:57:46 +08001725 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001726 case SHELL_SURFACE_NONE:
1727 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001728 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001729 wl_list_insert(&shell->toplevel_layer.surface_list,
1730 &surface->layer_link);
1731 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001732 }
1733
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001734 if (surface_type != SHELL_SURFACE_NONE) {
1735 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001736 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1737 surface->output = shsurf->output;
1738 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001739
Juan Zhao7bb92f02011-12-15 11:31:51 -05001740 switch (surface_type) {
1741 case SHELL_SURFACE_TOPLEVEL:
1742 case SHELL_SURFACE_TRANSIENT:
1743 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001744 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001745 if (!shell->locked)
1746 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001747 (struct weston_input_device *)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001748 compositor->input_device);
Juan Zhao7bb92f02011-12-15 11:31:51 -05001749 break;
1750 default:
1751 break;
1752 }
1753
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001754 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001755 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001756}
1757
1758static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001759configure(struct weston_shell *base, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001760 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001761{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001762 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001763 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Alex Wu4539b082012-03-01 12:57:46 +08001764 enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001765 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001766
Pekka Paalanen77346a62011-11-30 16:26:35 +02001767 shsurf = get_shell_surface(surface);
1768 if (shsurf)
1769 surface_type = shsurf->type;
1770
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001771 surface->geometry.x = x;
1772 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001773 surface->geometry.width = width;
1774 surface->geometry.height = height;
1775 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001776
1777 switch (surface_type) {
1778 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001779 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001780 break;
Alex Wu4539b082012-03-01 12:57:46 +08001781 case SHELL_SURFACE_FULLSCREEN:
1782 shell_configure_fullscreen(shsurf);
1783 if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
1784 shell_stack_fullscreen(shsurf);
1785 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001786 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001787 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001788 surface->geometry.x = surface->output->x;
1789 surface->geometry.y = surface->output->y +
1790 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001791 break;
Alex Wu4539b082012-03-01 12:57:46 +08001792 case SHELL_SURFACE_TOPLEVEL:
1793 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001794 default:
1795 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001796 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001797
Alex Wu4539b082012-03-01 12:57:46 +08001798 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001799 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001800 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001801
1802 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1803 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001804 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1805 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001806 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001807}
1808
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001809static void
1810shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1811{
1812 struct weston_shell *shell = es->compositor->shell;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001813 struct shell_surface *shsurf = get_shell_surface(es);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001814
1815 if (!weston_surface_is_mapped(es)) {
1816 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001817 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001818 es->geometry.width != es->buffer->width ||
1819 es->geometry.height != es->buffer->height) {
1820 GLfloat from_x, from_y;
1821 GLfloat to_x, to_y;
1822
1823 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
1824 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
1825 configure(shell, es,
1826 es->geometry.x + to_x - from_x,
1827 es->geometry.y + to_y - from_y,
1828 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001829 shsurf->force_configure = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001830 }
1831}
1832
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001833static int launch_desktop_shell_process(struct wl_shell *shell);
1834
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001835static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001836desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001837{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001838 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001839 struct wl_shell *shell =
1840 container_of(process, struct wl_shell, child.process);
1841
1842 shell->child.process.pid = 0;
1843 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001844
1845 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1846 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001847 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001848 shell->child.deathstamp = time;
1849 shell->child.deathcount = 0;
1850 }
1851
1852 shell->child.deathcount++;
1853 if (shell->child.deathcount > 5) {
1854 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1855 return;
1856 }
1857
1858 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1859 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001860}
1861
1862static int
1863launch_desktop_shell_process(struct wl_shell *shell)
1864{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001865 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001866
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001867 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001868 &shell->child.process,
1869 shell_exe,
1870 desktop_shell_sigchld);
1871
1872 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001873 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001874 return 0;
1875}
1876
1877static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001878bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1879{
1880 struct wl_shell *shell = data;
1881
1882 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001883 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001884}
1885
Kristian Høgsberg75840622011-09-06 13:48:16 -04001886static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001887unbind_desktop_shell(struct wl_resource *resource)
1888{
1889 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001890
1891 if (shell->locked)
1892 resume_desktop(shell);
1893
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001894 shell->child.desktop_shell = NULL;
1895 shell->prepare_event_sent = false;
1896 free(resource);
1897}
1898
1899static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001900bind_desktop_shell(struct wl_client *client,
1901 void *data, uint32_t version, uint32_t id)
1902{
1903 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001904 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001905
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001906 resource = wl_client_add_object(client, &desktop_shell_interface,
1907 &desktop_shell_implementation,
1908 id, shell);
1909
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001910 if (client == shell->child.client) {
1911 resource->destroy = unbind_desktop_shell;
1912 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001913 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001914 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001915
1916 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1917 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001918 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001919}
1920
Pekka Paalanen6e168112011-11-24 11:34:05 +02001921static void
1922screensaver_set_surface(struct wl_client *client,
1923 struct wl_resource *resource,
1924 struct wl_resource *shell_surface_resource,
1925 struct wl_resource *output_resource)
1926{
1927 struct wl_shell *shell = resource->data;
1928 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001929 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001930
Pekka Paalanen98262232011-12-01 10:42:22 +02001931 if (reset_shell_surface_type(surface))
1932 return;
1933
Pekka Paalanen77346a62011-11-30 16:26:35 +02001934 surface->type = SHELL_SURFACE_SCREENSAVER;
1935
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001936 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001937 surface->output = output;
1938 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001939}
1940
1941static const struct screensaver_interface screensaver_implementation = {
1942 screensaver_set_surface
1943};
1944
1945static void
1946unbind_screensaver(struct wl_resource *resource)
1947{
1948 struct wl_shell *shell = resource->data;
1949
Pekka Paalanen77346a62011-11-30 16:26:35 +02001950 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001951 free(resource);
1952}
1953
1954static void
1955bind_screensaver(struct wl_client *client,
1956 void *data, uint32_t version, uint32_t id)
1957{
1958 struct wl_shell *shell = data;
1959 struct wl_resource *resource;
1960
1961 resource = wl_client_add_object(client, &screensaver_interface,
1962 &screensaver_implementation,
1963 id, shell);
1964
Pekka Paalanen77346a62011-11-30 16:26:35 +02001965 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001966 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001967 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001968 return;
1969 }
1970
1971 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1972 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001973 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001974}
1975
Kristian Høgsberg07045392012-02-19 18:52:44 -05001976struct switcher {
1977 struct weston_compositor *compositor;
1978 struct weston_surface *current;
1979 struct wl_listener listener;
1980 struct wl_keyboard_grab grab;
1981};
1982
1983static void
1984switcher_next(struct switcher *switcher)
1985{
1986 struct weston_compositor *compositor = switcher->compositor;
1987 struct weston_surface *surface;
1988 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04001989 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05001990
1991 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05001992 switch (get_shell_surface_type(surface)) {
1993 case SHELL_SURFACE_TOPLEVEL:
1994 case SHELL_SURFACE_FULLSCREEN:
1995 case SHELL_SURFACE_MAXIMIZED:
1996 if (first == NULL)
1997 first = surface;
1998 if (prev == switcher->current)
1999 next = surface;
2000 prev = surface;
2001 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002002 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002003 weston_surface_damage(surface);
2004 break;
2005 default:
2006 break;
2007 }
Alex Wu1659daa2012-04-01 20:13:09 +08002008
2009 if (is_black_surface(surface, NULL)) {
2010 surface->alpha = 64;
2011 surface->geometry.dirty = 1;
2012 weston_surface_damage(surface);
2013 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002014 }
2015
2016 if (next == NULL)
2017 next = first;
2018
Alex Wu07b26062012-03-12 16:06:01 +08002019 if (next == NULL)
2020 return;
2021
Kristian Høgsberg07045392012-02-19 18:52:44 -05002022 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002023 wl_signal_add(&next->surface.resource.destroy_signal,
2024 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002025
2026 switcher->current = next;
2027 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002028
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002029 shsurf = get_shell_surface(switcher->current);
2030 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2031 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002032}
2033
2034static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002035switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002036{
2037 struct switcher *switcher =
2038 container_of(listener, struct switcher, listener);
2039
2040 switcher_next(switcher);
2041}
2042
2043static void
2044switcher_destroy(struct switcher *switcher, uint32_t time)
2045{
2046 struct weston_compositor *compositor = switcher->compositor;
2047 struct weston_surface *surface;
2048 struct weston_input_device *device =
2049 (struct weston_input_device *) switcher->grab.input_device;
2050
2051 wl_list_for_each(surface, &compositor->surface_list, link) {
2052 surface->alpha = 255;
2053 weston_surface_damage(surface);
2054 }
2055
Alex Wu07b26062012-03-12 16:06:01 +08002056 if (switcher->current)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002057 activate(compositor->shell, switcher->current, device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002058 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002059 wl_input_device_end_keyboard_grab(&device->input_device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002060 free(switcher);
2061}
2062
2063static void
2064switcher_key(struct wl_keyboard_grab *grab,
2065 uint32_t time, uint32_t key, int32_t state)
2066{
2067 struct switcher *switcher = container_of(grab, struct switcher, grab);
2068 struct weston_input_device *device =
2069 (struct weston_input_device *) grab->input_device;
2070
2071 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
2072 switcher_destroy(switcher, time);
2073 } else if (key == KEY_TAB && state) {
2074 switcher_next(switcher);
2075 }
2076};
2077
2078static const struct wl_keyboard_grab_interface switcher_grab = {
2079 switcher_key
2080};
2081
2082static void
2083switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002084 uint32_t key, uint32_t button, uint32_t axis,
2085 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002086{
2087 struct weston_compositor *compositor = data;
2088 struct switcher *switcher;
2089
2090 switcher = malloc(sizeof *switcher);
2091 switcher->compositor = compositor;
2092 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002093 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002094 wl_list_init(&switcher->listener.link);
2095
2096 switcher->grab.interface = &switcher_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002097 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2098 wl_input_device_set_keyboard_focus(device, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002099 switcher_next(switcher);
2100}
2101
Pekka Paalanen3c647232011-12-22 13:43:43 +02002102static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002103backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002104 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002105{
2106 struct weston_compositor *compositor = data;
2107 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002108 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002109
2110 /* TODO: we're limiting to simple use cases, where we assume just
2111 * control on the primary display. We'd have to extend later if we
2112 * ever get support for setting backlights on random desktop LCD
2113 * panels though */
2114 output = get_default_output(compositor);
2115 if (!output)
2116 return;
2117
2118 if (!output->set_backlight)
2119 return;
2120
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002121 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2122 backlight_new = output->backlight_current - 25;
2123 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2124 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002125
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002126 if (backlight_new < 5)
2127 backlight_new = 5;
2128 if (backlight_new > 255)
2129 backlight_new = 255;
2130
2131 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002132 output->set_backlight(output, output->backlight_current);
2133}
2134
2135static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002136debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002137 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002138{
2139 struct weston_compositor *compositor = data;
2140 struct wl_shell *shell =
2141 container_of(compositor->shell, struct wl_shell, shell);
2142 struct weston_surface *surface;
2143
2144 if (shell->debug_repaint_surface) {
2145 weston_surface_destroy(shell->debug_repaint_surface);
2146 shell->debug_repaint_surface = NULL;
2147 } else {
2148 surface = weston_surface_create(compositor);
2149 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2150 weston_surface_configure(surface, 0, 0, 8192, 8192);
2151 wl_list_insert(&compositor->fade_layer.surface_list,
2152 &surface->layer_link);
2153 weston_surface_assign_output(surface);
2154 pixman_region32_init(&surface->input);
2155
2156 /* Here's the dirty little trick that makes the
2157 * repaint debugging work: we force an
2158 * update_transform first to update dependent state
2159 * and clear the geometry.dirty bit. Then we clear
2160 * the surface damage so it only gets repainted
2161 * piecewise as we repaint other things. */
2162
2163 weston_surface_update_transform(surface);
2164 pixman_region32_fini(&surface->damage);
2165 pixman_region32_init(&surface->damage);
2166 shell->debug_repaint_surface = surface;
2167 }
2168}
2169
2170static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05002171shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002172{
2173 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
2174
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002175 if (shell->child.client)
2176 wl_client_destroy(shell->child.client);
2177
Pekka Paalanen3c647232011-12-22 13:43:43 +02002178 free(shell->screensaver.path);
2179 free(shell);
2180}
2181
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002182int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002183shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002184
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002185WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002186shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002187{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002188 struct wl_shell *shell;
2189
2190 shell = malloc(sizeof *shell);
2191 if (shell == NULL)
2192 return -1;
2193
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002194 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002195 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002196 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002197 shell->shell.unlock = unlock;
Pekka Paalanen3c647232011-12-22 13:43:43 +02002198 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002199
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002200 wl_list_init(&shell->backgrounds);
2201 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002202 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002203
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002204 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2205 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2206 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2207 weston_layer_init(&shell->background_layer,
2208 &shell->toplevel_layer.link);
2209 wl_list_init(&shell->lock_layer.surface_list);
2210
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002211 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002212
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002213 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2214 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002215 return -1;
2216
Kristian Høgsberg75840622011-09-06 13:48:16 -04002217 if (wl_display_add_global(ec->wl_display,
2218 &desktop_shell_interface,
2219 shell, bind_desktop_shell) == NULL)
2220 return -1;
2221
Pekka Paalanen6e168112011-11-24 11:34:05 +02002222 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2223 shell, bind_screensaver) == NULL)
2224 return -1;
2225
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002226 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002227 if (launch_desktop_shell_process(shell) != 0)
2228 return -1;
2229
Scott Moreau6a3633d2012-03-20 08:47:59 -06002230 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, MODIFIER_SUPER,
2231 move_binding, shell);
2232 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, MODIFIER_SUPER,
2233 resize_binding, shell);
2234 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2235 MODIFIER_CTRL | MODIFIER_ALT,
2236 terminate_binding, ec);
2237 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2238 click_to_activate_binding, ec);
Scott Moreau1b45a792012-03-22 10:58:23 -06002239 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002240 MODIFIER_SUPER | MODIFIER_ALT,
2241 surface_opacity_binding, NULL);
2242 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreau1b45a792012-03-22 10:58:23 -06002243 MODIFIER_SUPER, zoom_binding, NULL);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002244 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002245 MODIFIER_SUPER | MODIFIER_ALT,
2246 rotate_binding, NULL);
2247 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, MODIFIER_SUPER,
2248 switcher_binding, ec);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002249
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002250 /* brightness */
Scott Moreau6a3633d2012-03-20 08:47:59 -06002251 weston_compositor_add_binding(ec, KEY_F9, 0, 0, MODIFIER_CTRL,
2252 backlight_binding, ec);
2253 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2254 backlight_binding, ec);
2255 weston_compositor_add_binding(ec, KEY_F10, 0, 0, MODIFIER_CTRL,
2256 backlight_binding, ec);
2257 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2258 backlight_binding, ec);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002259
Scott Moreau6a3633d2012-03-20 08:47:59 -06002260 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, MODIFIER_SUPER,
2261 debug_repaint_binding, ec);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002262
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002263 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002264
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002265 return 0;
2266}