blob: a4cf0b46d2513c89dcb4197b569a78a0c9ebab02 [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
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500133struct weston_move_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700134 struct wl_pointer_grab grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500135 struct weston_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500136 int32_t dx, dy;
137};
138
Pekka Paalanen460099f2012-01-20 16:48:25 +0200139struct rotate_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700140 struct wl_pointer_grab grab;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200141 struct shell_surface *surface;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500142 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200143 struct {
144 int32_t x;
145 int32_t y;
146 } center;
147};
148
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500149static void
Alex Wu4539b082012-03-01 12:57:46 +0800150center_on_output(struct weston_surface *surface,
151 struct weston_output *output);
152
153static void
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200154shell_configuration(struct wl_shell *shell)
155{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200156 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200157 char *path = NULL;
158 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200159
160 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200161 { "path", CONFIG_KEY_STRING, &path },
162 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200163 };
164
165 struct config_section cs[] = {
166 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
167 };
168
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200169 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500170 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200171 free(config_file);
172
Pekka Paalanen7296e792011-12-07 16:22:00 +0200173 shell->screensaver.path = path;
174 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200175}
176
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200177static void
Scott Moreau447013d2012-02-18 05:05:29 -0700178noop_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500179 struct wl_surface *surface, int32_t x, int32_t y)
180{
181 grab->focus = NULL;
182}
183
184static void
Scott Moreau447013d2012-02-18 05:05:29 -0700185move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500186 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500187{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500188 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500189 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500190 struct weston_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500191
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500192 weston_surface_configure(es,
193 device->x + move->dx,
194 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200195 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500196}
197
198static void
Scott Moreau447013d2012-02-18 05:05:29 -0700199move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400200 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500201{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500202 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500203
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500204 if (device->button_count == 0 && state == 0) {
Scott Moreau447013d2012-02-18 05:05:29 -0700205 wl_input_device_end_pointer_grab(device, time);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500206 free(grab);
207 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500208}
209
Scott Moreau447013d2012-02-18 05:05:29 -0700210static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500211 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500212 move_grab_motion,
213 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500214};
215
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400216static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500217weston_surface_move(struct weston_surface *es,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500218 struct weston_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500219{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500220 struct weston_move_grab *move;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500221
222 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400223 if (!move)
224 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500225
226 move->grab.interface = &move_grab_interface;
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200227 move->dx = es->geometry.x - wd->input_device.grab_x;
228 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500229 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500230
Scott Moreau447013d2012-02-18 05:05:29 -0700231 wl_input_device_start_pointer_grab(&wd->input_device, &move->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500232
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200233 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400234
235 return 0;
236}
237
238static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200239shell_surface_move(struct wl_client *client, struct wl_resource *resource,
240 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400241{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500242 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200243 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400244
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500245 if (wd->input_device.button_count == 0 ||
246 wd->input_device.grab_time != time ||
247 wd->input_device.pointer_focus != &shsurf->surface->surface)
248 return;
249
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500250 if (weston_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400251 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500252}
253
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500254struct weston_resize_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700255 struct wl_pointer_grab grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500256 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200257 int32_t width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200258 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500259};
260
261static void
Scott Moreau447013d2012-02-18 05:05:29 -0700262resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500263 uint32_t time, int32_t x, int32_t y)
264{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500265 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500266 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500267 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200268 int32_t from_x, from_y;
269 int32_t to_x, to_y;
270
271 weston_surface_from_global(resize->shsurf->surface,
272 device->grab_x, device->grab_y,
273 &from_x, &from_y);
274 weston_surface_from_global(resize->shsurf->surface,
275 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500276
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200277 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200278 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200279 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200280 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500281 } else {
282 width = resize->width;
283 }
284
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200285 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200286 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200287 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200288 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500289 } else {
290 height = resize->height;
291 }
292
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500293 wl_shell_surface_send_configure(&resize->shsurf->resource,
294 time, resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500295}
296
297static void
Scott Moreau447013d2012-02-18 05:05:29 -0700298resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400299 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500300{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500301 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500302
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500303 if (device->button_count == 0 && state == 0) {
Scott Moreau447013d2012-02-18 05:05:29 -0700304 wl_input_device_end_pointer_grab(device, time);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500305 free(grab);
306 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500307}
308
Scott Moreau447013d2012-02-18 05:05:29 -0700309static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500310 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500311 resize_grab_motion,
312 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500313};
314
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400315static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500316weston_surface_resize(struct shell_surface *shsurf,
317 struct weston_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200318 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500319{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500320 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500321
Alex Wu4539b082012-03-01 12:57:46 +0800322 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
323 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500324
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200325 if (edges == 0 || edges > 15 ||
326 (edges & 3) == 3 || (edges & 12) == 12)
327 return 0;
328
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500329 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400330 if (!resize)
331 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500332
333 resize->grab.interface = &resize_grab_interface;
334 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200335 resize->width = shsurf->surface->geometry.width;
336 resize->height = shsurf->surface->geometry.height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200337 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400338
Scott Moreau447013d2012-02-18 05:05:29 -0700339 wl_input_device_start_pointer_grab(&wd->input_device, &resize->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500340
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200341 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400342
343 return 0;
344}
345
346static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200347shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
348 struct wl_resource *input_resource, uint32_t time,
349 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400350{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500351 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200352 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400353
Alex Wu4539b082012-03-01 12:57:46 +0800354 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
355 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400356
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500357 if (wd->input_device.button_count == 0 ||
358 wd->input_device.grab_time != time ||
359 wd->input_device.pointer_focus != &shsurf->surface->surface)
360 return;
361
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500362 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400363 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500364}
365
Juan Zhao96879df2012-02-07 08:45:41 +0800366static struct weston_output *
367get_default_output(struct weston_compositor *compositor)
368{
369 return container_of(compositor->output_list.next,
370 struct weston_output, link);
371}
372
Alex Wu4539b082012-03-01 12:57:46 +0800373static void
374shell_unset_fullscreen(struct shell_surface *shsurf)
375{
376 /* undo all fullscreen things here */
377 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
378 shsurf->fullscreen.framerate = 0;
379 wl_list_remove(&shsurf->fullscreen.transform.link);
380 wl_list_init(&shsurf->fullscreen.transform.link);
381 weston_surface_destroy(shsurf->fullscreen.black_surface);
382 shsurf->fullscreen.black_surface = NULL;
383 shsurf->fullscreen_output = NULL;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300384 shsurf->force_configure = 1;
Alex Wu4539b082012-03-01 12:57:46 +0800385 weston_surface_set_position(shsurf->surface,
386 shsurf->saved_x, shsurf->saved_y);
387}
388
Pekka Paalanen98262232011-12-01 10:42:22 +0200389static int
390reset_shell_surface_type(struct shell_surface *surface)
391{
392 switch (surface->type) {
393 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800394 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200395 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800396 case SHELL_SURFACE_MAXIMIZED:
397 surface->output = get_default_output(surface->surface->compositor);
398 weston_surface_set_position(surface->surface,
399 surface->saved_x,
400 surface->saved_y);
401 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200402 case SHELL_SURFACE_PANEL:
403 case SHELL_SURFACE_BACKGROUND:
404 wl_list_remove(&surface->link);
405 wl_list_init(&surface->link);
406 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200407 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200408 case SHELL_SURFACE_LOCK:
409 wl_resource_post_error(&surface->resource,
410 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200411 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200412 return -1;
413 case SHELL_SURFACE_NONE:
414 case SHELL_SURFACE_TOPLEVEL:
415 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500416 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200417 break;
418 }
419
420 surface->type = SHELL_SURFACE_NONE;
421 return 0;
422}
423
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500424static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200425shell_surface_set_toplevel(struct wl_client *client,
426 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400427
428{
Pekka Paalanen98262232011-12-01 10:42:22 +0200429 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400430
Pekka Paalanen98262232011-12-01 10:42:22 +0200431 if (reset_shell_surface_type(surface))
432 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400433
Pekka Paalanen98262232011-12-01 10:42:22 +0200434 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400435}
436
437static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200438shell_surface_set_transient(struct wl_client *client,
439 struct wl_resource *resource,
440 struct wl_resource *parent_resource,
441 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400442{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200443 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500444 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200445 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500446 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400447
Pekka Paalanen98262232011-12-01 10:42:22 +0200448 if (reset_shell_surface_type(shsurf))
449 return;
450
Alex Wu4539b082012-03-01 12:57:46 +0800451 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800452 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200453 weston_surface_set_position(es, pes->geometry.x + x,
454 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400455
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200456 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400457}
458
Juan Zhao96879df2012-02-07 08:45:41 +0800459static struct wl_shell *
460shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200461{
Juan Zhao96879df2012-02-07 08:45:41 +0800462 struct weston_surface *es = shsurf->surface;
463 struct weston_shell *shell = es->compositor->shell;
464
465 return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
466}
467
468static int
469get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
470{
471 struct shell_surface *priv;
472 int panel_height = 0;
473
474 if (!output)
475 return 0;
476
477 wl_list_for_each(priv, &wlshell->panels, link) {
478 if (priv->output == output) {
479 panel_height = priv->surface->geometry.height;
480 break;
481 }
482 }
483 return panel_height;
484}
485
486static void
487shell_surface_set_maximized(struct wl_client *client,
488 struct wl_resource *resource,
489 struct wl_resource *output_resource )
490{
491 struct shell_surface *shsurf = resource->data;
492 struct weston_surface *es = shsurf->surface;
493 struct wl_shell *wlshell = NULL;
494 uint32_t edges = 0, panel_height = 0;
495
496 /* get the default output, if the client set it as NULL
497 check whether the ouput is available */
498 if (output_resource)
499 shsurf->output = output_resource->data;
500 else
501 shsurf->output = get_default_output(es->compositor);
502
503 if (reset_shell_surface_type(shsurf))
504 return;
505
506 shsurf->saved_x = es->geometry.x;
507 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800508 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800509
510 wlshell = shell_surface_get_shell(shsurf);
511 panel_height = get_output_panel_height(wlshell, es->output);
512 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500513
514 wl_shell_surface_send_configure(&shsurf->resource,
515 weston_compositor_get_time(), edges,
516 es->output->current->width,
517 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800518
519 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200520}
521
Alex Wu21858432012-04-01 20:13:08 +0800522static void
523black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
524
Alex Wu4539b082012-03-01 12:57:46 +0800525static struct weston_surface *
526create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800527 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800528 GLfloat x, GLfloat y, int w, int h)
529{
530 struct weston_surface *surface = NULL;
531
532 surface = weston_surface_create(ec);
533 if (surface == NULL) {
534 fprintf(stderr, "no memory\n");
535 return NULL;
536 }
537
Alex Wu21858432012-04-01 20:13:08 +0800538 surface->configure = black_surface_configure;
539 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800540 weston_surface_configure(surface, x, y, w, h);
541 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
542 return surface;
543}
544
545/* Create black surface and append it to the associated fullscreen surface.
546 * Handle size dismatch and positioning according to the method. */
547static void
548shell_configure_fullscreen(struct shell_surface *shsurf)
549{
550 struct weston_output *output = shsurf->fullscreen_output;
551 struct weston_surface *surface = shsurf->surface;
552 struct weston_matrix *matrix;
553 float scale;
554
555 center_on_output(surface, output);
556
557 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500558 shsurf->fullscreen.black_surface =
559 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800560 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500561 output->x, output->y,
562 output->current->width,
563 output->current->height);
564
565 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
566 wl_list_insert(&surface->layer_link,
567 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800568 shsurf->fullscreen.black_surface->output = output;
569
570 switch (shsurf->fullscreen.type) {
571 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
572 break;
573 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
574 matrix = &shsurf->fullscreen.transform.matrix;
575 weston_matrix_init(matrix);
576 scale = (float)output->current->width/(float)surface->geometry.width;
577 weston_matrix_scale(matrix, scale, scale, 1);
578 wl_list_remove(&shsurf->fullscreen.transform.link);
579 wl_list_insert(surface->geometry.transformation_list.prev,
580 &shsurf->fullscreen.transform.link);
581 weston_surface_set_position(surface, output->x, output->y);
582 break;
583 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
584 break;
585 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
586 break;
587 default:
588 break;
589 }
590}
591
592/* make the fullscreen and black surface at the top */
593static void
594shell_stack_fullscreen(struct shell_surface *shsurf)
595{
596 struct weston_surface *surface = shsurf->surface;
597 struct wl_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800598
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500599 wl_list_remove(&surface->layer_link);
600 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800601
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500602 wl_list_insert(&shell->fullscreen_layer.surface_list,
603 &surface->layer_link);
604 wl_list_insert(&surface->layer_link,
605 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800606
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500607 weston_surface_damage(surface);
608 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800609}
610
611static void
612shell_map_fullscreen(struct shell_surface *shsurf)
613{
614 shell_configure_fullscreen(shsurf);
615 shell_stack_fullscreen(shsurf);
616}
617
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400618static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200619shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500620 struct wl_resource *resource,
621 uint32_t method,
622 uint32_t framerate,
623 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400624{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200625 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500626 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800627
628 if (output_resource)
629 shsurf->output = output_resource->data;
630 else
631 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400632
Pekka Paalanen98262232011-12-01 10:42:22 +0200633 if (reset_shell_surface_type(shsurf))
634 return;
635
Alex Wu4539b082012-03-01 12:57:46 +0800636 shsurf->fullscreen_output = shsurf->output;
637 shsurf->fullscreen.type = method;
638 shsurf->fullscreen.framerate = framerate;
639 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400640
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200641 shsurf->saved_x = es->geometry.x;
642 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800643 shsurf->saved_position_valid = true;
644
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300645 if (weston_surface_is_mapped(es))
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300646 shsurf->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500647
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500648 wl_shell_surface_send_configure(&shsurf->resource,
649 weston_compositor_get_time(), 0,
650 shsurf->output->current->width,
651 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400652}
653
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500654static void
Scott Moreau447013d2012-02-18 05:05:29 -0700655popup_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500656 struct wl_surface *surface, int32_t x, int32_t y)
657{
658 struct wl_input_device *device = grab->input_device;
659 struct shell_surface *priv =
660 container_of(grab, struct shell_surface, popup.grab);
661 struct wl_client *client = priv->surface->surface.resource.client;
662
Pekka Paalanencb108432012-01-19 16:25:40 +0200663 if (surface && surface->resource.client == client) {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200664 wl_input_device_set_pointer_focus(device, surface, time, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500665 grab->focus = surface;
666 } else {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200667 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500668 grab->focus = NULL;
669 }
670}
671
672static void
Scott Moreau447013d2012-02-18 05:05:29 -0700673popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200674 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500675{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500676 struct wl_resource *resource;
677
678 resource = grab->input_device->pointer_focus_resource;
679 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500680 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500681}
682
683static void
Scott Moreau447013d2012-02-18 05:05:29 -0700684popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400685 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500686{
687 struct wl_resource *resource;
688 struct shell_surface *shsurf =
689 container_of(grab, struct shell_surface, popup.grab);
690
691 resource = grab->input_device->pointer_focus_resource;
692 if (resource) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500693 wl_input_device_send_button(resource, time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500694 } else if (state == 0 &&
695 (shsurf->popup.initial_up ||
696 time - shsurf->popup.time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500697 wl_shell_surface_send_popup_done(&shsurf->resource);
Scott Moreau447013d2012-02-18 05:05:29 -0700698 wl_input_device_end_pointer_grab(grab->input_device, time);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500699 shsurf->popup.grab.input_device = NULL;
700 }
701
702 if (state == 0)
703 shsurf->popup.initial_up = 1;
704}
705
Scott Moreau447013d2012-02-18 05:05:29 -0700706static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500707 popup_grab_focus,
708 popup_grab_motion,
709 popup_grab_button,
710};
711
712static void
713shell_map_popup(struct shell_surface *shsurf, uint32_t time)
714{
715 struct wl_input_device *device;
716 struct weston_surface *es = shsurf->surface;
717 struct weston_surface *parent = shsurf->parent->surface;
718
719 es->output = parent->output;
720
721 shsurf->popup.grab.interface = &popup_grab_interface;
722 device = es->compositor->input_device;
723
Pekka Paalanen938269a2012-02-07 14:19:01 +0200724 weston_surface_update_transform(parent);
725 if (parent->transform.enabled) {
726 shsurf->popup.parent_transform.matrix =
727 parent->transform.matrix;
728 } else {
729 /* construct x, y translation matrix */
730 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
731 shsurf->popup.parent_transform.matrix.d[12] =
732 parent->geometry.x;
733 shsurf->popup.parent_transform.matrix.d[13] =
734 parent->geometry.y;
735 }
736 wl_list_insert(es->geometry.transformation_list.prev,
737 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200738 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500739
740 shsurf->popup.grab.input_device = device;
741 shsurf->popup.time = device->grab_time;
742 shsurf->popup.initial_up = 0;
743
Scott Moreau447013d2012-02-18 05:05:29 -0700744 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500745 &shsurf->popup.grab, shsurf->popup.time);
746}
747
748static void
749shell_surface_set_popup(struct wl_client *client,
750 struct wl_resource *resource,
751 struct wl_resource *input_device_resource,
752 uint32_t time,
753 struct wl_resource *parent_resource,
754 int32_t x, int32_t y, uint32_t flags)
755{
756 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500757
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500758 shsurf->type = SHELL_SURFACE_POPUP;
759 shsurf->parent = parent_resource->data;
760 shsurf->popup.x = x;
761 shsurf->popup.y = y;
762}
763
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200764static const struct wl_shell_surface_interface shell_surface_implementation = {
765 shell_surface_move,
766 shell_surface_resize,
767 shell_surface_set_toplevel,
768 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500769 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +0800770 shell_surface_set_popup,
771 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200772};
773
774static void
775destroy_shell_surface(struct wl_resource *resource)
776{
777 struct shell_surface *shsurf = resource->data;
778
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500779 if (shsurf->popup.grab.input_device)
Scott Moreau447013d2012-02-18 05:05:29 -0700780 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500781
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200782 /* in case cleaning up a dead client destroys shell_surface first */
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300783 if (shsurf->surface) {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200784 wl_list_remove(&shsurf->surface_destroy_listener.link);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300785 shsurf->surface->configure = NULL;
786 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200787
Alex Wuaa08e2d2012-03-05 11:01:40 +0800788 if (shsurf->fullscreen.black_surface)
789 weston_surface_destroy(shsurf->fullscreen.black_surface);
790
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200791 wl_list_remove(&shsurf->link);
792 free(shsurf);
793}
794
795static void
796shell_handle_surface_destroy(struct wl_listener *listener,
797 struct wl_resource *resource, uint32_t time)
798{
799 struct shell_surface *shsurf = container_of(listener,
800 struct shell_surface,
801 surface_destroy_listener);
802
803 shsurf->surface = NULL;
804 wl_resource_destroy(&shsurf->resource, time);
805}
806
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200807static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500808get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200809{
810 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
811 struct wl_listener *listener;
812
813 /* search the destroy listener list for our callback */
814 wl_list_for_each(listener, lst, link) {
815 if (listener->func == shell_handle_surface_destroy) {
816 return container_of(listener, struct shell_surface,
817 surface_destroy_listener);
818 }
819 }
820
821 return NULL;
822}
823
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200824static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300825shell_surface_configure(struct weston_surface *, int32_t, int32_t);
826
827static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200828shell_get_shell_surface(struct wl_client *client,
829 struct wl_resource *resource,
830 uint32_t id,
831 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200832{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500833 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200834 struct shell_surface *shsurf;
835
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200836 if (get_shell_surface(surface)) {
837 wl_resource_post_error(surface_resource,
838 WL_DISPLAY_ERROR_INVALID_OBJECT,
839 "wl_shell::get_shell_surface already requested");
840 return;
841 }
842
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300843 if (surface->configure) {
844 wl_resource_post_error(surface_resource,
845 WL_DISPLAY_ERROR_INVALID_OBJECT,
846 "surface->configure already set");
847 return;
848 }
849
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200850 shsurf = calloc(1, sizeof *shsurf);
851 if (!shsurf) {
852 wl_resource_post_no_memory(resource);
853 return;
854 }
855
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300856 surface->configure = shell_surface_configure;
857
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200858 shsurf->resource.destroy = destroy_shell_surface;
859 shsurf->resource.object.id = id;
860 shsurf->resource.object.interface = &wl_shell_surface_interface;
861 shsurf->resource.object.implementation =
862 (void (**)(void)) &shell_surface_implementation;
863 shsurf->resource.data = shsurf;
864
Alex Wu4539b082012-03-01 12:57:46 +0800865 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200866 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +0800867 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
868 shsurf->fullscreen.framerate = 0;
869 shsurf->fullscreen.black_surface = NULL;
870 wl_list_init(&shsurf->fullscreen.transform.link);
871
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200872 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
873 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
874 &shsurf->surface_destroy_listener.link);
875
876 /* init link so its safe to always remove it in destroy_shell_surface */
877 wl_list_init(&shsurf->link);
878
Pekka Paalanen460099f2012-01-20 16:48:25 +0200879 /* empty when not in use */
880 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500881 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +0200882
Pekka Paalanen98262232011-12-01 10:42:22 +0200883 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200884
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200885 wl_client_add_resource(client, &shsurf->resource);
886}
887
888static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200889 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500890};
891
Kristian Høgsberg07937562011-04-12 17:25:42 -0400892static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500893handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200894{
895 proc->pid = 0;
896}
897
898static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200899launch_screensaver(struct wl_shell *shell)
900{
901 if (shell->screensaver.binding)
902 return;
903
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200904 if (!shell->screensaver.path)
905 return;
906
Kristian Høgsberg32bed572012-03-01 17:11:36 -0500907 if (shell->screensaver.process.pid != 0) {
908 fprintf(stderr, "old screensaver still running\n");
909 return;
910 }
911
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500912 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200913 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200914 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200915 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200916}
917
918static void
919terminate_screensaver(struct wl_shell *shell)
920{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200921 if (shell->screensaver.process.pid == 0)
922 return;
923
924 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200925}
926
927static void
928show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
929{
930 struct wl_list *list;
931
932 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500933 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200934 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500935 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200936
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500937 wl_list_remove(&surface->surface->layer_link);
938 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200939 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200940 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200941}
942
943static void
944hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
945{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500946 wl_list_remove(&surface->surface->layer_link);
947 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200948 surface->surface->output = NULL;
949}
950
951static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400952desktop_shell_set_background(struct wl_client *client,
953 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100954 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400955 struct wl_resource *surface_resource)
956{
957 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200958 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500959 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200960 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400961
Pekka Paalanen98262232011-12-01 10:42:22 +0200962 if (reset_shell_surface_type(shsurf))
963 return;
964
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100965 wl_list_for_each(priv, &shell->backgrounds, link) {
966 if (priv->output == output_resource->data) {
967 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500968 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100969 wl_list_remove(&priv->link);
970 break;
971 }
972 }
973
Pekka Paalanen068ae942011-11-28 14:11:15 +0200974 shsurf->type = SHELL_SURFACE_BACKGROUND;
975 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100976
Pekka Paalanen068ae942011-11-28 14:11:15 +0200977 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100978
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200979 weston_surface_set_position(surface, shsurf->output->x,
980 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200981
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500982 desktop_shell_send_configure(resource,
983 weston_compositor_get_time(), 0,
984 surface_resource,
985 shsurf->output->current->width,
986 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400987}
988
989static void
990desktop_shell_set_panel(struct wl_client *client,
991 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100992 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400993 struct wl_resource *surface_resource)
994{
995 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200996 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500997 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200998 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400999
Pekka Paalanen98262232011-12-01 10:42:22 +02001000 if (reset_shell_surface_type(shsurf))
1001 return;
1002
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001003 wl_list_for_each(priv, &shell->panels, link) {
1004 if (priv->output == output_resource->data) {
1005 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001006 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001007 wl_list_remove(&priv->link);
1008 break;
1009 }
1010 }
1011
Pekka Paalanen068ae942011-11-28 14:11:15 +02001012 shsurf->type = SHELL_SURFACE_PANEL;
1013 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001014
Pekka Paalanen068ae942011-11-28 14:11:15 +02001015 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001016
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001017 weston_surface_set_position(surface, shsurf->output->x,
1018 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001019
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001020 desktop_shell_send_configure(resource,
1021 weston_compositor_get_time(), 0,
1022 surface_resource,
1023 shsurf->output->current->width,
1024 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001025}
1026
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001027static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001028handle_lock_surface_destroy(struct wl_listener *listener,
1029 struct wl_resource *resource, uint32_t time)
1030{
1031 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +02001032 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001033
1034 fprintf(stderr, "lock surface gone\n");
1035 shell->lock_surface = NULL;
1036}
1037
1038static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001039desktop_shell_set_lock_surface(struct wl_client *client,
1040 struct wl_resource *resource,
1041 struct wl_resource *surface_resource)
1042{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001043 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001044 struct shell_surface *surface = surface_resource->data;
1045
1046 if (reset_shell_surface_type(surface))
1047 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001048
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001049 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001050
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001051 if (!shell->locked)
1052 return;
1053
Pekka Paalanen98262232011-12-01 10:42:22 +02001054 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001055
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001056 shell->lock_surface_listener.func = handle_lock_surface_destroy;
1057 wl_list_insert(&surface_resource->destroy_listener_list,
1058 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001059
Pekka Paalanen068ae942011-11-28 14:11:15 +02001060 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001061}
1062
1063static void
1064resume_desktop(struct wl_shell *shell)
1065{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001066 struct shell_surface *tmp;
1067
1068 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1069 hide_screensaver(shell, tmp);
1070
1071 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001072
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001073 wl_list_remove(&shell->lock_layer.link);
1074 wl_list_insert(&shell->compositor->cursor_layer.link,
1075 &shell->fullscreen_layer.link);
1076 wl_list_insert(&shell->fullscreen_layer.link,
1077 &shell->panel_layer.link);
1078 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001079
1080 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001081 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001082 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001083 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001084}
1085
1086static void
1087desktop_shell_unlock(struct wl_client *client,
1088 struct wl_resource *resource)
1089{
1090 struct wl_shell *shell = resource->data;
1091
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001092 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001093
1094 if (shell->locked)
1095 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001096}
1097
Kristian Høgsberg75840622011-09-06 13:48:16 -04001098static const struct desktop_shell_interface desktop_shell_implementation = {
1099 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001100 desktop_shell_set_panel,
1101 desktop_shell_set_lock_surface,
1102 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001103};
1104
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001105static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001106get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001107{
1108 struct shell_surface *shsurf;
1109
1110 shsurf = get_shell_surface(surface);
1111 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001112 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001113 return shsurf->type;
1114}
1115
Kristian Høgsberg75840622011-09-06 13:48:16 -04001116static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001117move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001118 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001119{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001120 struct weston_surface *surface =
1121 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001122
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001123 if (surface == NULL)
1124 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001125
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001126 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001127 case SHELL_SURFACE_PANEL:
1128 case SHELL_SURFACE_BACKGROUND:
1129 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001130 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001131 return;
1132 default:
1133 break;
1134 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001135
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001136 weston_surface_move(surface, (struct weston_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001137}
1138
1139static void
1140resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001141 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001142{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001143 struct weston_surface *surface =
1144 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001145 uint32_t edges = 0;
1146 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001147 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001148
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001149 if (surface == NULL)
1150 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001151
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001152 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001153 if (!shsurf)
1154 return;
1155
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001156 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001157 case SHELL_SURFACE_PANEL:
1158 case SHELL_SURFACE_BACKGROUND:
1159 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001160 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001161 return;
1162 default:
1163 break;
1164 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001165
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001166 weston_surface_from_global(surface,
1167 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001168
Pekka Paalanen60921e52012-01-25 15:55:43 +02001169 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001170 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001171 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001172 edges |= 0;
1173 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001174 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001175
Pekka Paalanen60921e52012-01-25 15:55:43 +02001176 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001177 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001178 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001179 edges |= 0;
1180 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001181 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001182
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001183 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001184 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001185}
1186
1187static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001188surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1189 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1190{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001191 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001192 struct shell_surface *shsurf;
1193 struct weston_surface *surface =
1194 (struct weston_surface *) device->pointer_focus;
1195
1196 if (surface == NULL)
1197 return;
1198
1199 shsurf = get_shell_surface(surface);
1200 if (!shsurf)
1201 return;
1202
1203 switch (shsurf->type) {
1204 case SHELL_SURFACE_BACKGROUND:
1205 case SHELL_SURFACE_SCREENSAVER:
1206 return;
1207 default:
1208 break;
1209 }
1210
1211 surface->alpha += value * step;
1212
1213 if (surface->alpha > 255)
1214 surface->alpha = 255;
1215 if (surface->alpha < step)
1216 surface->alpha = step;
1217
1218 surface->geometry.dirty = 1;
1219 weston_surface_damage(surface);
1220}
1221
1222static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001223zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001224 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001225{
1226 struct weston_input_device *wd = (struct weston_input_device *) device;
1227 struct weston_compositor *compositor = wd->compositor;
1228 struct weston_output *output;
1229
1230 wl_list_for_each(output, &compositor->output_list, link) {
1231 if (pixman_region32_contains_point(&output->region,
1232 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001233 output->zoom.active = 1;
1234 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001235
1236 if (output->zoom.level >= 1.0) {
1237 output->zoom.active = 0;
1238 output->zoom.level = 1.0;
1239 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001240
1241 if (output->zoom.level < output->zoom.increment)
1242 output->zoom.level = output->zoom.increment;
1243
1244 weston_output_update_zoom(output, device->x, device->y);
1245 }
1246 }
1247}
1248
Scott Moreauccbf29d2012-02-22 14:21:41 -07001249static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001250terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001251 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001252{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001253 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001254
1255 if (state)
1256 wl_display_terminate(compositor->wl_display);
1257}
1258
1259static void
Scott Moreau447013d2012-02-18 05:05:29 -07001260rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001261 uint32_t time, int32_t x, int32_t y)
1262{
1263 struct rotate_grab *rotate =
1264 container_of(grab, struct rotate_grab, grab);
1265 struct wl_input_device *device = grab->input_device;
1266 struct shell_surface *surface = rotate->surface;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001267 struct weston_surface *base_surface = surface->surface;
1268 GLfloat cx = 0.5f * base_surface->geometry.width;
1269 GLfloat cy = 0.5f * base_surface->geometry.height;
1270 GLfloat dx, dy, cposx, cposy, dposx, dposy;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001271 GLfloat r;
1272
1273 dx = device->x - rotate->center.x;
1274 dy = device->y - rotate->center.y;
1275 r = sqrtf(dx * dx + dy * dy);
1276
1277 wl_list_remove(&surface->rotation.transform.link);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001278 surface->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001279
1280 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001281 struct weston_matrix *matrix =
1282 &surface->rotation.transform.matrix;
1283
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001284 weston_matrix_init(&rotate->rotation);
1285 rotate->rotation.d[0] = dx / r;
1286 rotate->rotation.d[4] = -dy / r;
1287 rotate->rotation.d[1] = -rotate->rotation.d[4];
1288 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001289
1290 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001291 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001292 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1293 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001294 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001295
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001296 wl_list_insert(
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001297 &surface->surface->geometry.transformation_list,
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001298 &surface->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001299 } else {
1300 wl_list_init(&surface->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001301 weston_matrix_init(&surface->rotation.rotation);
1302 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001303 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001304
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001305 /* We need to adjust the position of the surface
1306 * in case it was resized in a rotated state before */
1307 cposx = base_surface->geometry.x + cx;
1308 cposy = base_surface->geometry.y + cy;
1309 dposx = rotate->center.x - cposx;
1310 dposy = rotate->center.y - cposy;
1311 if (dposx != 0.0f || dposy != 0.0f) {
1312 weston_surface_set_position(base_surface,
1313 base_surface->geometry.x + dposx,
1314 base_surface->geometry.y + dposy);
1315 }
1316
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001317 /* Repaint implies weston_surface_update_transform(), which
1318 * lazily applies the damage due to rotation update.
1319 */
1320 weston_compositor_schedule_repaint(surface->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001321}
1322
1323static void
Scott Moreau447013d2012-02-18 05:05:29 -07001324rotate_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001325 uint32_t time, uint32_t button, int32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001326{
1327 struct rotate_grab *rotate =
1328 container_of(grab, struct rotate_grab, grab);
1329 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001330 struct shell_surface *surface = rotate->surface;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001331
1332 if (device->button_count == 0 && state == 0) {
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001333 weston_matrix_multiply(&surface->rotation.rotation,
1334 &rotate->rotation);
Scott Moreau447013d2012-02-18 05:05:29 -07001335 wl_input_device_end_pointer_grab(device, time);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001336 free(rotate);
1337 }
1338}
1339
Scott Moreau447013d2012-02-18 05:05:29 -07001340static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001341 noop_grab_focus,
1342 rotate_grab_motion,
1343 rotate_grab_button,
1344};
1345
1346static void
1347rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001348 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001349{
1350 struct weston_surface *base_surface =
1351 (struct weston_surface *) device->pointer_focus;
1352 struct shell_surface *surface;
1353 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001354 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001355 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001356
1357 if (base_surface == NULL)
1358 return;
1359
1360 surface = get_shell_surface(base_surface);
1361 if (!surface)
1362 return;
1363
1364 switch (surface->type) {
1365 case SHELL_SURFACE_PANEL:
1366 case SHELL_SURFACE_BACKGROUND:
1367 case SHELL_SURFACE_FULLSCREEN:
1368 case SHELL_SURFACE_SCREENSAVER:
1369 return;
1370 default:
1371 break;
1372 }
1373
Pekka Paalanen460099f2012-01-20 16:48:25 +02001374 rotate = malloc(sizeof *rotate);
1375 if (!rotate)
1376 return;
1377
1378 rotate->grab.interface = &rotate_grab_interface;
1379 rotate->surface = surface;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001380
1381 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001382 surface->surface->geometry.width / 2,
1383 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001384 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001385
Scott Moreau447013d2012-02-18 05:05:29 -07001386 wl_input_device_start_pointer_grab(device, &rotate->grab, time);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001387
1388 dx = device->x - rotate->center.x;
1389 dy = device->y - rotate->center.y;
1390 r = sqrtf(dx * dx + dy * dy);
1391 if (r > 20.0f) {
1392 struct weston_matrix inverse;
1393
1394 weston_matrix_init(&inverse);
1395 inverse.d[0] = dx / r;
1396 inverse.d[4] = dy / r;
1397 inverse.d[1] = -inverse.d[4];
1398 inverse.d[5] = inverse.d[0];
1399 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001400
1401 weston_matrix_init(&rotate->rotation);
1402 rotate->rotation.d[0] = dx / r;
1403 rotate->rotation.d[4] = -dy / r;
1404 rotate->rotation.d[1] = -rotate->rotation.d[4];
1405 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001406 } else {
1407 weston_matrix_init(&surface->rotation.rotation);
1408 weston_matrix_init(&rotate->rotation);
1409 }
1410
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001411 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001412}
1413
1414static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001415activate(struct weston_shell *base, struct weston_surface *es,
1416 struct weston_input_device *device, uint32_t time)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001417{
1418 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001419 struct weston_compositor *compositor = shell->compositor;
Alex Wu21858432012-04-01 20:13:08 +08001420 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001421
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001422 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001423
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001424 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001425 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001426
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001427 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001428 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001429 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001430 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001431 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001432
Pekka Paalanen77346a62011-11-30 16:26:35 +02001433 case SHELL_SURFACE_SCREENSAVER:
1434 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001435 if (shell->lock_surface)
1436 weston_surface_restack(es,
1437 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001438 break;
Alex Wu4539b082012-03-01 12:57:46 +08001439 case SHELL_SURFACE_FULLSCREEN:
1440 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001441 shell_stack_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001442 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001443 default:
Alex Wu21858432012-04-01 20:13:08 +08001444 /* move the fullscreen surfaces down into the toplevel layer */
1445 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1446 wl_list_for_each_reverse_safe(surf,
1447 prev,
1448 &shell->fullscreen_layer.surface_list,
1449 layer_link)
1450 weston_surface_restack(surf,
1451 &shell->toplevel_layer.surface_list);
1452 }
1453
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001454 weston_surface_restack(es,
1455 &shell->toplevel_layer.surface_list);
1456 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001457 }
1458}
1459
Alex Wu21858432012-04-01 20:13:08 +08001460/* no-op func for checking black surface */
1461static void
1462black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1463{
1464}
1465
1466static bool
1467is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1468{
1469 if (es->configure == black_surface_configure) {
1470 if (fs_surface)
1471 *fs_surface = (struct weston_surface *)es->private;
1472 return true;
1473 }
1474 return false;
1475}
1476
Kristian Høgsberg75840622011-09-06 13:48:16 -04001477static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001478click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001479 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001480 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001481{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001482 struct weston_input_device *wd = (struct weston_input_device *) device;
1483 struct weston_compositor *compositor = data;
1484 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001485 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001486
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001487 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001488 if (!focus)
1489 return;
1490
Alex Wu21858432012-04-01 20:13:08 +08001491 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001492 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001493
Alex Wu9c35e6b2012-03-05 14:13:13 +08001494 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001495 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001496}
1497
1498static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001499lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001500{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001501 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001502 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001503 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001504 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001505 uint32_t time;
1506
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001507 if (shell->locked) {
1508 wl_list_for_each(output, &shell->compositor->output_list, link)
1509 /* TODO: find a way to jump to other DPMS levels */
1510 if (output->set_dpms)
1511 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001512 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001513 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001514
1515 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001516
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001517 /* Hide all surfaces by removing the fullscreen, panel and
1518 * toplevel layers. This way nothing else can show or receive
1519 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001520
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001521 wl_list_remove(&shell->panel_layer.link);
1522 wl_list_remove(&shell->toplevel_layer.link);
1523 wl_list_remove(&shell->fullscreen_layer.link);
1524 wl_list_insert(&shell->compositor->cursor_layer.link,
1525 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001526
Pekka Paalanen77346a62011-11-30 16:26:35 +02001527 launch_screensaver(shell);
1528
1529 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1530 show_screensaver(shell, shsurf);
1531
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001532 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001533 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001534 weston_compositor_wake(shell->compositor);
1535 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001536 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001537
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001538 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001539 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001540
1541 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001542 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001543 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1544 wl_input_device_set_keyboard_focus(&device->input_device,
1545 NULL, time);
1546 }
1547
1548 /* TODO: disable bindings that should not work while locked. */
1549
1550 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001551}
1552
1553static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001554unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001555{
1556 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1557
Pekka Paalanend81c2162011-11-16 13:47:34 +02001558 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001559 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001560 return;
1561 }
1562
1563 /* If desktop-shell client has gone away, unlock immediately. */
1564 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001565 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001566 return;
1567 }
1568
1569 if (shell->prepare_event_sent)
1570 return;
1571
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001572 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001573 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001574}
1575
1576static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001577center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001578{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001579 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001580 GLfloat x = (mode->width - surface->geometry.width) / 2;
1581 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001582
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001583 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001584}
1585
1586static void
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001587map(struct weston_shell *base, struct weston_surface *surface,
1588 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001589{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001590 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001591 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001592 struct shell_surface *shsurf;
1593 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001594 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001595 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001596
Pekka Paalanen77346a62011-11-30 16:26:35 +02001597 shsurf = get_shell_surface(surface);
1598 if (shsurf)
1599 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001600
Pekka Paalanen60921e52012-01-25 15:55:43 +02001601 surface->geometry.width = width;
1602 surface->geometry.height = height;
1603 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001604
1605 /* initial positioning, see also configure() */
1606 switch (surface_type) {
1607 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001608 weston_surface_set_position(surface, 10 + random() % 400,
1609 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001610 break;
1611 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001612 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001613 break;
Alex Wu4539b082012-03-01 12:57:46 +08001614 case SHELL_SURFACE_FULLSCREEN:
1615 shell_map_fullscreen(shsurf);
1616 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001617 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001618 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001619 panel_height = get_output_panel_height(shell,surface->output);
1620 weston_surface_set_position(surface, surface->output->x,
1621 surface->output->y + panel_height);
1622 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001623 case SHELL_SURFACE_LOCK:
1624 center_on_output(surface, get_default_output(compositor));
1625 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001626 case SHELL_SURFACE_POPUP:
1627 shell_map_popup(shsurf, shsurf->popup.time);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001628 case SHELL_SURFACE_NONE:
1629 weston_surface_set_position(surface,
1630 surface->geometry.x + sx,
1631 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001632 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001633 default:
1634 ;
1635 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001636
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001637 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001638 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001639 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001640 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001641 wl_list_insert(&shell->background_layer.surface_list,
1642 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001643 break;
1644 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001645 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001646 wl_list_insert(&shell->panel_layer.surface_list,
1647 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001648 break;
1649 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001650 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001651 wl_list_insert(&shell->lock_layer.surface_list,
1652 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001653 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001654 break;
1655 case SHELL_SURFACE_SCREENSAVER:
1656 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001657 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001658 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001659 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001660 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001661 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001662 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001663 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001664 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001665 case SHELL_SURFACE_POPUP:
1666 case SHELL_SURFACE_TRANSIENT:
1667 parent = shsurf->parent->surface;
1668 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1669 break;
Alex Wu4539b082012-03-01 12:57:46 +08001670 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001671 case SHELL_SURFACE_NONE:
1672 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001673 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001674 wl_list_insert(&shell->toplevel_layer.surface_list,
1675 &surface->layer_link);
1676 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001677 }
1678
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001679 if (surface_type != SHELL_SURFACE_NONE) {
1680 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001681 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1682 surface->output = shsurf->output;
1683 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001684
Juan Zhao7bb92f02011-12-15 11:31:51 -05001685 switch (surface_type) {
1686 case SHELL_SURFACE_TOPLEVEL:
1687 case SHELL_SURFACE_TRANSIENT:
1688 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001689 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001690 if (!shell->locked)
1691 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001692 (struct weston_input_device *)
Alex Wu4539b082012-03-01 12:57:46 +08001693 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001694 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001695 break;
1696 default:
1697 break;
1698 }
1699
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001700 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001701 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001702}
1703
1704static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001705configure(struct weston_shell *base, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001706 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001707{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001708 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001709 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Alex Wu4539b082012-03-01 12:57:46 +08001710 enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001711 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001712
Pekka Paalanen77346a62011-11-30 16:26:35 +02001713 shsurf = get_shell_surface(surface);
1714 if (shsurf)
1715 surface_type = shsurf->type;
1716
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001717 surface->geometry.x = x;
1718 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001719 surface->geometry.width = width;
1720 surface->geometry.height = height;
1721 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001722
1723 switch (surface_type) {
1724 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001725 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001726 break;
Alex Wu4539b082012-03-01 12:57:46 +08001727 case SHELL_SURFACE_FULLSCREEN:
1728 shell_configure_fullscreen(shsurf);
1729 if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
1730 shell_stack_fullscreen(shsurf);
1731 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001732 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001733 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001734 surface->geometry.x = surface->output->x;
1735 surface->geometry.y = surface->output->y +
1736 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001737 break;
Alex Wu4539b082012-03-01 12:57:46 +08001738 case SHELL_SURFACE_TOPLEVEL:
1739 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001740 default:
1741 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001742 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001743
Alex Wu4539b082012-03-01 12:57:46 +08001744 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001745 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001746 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001747
1748 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1749 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001750 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1751 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001752 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001753}
1754
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001755static void
1756shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1757{
1758 struct weston_shell *shell = es->compositor->shell;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001759 struct shell_surface *shsurf = get_shell_surface(es);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001760
1761 if (!weston_surface_is_mapped(es)) {
1762 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001763 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001764 es->geometry.width != es->buffer->width ||
1765 es->geometry.height != es->buffer->height) {
1766 GLfloat from_x, from_y;
1767 GLfloat to_x, to_y;
1768
1769 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
1770 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
1771 configure(shell, es,
1772 es->geometry.x + to_x - from_x,
1773 es->geometry.y + to_y - from_y,
1774 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001775 shsurf->force_configure = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001776 }
1777}
1778
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001779static int launch_desktop_shell_process(struct wl_shell *shell);
1780
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001781static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001782desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001783{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001784 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001785 struct wl_shell *shell =
1786 container_of(process, struct wl_shell, child.process);
1787
1788 shell->child.process.pid = 0;
1789 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001790
1791 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1792 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001793 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001794 shell->child.deathstamp = time;
1795 shell->child.deathcount = 0;
1796 }
1797
1798 shell->child.deathcount++;
1799 if (shell->child.deathcount > 5) {
1800 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1801 return;
1802 }
1803
1804 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1805 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001806}
1807
1808static int
1809launch_desktop_shell_process(struct wl_shell *shell)
1810{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001811 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001812
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001813 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001814 &shell->child.process,
1815 shell_exe,
1816 desktop_shell_sigchld);
1817
1818 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001819 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001820 return 0;
1821}
1822
1823static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001824bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1825{
1826 struct wl_shell *shell = data;
1827
1828 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001829 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001830}
1831
Kristian Høgsberg75840622011-09-06 13:48:16 -04001832static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001833unbind_desktop_shell(struct wl_resource *resource)
1834{
1835 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001836
1837 if (shell->locked)
1838 resume_desktop(shell);
1839
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001840 shell->child.desktop_shell = NULL;
1841 shell->prepare_event_sent = false;
1842 free(resource);
1843}
1844
1845static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001846bind_desktop_shell(struct wl_client *client,
1847 void *data, uint32_t version, uint32_t id)
1848{
1849 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001850 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001851
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001852 resource = wl_client_add_object(client, &desktop_shell_interface,
1853 &desktop_shell_implementation,
1854 id, shell);
1855
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001856 if (client == shell->child.client) {
1857 resource->destroy = unbind_desktop_shell;
1858 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001859 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001860 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001861
1862 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1863 "permission to bind desktop_shell denied");
1864 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001865}
1866
Pekka Paalanen6e168112011-11-24 11:34:05 +02001867static void
1868screensaver_set_surface(struct wl_client *client,
1869 struct wl_resource *resource,
1870 struct wl_resource *shell_surface_resource,
1871 struct wl_resource *output_resource)
1872{
1873 struct wl_shell *shell = resource->data;
1874 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001875 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001876
Pekka Paalanen98262232011-12-01 10:42:22 +02001877 if (reset_shell_surface_type(surface))
1878 return;
1879
Pekka Paalanen77346a62011-11-30 16:26:35 +02001880 surface->type = SHELL_SURFACE_SCREENSAVER;
1881
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001882 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001883 surface->output = output;
1884 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001885}
1886
1887static const struct screensaver_interface screensaver_implementation = {
1888 screensaver_set_surface
1889};
1890
1891static void
1892unbind_screensaver(struct wl_resource *resource)
1893{
1894 struct wl_shell *shell = resource->data;
1895
Pekka Paalanen77346a62011-11-30 16:26:35 +02001896 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001897 free(resource);
1898}
1899
1900static void
1901bind_screensaver(struct wl_client *client,
1902 void *data, uint32_t version, uint32_t id)
1903{
1904 struct wl_shell *shell = data;
1905 struct wl_resource *resource;
1906
1907 resource = wl_client_add_object(client, &screensaver_interface,
1908 &screensaver_implementation,
1909 id, shell);
1910
Pekka Paalanen77346a62011-11-30 16:26:35 +02001911 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001912 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001913 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001914 return;
1915 }
1916
1917 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1918 "interface object already bound");
1919 wl_resource_destroy(resource, 0);
1920}
1921
Kristian Høgsberg07045392012-02-19 18:52:44 -05001922struct switcher {
1923 struct weston_compositor *compositor;
1924 struct weston_surface *current;
1925 struct wl_listener listener;
1926 struct wl_keyboard_grab grab;
1927};
1928
1929static void
1930switcher_next(struct switcher *switcher)
1931{
1932 struct weston_compositor *compositor = switcher->compositor;
1933 struct weston_surface *surface;
1934 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
1935
1936 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05001937 switch (get_shell_surface_type(surface)) {
1938 case SHELL_SURFACE_TOPLEVEL:
1939 case SHELL_SURFACE_FULLSCREEN:
1940 case SHELL_SURFACE_MAXIMIZED:
1941 if (first == NULL)
1942 first = surface;
1943 if (prev == switcher->current)
1944 next = surface;
1945 prev = surface;
1946 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05001947 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05001948 weston_surface_damage(surface);
1949 break;
1950 default:
1951 break;
1952 }
1953 }
1954
1955 if (next == NULL)
1956 next = first;
1957
Alex Wu07b26062012-03-12 16:06:01 +08001958 if (next == NULL)
1959 return;
1960
Kristian Høgsberg07045392012-02-19 18:52:44 -05001961 wl_list_remove(&switcher->listener.link);
1962 wl_list_insert(next->surface.resource.destroy_listener_list.prev,
1963 &switcher->listener.link);
1964
1965 switcher->current = next;
1966 next->alpha = 255;
1967}
1968
1969static void
1970switcher_handle_surface_destroy(struct wl_listener *listener,
1971 struct wl_resource *resource, uint32_t time)
1972{
1973 struct switcher *switcher =
1974 container_of(listener, struct switcher, listener);
1975
1976 switcher_next(switcher);
1977}
1978
1979static void
1980switcher_destroy(struct switcher *switcher, uint32_t time)
1981{
1982 struct weston_compositor *compositor = switcher->compositor;
1983 struct weston_surface *surface;
1984 struct weston_input_device *device =
1985 (struct weston_input_device *) switcher->grab.input_device;
1986
1987 wl_list_for_each(surface, &compositor->surface_list, link) {
1988 surface->alpha = 255;
1989 weston_surface_damage(surface);
1990 }
1991
Alex Wu07b26062012-03-12 16:06:01 +08001992 if (switcher->current)
1993 activate(compositor->shell, switcher->current, device, time);
Kristian Høgsberg07045392012-02-19 18:52:44 -05001994 wl_list_remove(&switcher->listener.link);
1995 wl_input_device_end_keyboard_grab(&device->input_device, time);
1996 free(switcher);
1997}
1998
1999static void
2000switcher_key(struct wl_keyboard_grab *grab,
2001 uint32_t time, uint32_t key, int32_t state)
2002{
2003 struct switcher *switcher = container_of(grab, struct switcher, grab);
2004 struct weston_input_device *device =
2005 (struct weston_input_device *) grab->input_device;
2006
2007 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
2008 switcher_destroy(switcher, time);
2009 } else if (key == KEY_TAB && state) {
2010 switcher_next(switcher);
2011 }
2012};
2013
2014static const struct wl_keyboard_grab_interface switcher_grab = {
2015 switcher_key
2016};
2017
2018static void
2019switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002020 uint32_t key, uint32_t button, uint32_t axis,
2021 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002022{
2023 struct weston_compositor *compositor = data;
2024 struct switcher *switcher;
2025
2026 switcher = malloc(sizeof *switcher);
2027 switcher->compositor = compositor;
2028 switcher->current = NULL;
2029 switcher->listener.func = switcher_handle_surface_destroy;
2030 wl_list_init(&switcher->listener.link);
2031
2032 switcher->grab.interface = &switcher_grab;
2033 wl_input_device_start_keyboard_grab(device, &switcher->grab, time);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05002034 wl_input_device_set_keyboard_focus(device, NULL,
2035 weston_compositor_get_time());
Kristian Høgsberg07045392012-02-19 18:52:44 -05002036 switcher_next(switcher);
2037}
2038
Pekka Paalanen3c647232011-12-22 13:43:43 +02002039static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002040backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002041 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002042{
2043 struct weston_compositor *compositor = data;
2044 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002045 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002046
2047 /* TODO: we're limiting to simple use cases, where we assume just
2048 * control on the primary display. We'd have to extend later if we
2049 * ever get support for setting backlights on random desktop LCD
2050 * panels though */
2051 output = get_default_output(compositor);
2052 if (!output)
2053 return;
2054
2055 if (!output->set_backlight)
2056 return;
2057
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002058 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2059 backlight_new = output->backlight_current - 25;
2060 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2061 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002062
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002063 if (backlight_new < 5)
2064 backlight_new = 5;
2065 if (backlight_new > 255)
2066 backlight_new = 255;
2067
2068 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002069 output->set_backlight(output, output->backlight_current);
2070}
2071
2072static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002073debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002074 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002075{
2076 struct weston_compositor *compositor = data;
2077 struct wl_shell *shell =
2078 container_of(compositor->shell, struct wl_shell, shell);
2079 struct weston_surface *surface;
2080
2081 if (shell->debug_repaint_surface) {
2082 weston_surface_destroy(shell->debug_repaint_surface);
2083 shell->debug_repaint_surface = NULL;
2084 } else {
2085 surface = weston_surface_create(compositor);
2086 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2087 weston_surface_configure(surface, 0, 0, 8192, 8192);
2088 wl_list_insert(&compositor->fade_layer.surface_list,
2089 &surface->layer_link);
2090 weston_surface_assign_output(surface);
2091 pixman_region32_init(&surface->input);
2092
2093 /* Here's the dirty little trick that makes the
2094 * repaint debugging work: we force an
2095 * update_transform first to update dependent state
2096 * and clear the geometry.dirty bit. Then we clear
2097 * the surface damage so it only gets repainted
2098 * piecewise as we repaint other things. */
2099
2100 weston_surface_update_transform(surface);
2101 pixman_region32_fini(&surface->damage);
2102 pixman_region32_init(&surface->damage);
2103 shell->debug_repaint_surface = surface;
2104 }
2105}
2106
2107static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05002108shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002109{
2110 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
2111
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002112 if (shell->child.client)
2113 wl_client_destroy(shell->child.client);
2114
Pekka Paalanen3c647232011-12-22 13:43:43 +02002115 free(shell->screensaver.path);
2116 free(shell);
2117}
2118
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002119int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002120shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002121
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002122WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002123shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002124{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002125 struct wl_shell *shell;
2126
2127 shell = malloc(sizeof *shell);
2128 if (shell == NULL)
2129 return -1;
2130
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002131 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002132 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002133 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002134 shell->shell.unlock = unlock;
Pekka Paalanen3c647232011-12-22 13:43:43 +02002135 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002136
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002137 wl_list_init(&shell->backgrounds);
2138 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002139 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002140
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002141 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2142 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2143 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2144 weston_layer_init(&shell->background_layer,
2145 &shell->toplevel_layer.link);
2146 wl_list_init(&shell->lock_layer.surface_list);
2147
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002148 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002149
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002150 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2151 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002152 return -1;
2153
Kristian Høgsberg75840622011-09-06 13:48:16 -04002154 if (wl_display_add_global(ec->wl_display,
2155 &desktop_shell_interface,
2156 shell, bind_desktop_shell) == NULL)
2157 return -1;
2158
Pekka Paalanen6e168112011-11-24 11:34:05 +02002159 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2160 shell, bind_screensaver) == NULL)
2161 return -1;
2162
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002163 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002164 if (launch_desktop_shell_process(shell) != 0)
2165 return -1;
2166
Scott Moreau6a3633d2012-03-20 08:47:59 -06002167 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, MODIFIER_SUPER,
2168 move_binding, shell);
2169 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, MODIFIER_SUPER,
2170 resize_binding, shell);
2171 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2172 MODIFIER_CTRL | MODIFIER_ALT,
2173 terminate_binding, ec);
2174 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2175 click_to_activate_binding, ec);
Scott Moreau1b45a792012-03-22 10:58:23 -06002176 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002177 MODIFIER_SUPER | MODIFIER_ALT,
2178 surface_opacity_binding, NULL);
2179 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreau1b45a792012-03-22 10:58:23 -06002180 MODIFIER_SUPER, zoom_binding, NULL);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002181 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002182 MODIFIER_SUPER | MODIFIER_ALT,
2183 rotate_binding, NULL);
2184 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, MODIFIER_SUPER,
2185 switcher_binding, ec);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002186
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002187 /* brightness */
Scott Moreau6a3633d2012-03-20 08:47:59 -06002188 weston_compositor_add_binding(ec, KEY_F9, 0, 0, MODIFIER_CTRL,
2189 backlight_binding, ec);
2190 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2191 backlight_binding, ec);
2192 weston_compositor_add_binding(ec, KEY_F10, 0, 0, MODIFIER_CTRL,
2193 backlight_binding, ec);
2194 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2195 backlight_binding, ec);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002196
Scott Moreau6a3633d2012-03-20 08:47:59 -06002197 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, MODIFIER_SUPER,
2198 debug_repaint_binding, ec);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002199
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002200 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002201
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002202 return 0;
2203}