blob: 02f203a2ee209afef609e2ea9d18db3bb410a9ca [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 Wu4539b082012-03-01 12:57:46 +0800522static struct weston_surface *
523create_black_surface(struct weston_compositor *ec,
524 GLfloat x, GLfloat y, int w, int h)
525{
526 struct weston_surface *surface = NULL;
527
528 surface = weston_surface_create(ec);
529 if (surface == NULL) {
530 fprintf(stderr, "no memory\n");
531 return NULL;
532 }
533
534 weston_surface_configure(surface, x, y, w, h);
535 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
536 return surface;
537}
538
539/* Create black surface and append it to the associated fullscreen surface.
540 * Handle size dismatch and positioning according to the method. */
541static void
542shell_configure_fullscreen(struct shell_surface *shsurf)
543{
544 struct weston_output *output = shsurf->fullscreen_output;
545 struct weston_surface *surface = shsurf->surface;
546 struct weston_matrix *matrix;
547 float scale;
548
549 center_on_output(surface, output);
550
551 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500552 shsurf->fullscreen.black_surface =
553 create_black_surface(surface->compositor,
554 output->x, output->y,
555 output->current->width,
556 output->current->height);
557
558 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
559 wl_list_insert(&surface->layer_link,
560 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800561 shsurf->fullscreen.black_surface->output = output;
562
563 switch (shsurf->fullscreen.type) {
564 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
565 break;
566 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
567 matrix = &shsurf->fullscreen.transform.matrix;
568 weston_matrix_init(matrix);
569 scale = (float)output->current->width/(float)surface->geometry.width;
570 weston_matrix_scale(matrix, scale, scale, 1);
571 wl_list_remove(&shsurf->fullscreen.transform.link);
572 wl_list_insert(surface->geometry.transformation_list.prev,
573 &shsurf->fullscreen.transform.link);
574 weston_surface_set_position(surface, output->x, output->y);
575 break;
576 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
577 break;
578 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
579 break;
580 default:
581 break;
582 }
583}
584
585/* make the fullscreen and black surface at the top */
586static void
587shell_stack_fullscreen(struct shell_surface *shsurf)
588{
589 struct weston_surface *surface = shsurf->surface;
590 struct wl_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800591
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500592 wl_list_remove(&surface->layer_link);
593 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800594
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500595 wl_list_insert(&shell->fullscreen_layer.surface_list,
596 &surface->layer_link);
597 wl_list_insert(&surface->layer_link,
598 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800599
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500600 weston_surface_damage(surface);
601 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800602}
603
604static void
605shell_map_fullscreen(struct shell_surface *shsurf)
606{
607 shell_configure_fullscreen(shsurf);
608 shell_stack_fullscreen(shsurf);
609}
610
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400611static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200612shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500613 struct wl_resource *resource,
614 uint32_t method,
615 uint32_t framerate,
616 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400617{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200618 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500619 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800620
621 if (output_resource)
622 shsurf->output = output_resource->data;
623 else
624 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400625
Pekka Paalanen98262232011-12-01 10:42:22 +0200626 if (reset_shell_surface_type(shsurf))
627 return;
628
Alex Wu4539b082012-03-01 12:57:46 +0800629 shsurf->fullscreen_output = shsurf->output;
630 shsurf->fullscreen.type = method;
631 shsurf->fullscreen.framerate = framerate;
632 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400633
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200634 shsurf->saved_x = es->geometry.x;
635 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800636 shsurf->saved_position_valid = true;
637
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300638 if (weston_surface_is_mapped(es))
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300639 shsurf->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500640
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500641 wl_shell_surface_send_configure(&shsurf->resource,
642 weston_compositor_get_time(), 0,
643 shsurf->output->current->width,
644 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400645}
646
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500647static void
Scott Moreau447013d2012-02-18 05:05:29 -0700648popup_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500649 struct wl_surface *surface, int32_t x, int32_t y)
650{
651 struct wl_input_device *device = grab->input_device;
652 struct shell_surface *priv =
653 container_of(grab, struct shell_surface, popup.grab);
654 struct wl_client *client = priv->surface->surface.resource.client;
655
Pekka Paalanencb108432012-01-19 16:25:40 +0200656 if (surface && surface->resource.client == client) {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200657 wl_input_device_set_pointer_focus(device, surface, time, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500658 grab->focus = surface;
659 } else {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200660 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500661 grab->focus = NULL;
662 }
663}
664
665static void
Scott Moreau447013d2012-02-18 05:05:29 -0700666popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200667 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500668{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500669 struct wl_resource *resource;
670
671 resource = grab->input_device->pointer_focus_resource;
672 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500673 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500674}
675
676static void
Scott Moreau447013d2012-02-18 05:05:29 -0700677popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400678 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500679{
680 struct wl_resource *resource;
681 struct shell_surface *shsurf =
682 container_of(grab, struct shell_surface, popup.grab);
683
684 resource = grab->input_device->pointer_focus_resource;
685 if (resource) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500686 wl_input_device_send_button(resource, time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500687 } else if (state == 0 &&
688 (shsurf->popup.initial_up ||
689 time - shsurf->popup.time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500690 wl_shell_surface_send_popup_done(&shsurf->resource);
Scott Moreau447013d2012-02-18 05:05:29 -0700691 wl_input_device_end_pointer_grab(grab->input_device, time);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500692 shsurf->popup.grab.input_device = NULL;
693 }
694
695 if (state == 0)
696 shsurf->popup.initial_up = 1;
697}
698
Scott Moreau447013d2012-02-18 05:05:29 -0700699static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500700 popup_grab_focus,
701 popup_grab_motion,
702 popup_grab_button,
703};
704
705static void
706shell_map_popup(struct shell_surface *shsurf, uint32_t time)
707{
708 struct wl_input_device *device;
709 struct weston_surface *es = shsurf->surface;
710 struct weston_surface *parent = shsurf->parent->surface;
711
712 es->output = parent->output;
713
714 shsurf->popup.grab.interface = &popup_grab_interface;
715 device = es->compositor->input_device;
716
Pekka Paalanen938269a2012-02-07 14:19:01 +0200717 weston_surface_update_transform(parent);
718 if (parent->transform.enabled) {
719 shsurf->popup.parent_transform.matrix =
720 parent->transform.matrix;
721 } else {
722 /* construct x, y translation matrix */
723 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
724 shsurf->popup.parent_transform.matrix.d[12] =
725 parent->geometry.x;
726 shsurf->popup.parent_transform.matrix.d[13] =
727 parent->geometry.y;
728 }
729 wl_list_insert(es->geometry.transformation_list.prev,
730 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200731 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500732
733 shsurf->popup.grab.input_device = device;
734 shsurf->popup.time = device->grab_time;
735 shsurf->popup.initial_up = 0;
736
Scott Moreau447013d2012-02-18 05:05:29 -0700737 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500738 &shsurf->popup.grab, shsurf->popup.time);
739}
740
741static void
742shell_surface_set_popup(struct wl_client *client,
743 struct wl_resource *resource,
744 struct wl_resource *input_device_resource,
745 uint32_t time,
746 struct wl_resource *parent_resource,
747 int32_t x, int32_t y, uint32_t flags)
748{
749 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500750
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500751 shsurf->type = SHELL_SURFACE_POPUP;
752 shsurf->parent = parent_resource->data;
753 shsurf->popup.x = x;
754 shsurf->popup.y = y;
755}
756
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200757static const struct wl_shell_surface_interface shell_surface_implementation = {
758 shell_surface_move,
759 shell_surface_resize,
760 shell_surface_set_toplevel,
761 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500762 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +0800763 shell_surface_set_popup,
764 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200765};
766
767static void
768destroy_shell_surface(struct wl_resource *resource)
769{
770 struct shell_surface *shsurf = resource->data;
771
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500772 if (shsurf->popup.grab.input_device)
Scott Moreau447013d2012-02-18 05:05:29 -0700773 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500774
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200775 /* in case cleaning up a dead client destroys shell_surface first */
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300776 if (shsurf->surface) {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200777 wl_list_remove(&shsurf->surface_destroy_listener.link);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300778 shsurf->surface->configure = NULL;
779 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200780
Alex Wuaa08e2d2012-03-05 11:01:40 +0800781 if (shsurf->fullscreen.black_surface)
782 weston_surface_destroy(shsurf->fullscreen.black_surface);
783
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200784 wl_list_remove(&shsurf->link);
785 free(shsurf);
786}
787
788static void
789shell_handle_surface_destroy(struct wl_listener *listener,
790 struct wl_resource *resource, uint32_t time)
791{
792 struct shell_surface *shsurf = container_of(listener,
793 struct shell_surface,
794 surface_destroy_listener);
795
796 shsurf->surface = NULL;
797 wl_resource_destroy(&shsurf->resource, time);
798}
799
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200800static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500801get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200802{
803 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
804 struct wl_listener *listener;
805
806 /* search the destroy listener list for our callback */
807 wl_list_for_each(listener, lst, link) {
808 if (listener->func == shell_handle_surface_destroy) {
809 return container_of(listener, struct shell_surface,
810 surface_destroy_listener);
811 }
812 }
813
814 return NULL;
815}
816
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200817static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300818shell_surface_configure(struct weston_surface *, int32_t, int32_t);
819
820static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200821shell_get_shell_surface(struct wl_client *client,
822 struct wl_resource *resource,
823 uint32_t id,
824 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200825{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500826 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200827 struct shell_surface *shsurf;
828
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200829 if (get_shell_surface(surface)) {
830 wl_resource_post_error(surface_resource,
831 WL_DISPLAY_ERROR_INVALID_OBJECT,
832 "wl_shell::get_shell_surface already requested");
833 return;
834 }
835
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300836 if (surface->configure) {
837 wl_resource_post_error(surface_resource,
838 WL_DISPLAY_ERROR_INVALID_OBJECT,
839 "surface->configure already set");
840 return;
841 }
842
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200843 shsurf = calloc(1, sizeof *shsurf);
844 if (!shsurf) {
845 wl_resource_post_no_memory(resource);
846 return;
847 }
848
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300849 surface->configure = shell_surface_configure;
850
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200851 shsurf->resource.destroy = destroy_shell_surface;
852 shsurf->resource.object.id = id;
853 shsurf->resource.object.interface = &wl_shell_surface_interface;
854 shsurf->resource.object.implementation =
855 (void (**)(void)) &shell_surface_implementation;
856 shsurf->resource.data = shsurf;
857
Alex Wu4539b082012-03-01 12:57:46 +0800858 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200859 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +0800860 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
861 shsurf->fullscreen.framerate = 0;
862 shsurf->fullscreen.black_surface = NULL;
863 wl_list_init(&shsurf->fullscreen.transform.link);
864
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200865 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
866 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
867 &shsurf->surface_destroy_listener.link);
868
869 /* init link so its safe to always remove it in destroy_shell_surface */
870 wl_list_init(&shsurf->link);
871
Pekka Paalanen460099f2012-01-20 16:48:25 +0200872 /* empty when not in use */
873 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500874 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +0200875
Pekka Paalanen98262232011-12-01 10:42:22 +0200876 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200877
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200878 wl_client_add_resource(client, &shsurf->resource);
879}
880
881static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200882 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500883};
884
Kristian Høgsberg07937562011-04-12 17:25:42 -0400885static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500886handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200887{
888 proc->pid = 0;
889}
890
891static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200892launch_screensaver(struct wl_shell *shell)
893{
894 if (shell->screensaver.binding)
895 return;
896
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200897 if (!shell->screensaver.path)
898 return;
899
Kristian Høgsberg32bed572012-03-01 17:11:36 -0500900 if (shell->screensaver.process.pid != 0) {
901 fprintf(stderr, "old screensaver still running\n");
902 return;
903 }
904
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500905 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200906 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200907 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200908 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200909}
910
911static void
912terminate_screensaver(struct wl_shell *shell)
913{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200914 if (shell->screensaver.process.pid == 0)
915 return;
916
917 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200918}
919
920static void
921show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
922{
923 struct wl_list *list;
924
925 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500926 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200927 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500928 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200929
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500930 wl_list_remove(&surface->surface->layer_link);
931 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200932 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200933 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200934}
935
936static void
937hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
938{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500939 wl_list_remove(&surface->surface->layer_link);
940 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200941 surface->surface->output = NULL;
942}
943
944static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400945desktop_shell_set_background(struct wl_client *client,
946 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100947 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400948 struct wl_resource *surface_resource)
949{
950 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200951 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500952 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200953 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400954
Pekka Paalanen98262232011-12-01 10:42:22 +0200955 if (reset_shell_surface_type(shsurf))
956 return;
957
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100958 wl_list_for_each(priv, &shell->backgrounds, link) {
959 if (priv->output == output_resource->data) {
960 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500961 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100962 wl_list_remove(&priv->link);
963 break;
964 }
965 }
966
Pekka Paalanen068ae942011-11-28 14:11:15 +0200967 shsurf->type = SHELL_SURFACE_BACKGROUND;
968 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100969
Pekka Paalanen068ae942011-11-28 14:11:15 +0200970 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100971
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200972 weston_surface_set_position(surface, shsurf->output->x,
973 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200974
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500975 desktop_shell_send_configure(resource,
976 weston_compositor_get_time(), 0,
977 surface_resource,
978 shsurf->output->current->width,
979 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400980}
981
982static void
983desktop_shell_set_panel(struct wl_client *client,
984 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100985 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400986 struct wl_resource *surface_resource)
987{
988 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200989 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500990 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200991 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400992
Pekka Paalanen98262232011-12-01 10:42:22 +0200993 if (reset_shell_surface_type(shsurf))
994 return;
995
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100996 wl_list_for_each(priv, &shell->panels, link) {
997 if (priv->output == output_resource->data) {
998 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500999 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001000 wl_list_remove(&priv->link);
1001 break;
1002 }
1003 }
1004
Pekka Paalanen068ae942011-11-28 14:11:15 +02001005 shsurf->type = SHELL_SURFACE_PANEL;
1006 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001007
Pekka Paalanen068ae942011-11-28 14:11:15 +02001008 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001009
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001010 weston_surface_set_position(surface, shsurf->output->x,
1011 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001012
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001013 desktop_shell_send_configure(resource,
1014 weston_compositor_get_time(), 0,
1015 surface_resource,
1016 shsurf->output->current->width,
1017 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001018}
1019
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001020static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001021handle_lock_surface_destroy(struct wl_listener *listener,
1022 struct wl_resource *resource, uint32_t time)
1023{
1024 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +02001025 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001026
1027 fprintf(stderr, "lock surface gone\n");
1028 shell->lock_surface = NULL;
1029}
1030
1031static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001032desktop_shell_set_lock_surface(struct wl_client *client,
1033 struct wl_resource *resource,
1034 struct wl_resource *surface_resource)
1035{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001036 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001037 struct shell_surface *surface = surface_resource->data;
1038
1039 if (reset_shell_surface_type(surface))
1040 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001041
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001042 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001043
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001044 if (!shell->locked)
1045 return;
1046
Pekka Paalanen98262232011-12-01 10:42:22 +02001047 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001048
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001049 shell->lock_surface_listener.func = handle_lock_surface_destroy;
1050 wl_list_insert(&surface_resource->destroy_listener_list,
1051 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001052
Pekka Paalanen068ae942011-11-28 14:11:15 +02001053 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001054}
1055
1056static void
1057resume_desktop(struct wl_shell *shell)
1058{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001059 struct shell_surface *tmp;
1060
1061 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1062 hide_screensaver(shell, tmp);
1063
1064 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001065
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001066 wl_list_remove(&shell->lock_layer.link);
1067 wl_list_insert(&shell->compositor->cursor_layer.link,
1068 &shell->fullscreen_layer.link);
1069 wl_list_insert(&shell->fullscreen_layer.link,
1070 &shell->panel_layer.link);
1071 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001072
1073 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001074 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001075 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001076 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001077}
1078
1079static void
1080desktop_shell_unlock(struct wl_client *client,
1081 struct wl_resource *resource)
1082{
1083 struct wl_shell *shell = resource->data;
1084
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001085 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001086
1087 if (shell->locked)
1088 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001089}
1090
Kristian Høgsberg75840622011-09-06 13:48:16 -04001091static const struct desktop_shell_interface desktop_shell_implementation = {
1092 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001093 desktop_shell_set_panel,
1094 desktop_shell_set_lock_surface,
1095 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001096};
1097
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001098static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001099get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001100{
1101 struct shell_surface *shsurf;
1102
1103 shsurf = get_shell_surface(surface);
1104 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001105 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001106 return shsurf->type;
1107}
1108
Kristian Høgsberg75840622011-09-06 13:48:16 -04001109static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001110move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001111 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001112{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001113 struct weston_surface *surface =
1114 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001115
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001116 if (surface == NULL)
1117 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001118
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001119 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001120 case SHELL_SURFACE_PANEL:
1121 case SHELL_SURFACE_BACKGROUND:
1122 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001123 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001124 return;
1125 default:
1126 break;
1127 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001128
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001129 weston_surface_move(surface, (struct weston_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001130}
1131
1132static void
1133resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001134 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001135{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001136 struct weston_surface *surface =
1137 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001138 uint32_t edges = 0;
1139 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001140 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001141
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001142 if (surface == NULL)
1143 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001144
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001145 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001146 if (!shsurf)
1147 return;
1148
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001149 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001150 case SHELL_SURFACE_PANEL:
1151 case SHELL_SURFACE_BACKGROUND:
1152 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001153 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001154 return;
1155 default:
1156 break;
1157 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001158
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001159 weston_surface_from_global(surface,
1160 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001161
Pekka Paalanen60921e52012-01-25 15:55:43 +02001162 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001163 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001164 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001165 edges |= 0;
1166 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001167 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001168
Pekka Paalanen60921e52012-01-25 15:55:43 +02001169 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001170 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001171 else if (y < 2 * surface->geometry.height / 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_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001175
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001176 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001177 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001178}
1179
1180static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001181surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1182 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1183{
1184 int step = 15;
1185 struct shell_surface *shsurf;
1186 struct weston_surface *surface =
1187 (struct weston_surface *) device->pointer_focus;
1188
1189 if (surface == NULL)
1190 return;
1191
1192 shsurf = get_shell_surface(surface);
1193 if (!shsurf)
1194 return;
1195
1196 switch (shsurf->type) {
1197 case SHELL_SURFACE_BACKGROUND:
1198 case SHELL_SURFACE_SCREENSAVER:
1199 return;
1200 default:
1201 break;
1202 }
1203
1204 surface->alpha += value * step;
1205
1206 if (surface->alpha > 255)
1207 surface->alpha = 255;
1208 if (surface->alpha < step)
1209 surface->alpha = step;
1210
1211 surface->geometry.dirty = 1;
1212 weston_surface_damage(surface);
1213}
1214
1215static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001216zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001217 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001218{
1219 struct weston_input_device *wd = (struct weston_input_device *) device;
1220 struct weston_compositor *compositor = wd->compositor;
1221 struct weston_output *output;
1222
1223 wl_list_for_each(output, &compositor->output_list, link) {
1224 if (pixman_region32_contains_point(&output->region,
1225 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001226 output->zoom.active = 1;
1227 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001228
1229 if (output->zoom.level >= 1.0) {
1230 output->zoom.active = 0;
1231 output->zoom.level = 1.0;
1232 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001233
1234 if (output->zoom.level < output->zoom.increment)
1235 output->zoom.level = output->zoom.increment;
1236
1237 weston_output_update_zoom(output, device->x, device->y);
1238 }
1239 }
1240}
1241
Scott Moreauccbf29d2012-02-22 14:21:41 -07001242static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001243terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001244 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001245{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001246 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001247
1248 if (state)
1249 wl_display_terminate(compositor->wl_display);
1250}
1251
1252static void
Scott Moreau447013d2012-02-18 05:05:29 -07001253rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001254 uint32_t time, int32_t x, int32_t y)
1255{
1256 struct rotate_grab *rotate =
1257 container_of(grab, struct rotate_grab, grab);
1258 struct wl_input_device *device = grab->input_device;
1259 struct shell_surface *surface = rotate->surface;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001260 struct weston_surface *base_surface = surface->surface;
1261 GLfloat cx = 0.5f * base_surface->geometry.width;
1262 GLfloat cy = 0.5f * base_surface->geometry.height;
1263 GLfloat dx, dy, cposx, cposy, dposx, dposy;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001264 GLfloat r;
1265
1266 dx = device->x - rotate->center.x;
1267 dy = device->y - rotate->center.y;
1268 r = sqrtf(dx * dx + dy * dy);
1269
1270 wl_list_remove(&surface->rotation.transform.link);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001271 surface->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001272
1273 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001274 struct weston_matrix *matrix =
1275 &surface->rotation.transform.matrix;
1276
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001277 weston_matrix_init(&rotate->rotation);
1278 rotate->rotation.d[0] = dx / r;
1279 rotate->rotation.d[4] = -dy / r;
1280 rotate->rotation.d[1] = -rotate->rotation.d[4];
1281 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001282
1283 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001284 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001285 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1286 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001287 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001288
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001289 wl_list_insert(
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001290 &surface->surface->geometry.transformation_list,
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001291 &surface->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001292 } else {
1293 wl_list_init(&surface->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001294 weston_matrix_init(&surface->rotation.rotation);
1295 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001296 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001297
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001298 /* We need to adjust the position of the surface
1299 * in case it was resized in a rotated state before */
1300 cposx = base_surface->geometry.x + cx;
1301 cposy = base_surface->geometry.y + cy;
1302 dposx = rotate->center.x - cposx;
1303 dposy = rotate->center.y - cposy;
1304 if (dposx != 0.0f || dposy != 0.0f) {
1305 weston_surface_set_position(base_surface,
1306 base_surface->geometry.x + dposx,
1307 base_surface->geometry.y + dposy);
1308 }
1309
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001310 /* Repaint implies weston_surface_update_transform(), which
1311 * lazily applies the damage due to rotation update.
1312 */
1313 weston_compositor_schedule_repaint(surface->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001314}
1315
1316static void
Scott Moreau447013d2012-02-18 05:05:29 -07001317rotate_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001318 uint32_t time, uint32_t button, int32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001319{
1320 struct rotate_grab *rotate =
1321 container_of(grab, struct rotate_grab, grab);
1322 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001323 struct shell_surface *surface = rotate->surface;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001324
1325 if (device->button_count == 0 && state == 0) {
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001326 weston_matrix_multiply(&surface->rotation.rotation,
1327 &rotate->rotation);
Scott Moreau447013d2012-02-18 05:05:29 -07001328 wl_input_device_end_pointer_grab(device, time);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001329 free(rotate);
1330 }
1331}
1332
Scott Moreau447013d2012-02-18 05:05:29 -07001333static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001334 noop_grab_focus,
1335 rotate_grab_motion,
1336 rotate_grab_button,
1337};
1338
1339static void
1340rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001341 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001342{
1343 struct weston_surface *base_surface =
1344 (struct weston_surface *) device->pointer_focus;
1345 struct shell_surface *surface;
1346 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001347 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001348 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001349
1350 if (base_surface == NULL)
1351 return;
1352
1353 surface = get_shell_surface(base_surface);
1354 if (!surface)
1355 return;
1356
1357 switch (surface->type) {
1358 case SHELL_SURFACE_PANEL:
1359 case SHELL_SURFACE_BACKGROUND:
1360 case SHELL_SURFACE_FULLSCREEN:
1361 case SHELL_SURFACE_SCREENSAVER:
1362 return;
1363 default:
1364 break;
1365 }
1366
Pekka Paalanen460099f2012-01-20 16:48:25 +02001367 rotate = malloc(sizeof *rotate);
1368 if (!rotate)
1369 return;
1370
1371 rotate->grab.interface = &rotate_grab_interface;
1372 rotate->surface = surface;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001373
1374 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001375 surface->surface->geometry.width / 2,
1376 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001377 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001378
Scott Moreau447013d2012-02-18 05:05:29 -07001379 wl_input_device_start_pointer_grab(device, &rotate->grab, time);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001380
1381 dx = device->x - rotate->center.x;
1382 dy = device->y - rotate->center.y;
1383 r = sqrtf(dx * dx + dy * dy);
1384 if (r > 20.0f) {
1385 struct weston_matrix inverse;
1386
1387 weston_matrix_init(&inverse);
1388 inverse.d[0] = dx / r;
1389 inverse.d[4] = dy / r;
1390 inverse.d[1] = -inverse.d[4];
1391 inverse.d[5] = inverse.d[0];
1392 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001393
1394 weston_matrix_init(&rotate->rotation);
1395 rotate->rotation.d[0] = dx / r;
1396 rotate->rotation.d[4] = -dy / r;
1397 rotate->rotation.d[1] = -rotate->rotation.d[4];
1398 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001399 } else {
1400 weston_matrix_init(&surface->rotation.rotation);
1401 weston_matrix_init(&rotate->rotation);
1402 }
1403
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001404 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001405}
1406
1407static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001408activate(struct weston_shell *base, struct weston_surface *es,
1409 struct weston_input_device *device, uint32_t time)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001410{
1411 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001412 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001413
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001414 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001415
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001416 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001417 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001418
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001419 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001420 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001421 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001422 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001423 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001424
Pekka Paalanen77346a62011-11-30 16:26:35 +02001425 case SHELL_SURFACE_SCREENSAVER:
1426 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001427 if (shell->lock_surface)
1428 weston_surface_restack(es,
1429 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001430 break;
Alex Wu4539b082012-03-01 12:57:46 +08001431 case SHELL_SURFACE_FULLSCREEN:
1432 /* should on top of panels */
1433 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001434 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001435 weston_surface_restack(es,
1436 &shell->toplevel_layer.surface_list);
1437 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001438 }
1439}
1440
1441static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001442click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001443 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001444 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001445{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001446 struct weston_input_device *wd = (struct weston_input_device *) device;
1447 struct weston_compositor *compositor = data;
1448 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001449 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001450
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001451 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001452 if (!focus)
1453 return;
1454
Alex Wu4539b082012-03-01 12:57:46 +08001455 upper = container_of(focus->link.prev, struct weston_surface, link);
1456 if (focus->link.prev != &compositor->surface_list &&
1457 get_shell_surface_type(upper) == SHELL_SURFACE_FULLSCREEN) {
1458 printf("%s: focus is black surface, raise its fullscreen surface\n", __func__);
1459 shell_stack_fullscreen(get_shell_surface(upper));
1460 focus = upper;
1461 }
1462
Alex Wu9c35e6b2012-03-05 14:13:13 +08001463 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001464 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001465}
1466
1467static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001468lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001469{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001470 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001471 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001472 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001473 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001474 uint32_t time;
1475
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001476 if (shell->locked) {
1477 wl_list_for_each(output, &shell->compositor->output_list, link)
1478 /* TODO: find a way to jump to other DPMS levels */
1479 if (output->set_dpms)
1480 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001481 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001482 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001483
1484 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001485
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001486 /* Hide all surfaces by removing the fullscreen, panel and
1487 * toplevel layers. This way nothing else can show or receive
1488 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001489
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001490 wl_list_remove(&shell->panel_layer.link);
1491 wl_list_remove(&shell->toplevel_layer.link);
1492 wl_list_remove(&shell->fullscreen_layer.link);
1493 wl_list_insert(&shell->compositor->cursor_layer.link,
1494 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001495
Pekka Paalanen77346a62011-11-30 16:26:35 +02001496 launch_screensaver(shell);
1497
1498 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1499 show_screensaver(shell, shsurf);
1500
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001501 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001502 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001503 weston_compositor_wake(shell->compositor);
1504 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001505 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001506
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001507 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001508 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001509
1510 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001511 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001512 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1513 wl_input_device_set_keyboard_focus(&device->input_device,
1514 NULL, time);
1515 }
1516
1517 /* TODO: disable bindings that should not work while locked. */
1518
1519 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001520}
1521
1522static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001523unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001524{
1525 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1526
Pekka Paalanend81c2162011-11-16 13:47:34 +02001527 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001528 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001529 return;
1530 }
1531
1532 /* If desktop-shell client has gone away, unlock immediately. */
1533 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001534 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001535 return;
1536 }
1537
1538 if (shell->prepare_event_sent)
1539 return;
1540
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001541 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001542 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001543}
1544
1545static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001546center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001547{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001548 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001549 GLfloat x = (mode->width - surface->geometry.width) / 2;
1550 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001551
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001552 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001553}
1554
1555static void
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001556map(struct weston_shell *base, struct weston_surface *surface,
1557 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001558{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001559 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001560 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001561 struct shell_surface *shsurf;
1562 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001563 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001564 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001565
Pekka Paalanen77346a62011-11-30 16:26:35 +02001566 shsurf = get_shell_surface(surface);
1567 if (shsurf)
1568 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001569
Pekka Paalanen60921e52012-01-25 15:55:43 +02001570 surface->geometry.width = width;
1571 surface->geometry.height = height;
1572 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001573
1574 /* initial positioning, see also configure() */
1575 switch (surface_type) {
1576 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001577 weston_surface_set_position(surface, 10 + random() % 400,
1578 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001579 break;
1580 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001581 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001582 break;
Alex Wu4539b082012-03-01 12:57:46 +08001583 case SHELL_SURFACE_FULLSCREEN:
1584 shell_map_fullscreen(shsurf);
1585 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001586 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001587 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001588 panel_height = get_output_panel_height(shell,surface->output);
1589 weston_surface_set_position(surface, surface->output->x,
1590 surface->output->y + panel_height);
1591 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001592 case SHELL_SURFACE_LOCK:
1593 center_on_output(surface, get_default_output(compositor));
1594 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001595 case SHELL_SURFACE_POPUP:
1596 shell_map_popup(shsurf, shsurf->popup.time);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001597 case SHELL_SURFACE_NONE:
1598 weston_surface_set_position(surface,
1599 surface->geometry.x + sx,
1600 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001601 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001602 default:
1603 ;
1604 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001605
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001606 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001607 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001608 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001609 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001610 wl_list_insert(&shell->background_layer.surface_list,
1611 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001612 break;
1613 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001614 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001615 wl_list_insert(&shell->panel_layer.surface_list,
1616 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001617 break;
1618 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001619 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001620 wl_list_insert(&shell->lock_layer.surface_list,
1621 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001622 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001623 break;
1624 case SHELL_SURFACE_SCREENSAVER:
1625 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001626 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001627 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001628 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001629 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001630 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001631 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001632 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001633 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001634 case SHELL_SURFACE_POPUP:
1635 case SHELL_SURFACE_TRANSIENT:
1636 parent = shsurf->parent->surface;
1637 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1638 break;
Alex Wu4539b082012-03-01 12:57:46 +08001639 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001640 case SHELL_SURFACE_NONE:
1641 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001642 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001643 wl_list_insert(&shell->toplevel_layer.surface_list,
1644 &surface->layer_link);
1645 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001646 }
1647
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001648 if (surface_type != SHELL_SURFACE_NONE) {
1649 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001650 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1651 surface->output = shsurf->output;
1652 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001653
Juan Zhao7bb92f02011-12-15 11:31:51 -05001654 switch (surface_type) {
1655 case SHELL_SURFACE_TOPLEVEL:
1656 case SHELL_SURFACE_TRANSIENT:
1657 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001658 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001659 if (!shell->locked)
1660 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001661 (struct weston_input_device *)
Alex Wu4539b082012-03-01 12:57:46 +08001662 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001663 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001664 break;
1665 default:
1666 break;
1667 }
1668
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001669 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001670 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001671}
1672
1673static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001674configure(struct weston_shell *base, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001675 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001676{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001677 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001678 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Alex Wu4539b082012-03-01 12:57:46 +08001679 enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001680 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001681
Pekka Paalanen77346a62011-11-30 16:26:35 +02001682 shsurf = get_shell_surface(surface);
1683 if (shsurf)
1684 surface_type = shsurf->type;
1685
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001686 surface->geometry.x = x;
1687 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001688 surface->geometry.width = width;
1689 surface->geometry.height = height;
1690 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001691
1692 switch (surface_type) {
1693 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001694 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001695 break;
Alex Wu4539b082012-03-01 12:57:46 +08001696 case SHELL_SURFACE_FULLSCREEN:
1697 shell_configure_fullscreen(shsurf);
1698 if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
1699 shell_stack_fullscreen(shsurf);
1700 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001701 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001702 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001703 surface->geometry.x = surface->output->x;
1704 surface->geometry.y = surface->output->y +
1705 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001706 break;
Alex Wu4539b082012-03-01 12:57:46 +08001707 case SHELL_SURFACE_TOPLEVEL:
1708 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001709 default:
1710 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001711 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001712
Alex Wu4539b082012-03-01 12:57:46 +08001713 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001714 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001715 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001716
1717 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1718 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001719 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1720 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001721 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001722}
1723
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001724static void
1725shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1726{
1727 struct weston_shell *shell = es->compositor->shell;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001728 struct shell_surface *shsurf = get_shell_surface(es);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001729
1730 if (!weston_surface_is_mapped(es)) {
1731 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001732 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001733 es->geometry.width != es->buffer->width ||
1734 es->geometry.height != es->buffer->height) {
1735 GLfloat from_x, from_y;
1736 GLfloat to_x, to_y;
1737
1738 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
1739 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
1740 configure(shell, es,
1741 es->geometry.x + to_x - from_x,
1742 es->geometry.y + to_y - from_y,
1743 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001744 shsurf->force_configure = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001745 }
1746}
1747
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001748static int launch_desktop_shell_process(struct wl_shell *shell);
1749
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001750static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001751desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001752{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001753 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001754 struct wl_shell *shell =
1755 container_of(process, struct wl_shell, child.process);
1756
1757 shell->child.process.pid = 0;
1758 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001759
1760 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1761 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001762 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001763 shell->child.deathstamp = time;
1764 shell->child.deathcount = 0;
1765 }
1766
1767 shell->child.deathcount++;
1768 if (shell->child.deathcount > 5) {
1769 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1770 return;
1771 }
1772
1773 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1774 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001775}
1776
1777static int
1778launch_desktop_shell_process(struct wl_shell *shell)
1779{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001780 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001781
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001782 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001783 &shell->child.process,
1784 shell_exe,
1785 desktop_shell_sigchld);
1786
1787 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001788 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001789 return 0;
1790}
1791
1792static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001793bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1794{
1795 struct wl_shell *shell = data;
1796
1797 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001798 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001799}
1800
Kristian Høgsberg75840622011-09-06 13:48:16 -04001801static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001802unbind_desktop_shell(struct wl_resource *resource)
1803{
1804 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001805
1806 if (shell->locked)
1807 resume_desktop(shell);
1808
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001809 shell->child.desktop_shell = NULL;
1810 shell->prepare_event_sent = false;
1811 free(resource);
1812}
1813
1814static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001815bind_desktop_shell(struct wl_client *client,
1816 void *data, uint32_t version, uint32_t id)
1817{
1818 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001819 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001820
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001821 resource = wl_client_add_object(client, &desktop_shell_interface,
1822 &desktop_shell_implementation,
1823 id, shell);
1824
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001825 if (client == shell->child.client) {
1826 resource->destroy = unbind_desktop_shell;
1827 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001828 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001829 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001830
1831 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1832 "permission to bind desktop_shell denied");
1833 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001834}
1835
Pekka Paalanen6e168112011-11-24 11:34:05 +02001836static void
1837screensaver_set_surface(struct wl_client *client,
1838 struct wl_resource *resource,
1839 struct wl_resource *shell_surface_resource,
1840 struct wl_resource *output_resource)
1841{
1842 struct wl_shell *shell = resource->data;
1843 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001844 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001845
Pekka Paalanen98262232011-12-01 10:42:22 +02001846 if (reset_shell_surface_type(surface))
1847 return;
1848
Pekka Paalanen77346a62011-11-30 16:26:35 +02001849 surface->type = SHELL_SURFACE_SCREENSAVER;
1850
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001851 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001852 surface->output = output;
1853 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001854}
1855
1856static const struct screensaver_interface screensaver_implementation = {
1857 screensaver_set_surface
1858};
1859
1860static void
1861unbind_screensaver(struct wl_resource *resource)
1862{
1863 struct wl_shell *shell = resource->data;
1864
Pekka Paalanen77346a62011-11-30 16:26:35 +02001865 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001866 free(resource);
1867}
1868
1869static void
1870bind_screensaver(struct wl_client *client,
1871 void *data, uint32_t version, uint32_t id)
1872{
1873 struct wl_shell *shell = data;
1874 struct wl_resource *resource;
1875
1876 resource = wl_client_add_object(client, &screensaver_interface,
1877 &screensaver_implementation,
1878 id, shell);
1879
Pekka Paalanen77346a62011-11-30 16:26:35 +02001880 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001881 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001882 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001883 return;
1884 }
1885
1886 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1887 "interface object already bound");
1888 wl_resource_destroy(resource, 0);
1889}
1890
Kristian Høgsberg07045392012-02-19 18:52:44 -05001891struct switcher {
1892 struct weston_compositor *compositor;
1893 struct weston_surface *current;
1894 struct wl_listener listener;
1895 struct wl_keyboard_grab grab;
1896};
1897
1898static void
1899switcher_next(struct switcher *switcher)
1900{
1901 struct weston_compositor *compositor = switcher->compositor;
1902 struct weston_surface *surface;
1903 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
1904
1905 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05001906 switch (get_shell_surface_type(surface)) {
1907 case SHELL_SURFACE_TOPLEVEL:
1908 case SHELL_SURFACE_FULLSCREEN:
1909 case SHELL_SURFACE_MAXIMIZED:
1910 if (first == NULL)
1911 first = surface;
1912 if (prev == switcher->current)
1913 next = surface;
1914 prev = surface;
1915 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05001916 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05001917 weston_surface_damage(surface);
1918 break;
1919 default:
1920 break;
1921 }
1922 }
1923
1924 if (next == NULL)
1925 next = first;
1926
Alex Wu07b26062012-03-12 16:06:01 +08001927 if (next == NULL)
1928 return;
1929
Kristian Høgsberg07045392012-02-19 18:52:44 -05001930 wl_list_remove(&switcher->listener.link);
1931 wl_list_insert(next->surface.resource.destroy_listener_list.prev,
1932 &switcher->listener.link);
1933
1934 switcher->current = next;
1935 next->alpha = 255;
1936}
1937
1938static void
1939switcher_handle_surface_destroy(struct wl_listener *listener,
1940 struct wl_resource *resource, uint32_t time)
1941{
1942 struct switcher *switcher =
1943 container_of(listener, struct switcher, listener);
1944
1945 switcher_next(switcher);
1946}
1947
1948static void
1949switcher_destroy(struct switcher *switcher, uint32_t time)
1950{
1951 struct weston_compositor *compositor = switcher->compositor;
1952 struct weston_surface *surface;
1953 struct weston_input_device *device =
1954 (struct weston_input_device *) switcher->grab.input_device;
1955
1956 wl_list_for_each(surface, &compositor->surface_list, link) {
1957 surface->alpha = 255;
1958 weston_surface_damage(surface);
1959 }
1960
Alex Wu07b26062012-03-12 16:06:01 +08001961 if (switcher->current)
1962 activate(compositor->shell, switcher->current, device, time);
Kristian Høgsberg07045392012-02-19 18:52:44 -05001963 wl_list_remove(&switcher->listener.link);
1964 wl_input_device_end_keyboard_grab(&device->input_device, time);
1965 free(switcher);
1966}
1967
1968static void
1969switcher_key(struct wl_keyboard_grab *grab,
1970 uint32_t time, uint32_t key, int32_t state)
1971{
1972 struct switcher *switcher = container_of(grab, struct switcher, grab);
1973 struct weston_input_device *device =
1974 (struct weston_input_device *) grab->input_device;
1975
1976 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
1977 switcher_destroy(switcher, time);
1978 } else if (key == KEY_TAB && state) {
1979 switcher_next(switcher);
1980 }
1981};
1982
1983static const struct wl_keyboard_grab_interface switcher_grab = {
1984 switcher_key
1985};
1986
1987static void
1988switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001989 uint32_t key, uint32_t button, uint32_t axis,
1990 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05001991{
1992 struct weston_compositor *compositor = data;
1993 struct switcher *switcher;
1994
1995 switcher = malloc(sizeof *switcher);
1996 switcher->compositor = compositor;
1997 switcher->current = NULL;
1998 switcher->listener.func = switcher_handle_surface_destroy;
1999 wl_list_init(&switcher->listener.link);
2000
2001 switcher->grab.interface = &switcher_grab;
2002 wl_input_device_start_keyboard_grab(device, &switcher->grab, time);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05002003 wl_input_device_set_keyboard_focus(device, NULL,
2004 weston_compositor_get_time());
Kristian Høgsberg07045392012-02-19 18:52:44 -05002005 switcher_next(switcher);
2006}
2007
Pekka Paalanen3c647232011-12-22 13:43:43 +02002008static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002009backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002010 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002011{
2012 struct weston_compositor *compositor = data;
2013 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002014 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002015
2016 /* TODO: we're limiting to simple use cases, where we assume just
2017 * control on the primary display. We'd have to extend later if we
2018 * ever get support for setting backlights on random desktop LCD
2019 * panels though */
2020 output = get_default_output(compositor);
2021 if (!output)
2022 return;
2023
2024 if (!output->set_backlight)
2025 return;
2026
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002027 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2028 backlight_new = output->backlight_current - 25;
2029 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2030 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002031
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002032 if (backlight_new < 5)
2033 backlight_new = 5;
2034 if (backlight_new > 255)
2035 backlight_new = 255;
2036
2037 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002038 output->set_backlight(output, output->backlight_current);
2039}
2040
2041static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002042debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002043 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002044{
2045 struct weston_compositor *compositor = data;
2046 struct wl_shell *shell =
2047 container_of(compositor->shell, struct wl_shell, shell);
2048 struct weston_surface *surface;
2049
2050 if (shell->debug_repaint_surface) {
2051 weston_surface_destroy(shell->debug_repaint_surface);
2052 shell->debug_repaint_surface = NULL;
2053 } else {
2054 surface = weston_surface_create(compositor);
2055 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2056 weston_surface_configure(surface, 0, 0, 8192, 8192);
2057 wl_list_insert(&compositor->fade_layer.surface_list,
2058 &surface->layer_link);
2059 weston_surface_assign_output(surface);
2060 pixman_region32_init(&surface->input);
2061
2062 /* Here's the dirty little trick that makes the
2063 * repaint debugging work: we force an
2064 * update_transform first to update dependent state
2065 * and clear the geometry.dirty bit. Then we clear
2066 * the surface damage so it only gets repainted
2067 * piecewise as we repaint other things. */
2068
2069 weston_surface_update_transform(surface);
2070 pixman_region32_fini(&surface->damage);
2071 pixman_region32_init(&surface->damage);
2072 shell->debug_repaint_surface = surface;
2073 }
2074}
2075
2076static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05002077shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002078{
2079 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
2080
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002081 if (shell->child.client)
2082 wl_client_destroy(shell->child.client);
2083
Pekka Paalanen3c647232011-12-22 13:43:43 +02002084 free(shell->screensaver.path);
2085 free(shell);
2086}
2087
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002088int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002089shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002090
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002091WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002092shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002093{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002094 struct wl_shell *shell;
2095
2096 shell = malloc(sizeof *shell);
2097 if (shell == NULL)
2098 return -1;
2099
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002100 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002101 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002102 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002103 shell->shell.unlock = unlock;
Pekka Paalanen3c647232011-12-22 13:43:43 +02002104 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002105
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002106 wl_list_init(&shell->backgrounds);
2107 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002108 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002109
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002110 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2111 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2112 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2113 weston_layer_init(&shell->background_layer,
2114 &shell->toplevel_layer.link);
2115 wl_list_init(&shell->lock_layer.surface_list);
2116
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002117 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002118
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002119 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2120 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002121 return -1;
2122
Kristian Høgsberg75840622011-09-06 13:48:16 -04002123 if (wl_display_add_global(ec->wl_display,
2124 &desktop_shell_interface,
2125 shell, bind_desktop_shell) == NULL)
2126 return -1;
2127
Pekka Paalanen6e168112011-11-24 11:34:05 +02002128 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2129 shell, bind_screensaver) == NULL)
2130 return -1;
2131
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002132 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002133 if (launch_desktop_shell_process(shell) != 0)
2134 return -1;
2135
Scott Moreau6a3633d2012-03-20 08:47:59 -06002136 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, MODIFIER_SUPER,
2137 move_binding, shell);
2138 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, MODIFIER_SUPER,
2139 resize_binding, shell);
2140 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2141 MODIFIER_CTRL | MODIFIER_ALT,
2142 terminate_binding, ec);
2143 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2144 click_to_activate_binding, ec);
Scott Moreau1b45a792012-03-22 10:58:23 -06002145 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002146 MODIFIER_SUPER | MODIFIER_ALT,
2147 surface_opacity_binding, NULL);
2148 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreau1b45a792012-03-22 10:58:23 -06002149 MODIFIER_SUPER, zoom_binding, NULL);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002150 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002151 MODIFIER_SUPER | MODIFIER_ALT,
2152 rotate_binding, NULL);
2153 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, MODIFIER_SUPER,
2154 switcher_binding, ec);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002155
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002156 /* brightness */
Scott Moreau6a3633d2012-03-20 08:47:59 -06002157 weston_compositor_add_binding(ec, KEY_F9, 0, 0, MODIFIER_CTRL,
2158 backlight_binding, ec);
2159 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2160 backlight_binding, ec);
2161 weston_compositor_add_binding(ec, KEY_F10, 0, 0, MODIFIER_CTRL,
2162 backlight_binding, ec);
2163 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2164 backlight_binding, ec);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002165
Scott Moreau6a3633d2012-03-20 08:47:59 -06002166 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, MODIFIER_SUPER,
2167 debug_repaint_binding, ec);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002168
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002169 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002170
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002171 return 0;
2172}