blob: 5c4a5cde4c654d93a7627ebca2164e51f3bbdeb2 [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;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200129};
130
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500131struct weston_move_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700132 struct wl_pointer_grab grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500133 struct weston_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500134 int32_t dx, dy;
135};
136
Pekka Paalanen460099f2012-01-20 16:48:25 +0200137struct rotate_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700138 struct wl_pointer_grab grab;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200139 struct shell_surface *surface;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500140 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200141 struct {
142 int32_t x;
143 int32_t y;
144 } center;
145};
146
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500147static void
Alex Wu4539b082012-03-01 12:57:46 +0800148center_on_output(struct weston_surface *surface,
149 struct weston_output *output);
150
151static void
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200152shell_configuration(struct wl_shell *shell)
153{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200154 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200155 char *path = NULL;
156 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200157
158 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200159 { "path", CONFIG_KEY_STRING, &path },
160 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200161 };
162
163 struct config_section cs[] = {
164 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
165 };
166
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200167 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500168 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200169 free(config_file);
170
Pekka Paalanen7296e792011-12-07 16:22:00 +0200171 shell->screensaver.path = path;
172 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200173}
174
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200175static void
Scott Moreau447013d2012-02-18 05:05:29 -0700176noop_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500177 struct wl_surface *surface, int32_t x, int32_t y)
178{
179 grab->focus = NULL;
180}
181
182static void
Scott Moreau447013d2012-02-18 05:05:29 -0700183move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500184 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500185{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500186 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500187 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500188 struct weston_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500189
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500190 weston_surface_configure(es,
191 device->x + move->dx,
192 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200193 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500194}
195
196static void
Scott Moreau447013d2012-02-18 05:05:29 -0700197move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500198 uint32_t time, int32_t button, int32_t state)
199{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500200 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500201
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500202 if (device->button_count == 0 && state == 0) {
Scott Moreau447013d2012-02-18 05:05:29 -0700203 wl_input_device_end_pointer_grab(device, time);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500204 free(grab);
205 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500206}
207
Scott Moreau447013d2012-02-18 05:05:29 -0700208static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500209 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500210 move_grab_motion,
211 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500212};
213
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400214static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500215weston_surface_move(struct weston_surface *es,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500216 struct weston_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500217{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500218 struct weston_move_grab *move;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500219
220 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400221 if (!move)
222 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500223
224 move->grab.interface = &move_grab_interface;
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200225 move->dx = es->geometry.x - wd->input_device.grab_x;
226 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500227 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500228
Scott Moreau447013d2012-02-18 05:05:29 -0700229 wl_input_device_start_pointer_grab(&wd->input_device, &move->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500230
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200231 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400232
233 return 0;
234}
235
236static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200237shell_surface_move(struct wl_client *client, struct wl_resource *resource,
238 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400239{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500240 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200241 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400242
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500243 if (wd->input_device.button_count == 0 ||
244 wd->input_device.grab_time != time ||
245 wd->input_device.pointer_focus != &shsurf->surface->surface)
246 return;
247
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500248 if (weston_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400249 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500250}
251
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500252struct weston_resize_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700253 struct wl_pointer_grab grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500254 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200255 int32_t width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200256 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500257};
258
259static void
Scott Moreau447013d2012-02-18 05:05:29 -0700260resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500261 uint32_t time, int32_t x, int32_t y)
262{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500263 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500264 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500265 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200266 int32_t from_x, from_y;
267 int32_t to_x, to_y;
268
269 weston_surface_from_global(resize->shsurf->surface,
270 device->grab_x, device->grab_y,
271 &from_x, &from_y);
272 weston_surface_from_global(resize->shsurf->surface,
273 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500274
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200275 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200276 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200277 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200278 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500279 } else {
280 width = resize->width;
281 }
282
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200283 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200284 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200285 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200286 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500287 } else {
288 height = resize->height;
289 }
290
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500291 wl_shell_surface_send_configure(&resize->shsurf->resource,
292 time, resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500293}
294
295static void
Scott Moreau447013d2012-02-18 05:05:29 -0700296resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500297 uint32_t time, int32_t button, int32_t state)
298{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500299 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500300
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500301 if (device->button_count == 0 && state == 0) {
Scott Moreau447013d2012-02-18 05:05:29 -0700302 wl_input_device_end_pointer_grab(device, time);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500303 free(grab);
304 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500305}
306
Scott Moreau447013d2012-02-18 05:05:29 -0700307static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500308 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500309 resize_grab_motion,
310 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500311};
312
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400313static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500314weston_surface_resize(struct shell_surface *shsurf,
315 struct weston_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200316 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500317{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500318 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500319
Alex Wu4539b082012-03-01 12:57:46 +0800320 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
321 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500322
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200323 if (edges == 0 || edges > 15 ||
324 (edges & 3) == 3 || (edges & 12) == 12)
325 return 0;
326
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500327 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400328 if (!resize)
329 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500330
331 resize->grab.interface = &resize_grab_interface;
332 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200333 resize->width = shsurf->surface->geometry.width;
334 resize->height = shsurf->surface->geometry.height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200335 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400336
Scott Moreau447013d2012-02-18 05:05:29 -0700337 wl_input_device_start_pointer_grab(&wd->input_device, &resize->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500338
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200339 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400340
341 return 0;
342}
343
344static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200345shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
346 struct wl_resource *input_resource, uint32_t time,
347 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400348{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500349 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200350 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400351
Alex Wu4539b082012-03-01 12:57:46 +0800352 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
353 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400354
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500355 if (wd->input_device.button_count == 0 ||
356 wd->input_device.grab_time != time ||
357 wd->input_device.pointer_focus != &shsurf->surface->surface)
358 return;
359
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500360 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400361 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500362}
363
Juan Zhao96879df2012-02-07 08:45:41 +0800364static struct weston_output *
365get_default_output(struct weston_compositor *compositor)
366{
367 return container_of(compositor->output_list.next,
368 struct weston_output, link);
369}
370
Alex Wu4539b082012-03-01 12:57:46 +0800371static void
372shell_unset_fullscreen(struct shell_surface *shsurf)
373{
374 /* undo all fullscreen things here */
375 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
376 shsurf->fullscreen.framerate = 0;
377 wl_list_remove(&shsurf->fullscreen.transform.link);
378 wl_list_init(&shsurf->fullscreen.transform.link);
379 weston_surface_destroy(shsurf->fullscreen.black_surface);
380 shsurf->fullscreen.black_surface = NULL;
381 shsurf->fullscreen_output = NULL;
382 shsurf->surface->force_configure = 1;
383 weston_surface_set_position(shsurf->surface,
384 shsurf->saved_x, shsurf->saved_y);
385}
386
Pekka Paalanen98262232011-12-01 10:42:22 +0200387static int
388reset_shell_surface_type(struct shell_surface *surface)
389{
390 switch (surface->type) {
391 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800392 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200393 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800394 case SHELL_SURFACE_MAXIMIZED:
395 surface->output = get_default_output(surface->surface->compositor);
396 weston_surface_set_position(surface->surface,
397 surface->saved_x,
398 surface->saved_y);
399 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200400 case SHELL_SURFACE_PANEL:
401 case SHELL_SURFACE_BACKGROUND:
402 wl_list_remove(&surface->link);
403 wl_list_init(&surface->link);
404 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200405 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200406 case SHELL_SURFACE_LOCK:
407 wl_resource_post_error(&surface->resource,
408 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200409 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200410 return -1;
411 case SHELL_SURFACE_NONE:
412 case SHELL_SURFACE_TOPLEVEL:
413 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500414 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200415 break;
416 }
417
418 surface->type = SHELL_SURFACE_NONE;
419 return 0;
420}
421
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500422static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200423shell_surface_set_toplevel(struct wl_client *client,
424 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400425
426{
Pekka Paalanen98262232011-12-01 10:42:22 +0200427 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400428
Pekka Paalanen98262232011-12-01 10:42:22 +0200429 if (reset_shell_surface_type(surface))
430 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400431
Pekka Paalanen98262232011-12-01 10:42:22 +0200432 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400433}
434
435static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200436shell_surface_set_transient(struct wl_client *client,
437 struct wl_resource *resource,
438 struct wl_resource *parent_resource,
439 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400440{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200441 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500442 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200443 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500444 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400445
Pekka Paalanen98262232011-12-01 10:42:22 +0200446 if (reset_shell_surface_type(shsurf))
447 return;
448
Alex Wu4539b082012-03-01 12:57:46 +0800449 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800450 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200451 weston_surface_set_position(es, pes->geometry.x + x,
452 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400453
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200454 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400455}
456
Juan Zhao96879df2012-02-07 08:45:41 +0800457static struct wl_shell *
458shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200459{
Juan Zhao96879df2012-02-07 08:45:41 +0800460 struct weston_surface *es = shsurf->surface;
461 struct weston_shell *shell = es->compositor->shell;
462
463 return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
464}
465
466static int
467get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
468{
469 struct shell_surface *priv;
470 int panel_height = 0;
471
472 if (!output)
473 return 0;
474
475 wl_list_for_each(priv, &wlshell->panels, link) {
476 if (priv->output == output) {
477 panel_height = priv->surface->geometry.height;
478 break;
479 }
480 }
481 return panel_height;
482}
483
484static void
485shell_surface_set_maximized(struct wl_client *client,
486 struct wl_resource *resource,
487 struct wl_resource *output_resource )
488{
489 struct shell_surface *shsurf = resource->data;
490 struct weston_surface *es = shsurf->surface;
491 struct wl_shell *wlshell = NULL;
492 uint32_t edges = 0, panel_height = 0;
493
494 /* get the default output, if the client set it as NULL
495 check whether the ouput is available */
496 if (output_resource)
497 shsurf->output = output_resource->data;
498 else
499 shsurf->output = get_default_output(es->compositor);
500
501 if (reset_shell_surface_type(shsurf))
502 return;
503
504 shsurf->saved_x = es->geometry.x;
505 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800506 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800507
508 wlshell = shell_surface_get_shell(shsurf);
509 panel_height = get_output_panel_height(wlshell, es->output);
510 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500511
512 wl_shell_surface_send_configure(&shsurf->resource,
513 weston_compositor_get_time(), edges,
514 es->output->current->width,
515 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800516
517 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200518}
519
Alex Wu4539b082012-03-01 12:57:46 +0800520static struct weston_surface *
521create_black_surface(struct weston_compositor *ec,
522 GLfloat x, GLfloat y, int w, int h)
523{
524 struct weston_surface *surface = NULL;
525
526 surface = weston_surface_create(ec);
527 if (surface == NULL) {
528 fprintf(stderr, "no memory\n");
529 return NULL;
530 }
531
532 weston_surface_configure(surface, x, y, w, h);
533 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
534 return surface;
535}
536
537/* Create black surface and append it to the associated fullscreen surface.
538 * Handle size dismatch and positioning according to the method. */
539static void
540shell_configure_fullscreen(struct shell_surface *shsurf)
541{
542 struct weston_output *output = shsurf->fullscreen_output;
543 struct weston_surface *surface = shsurf->surface;
544 struct weston_matrix *matrix;
545 float scale;
546
547 center_on_output(surface, output);
548
549 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500550 shsurf->fullscreen.black_surface =
551 create_black_surface(surface->compositor,
552 output->x, output->y,
553 output->current->width,
554 output->current->height);
555
556 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
557 wl_list_insert(&surface->layer_link,
558 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800559 shsurf->fullscreen.black_surface->output = output;
560
561 switch (shsurf->fullscreen.type) {
562 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
563 break;
564 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
565 matrix = &shsurf->fullscreen.transform.matrix;
566 weston_matrix_init(matrix);
567 scale = (float)output->current->width/(float)surface->geometry.width;
568 weston_matrix_scale(matrix, scale, scale, 1);
569 wl_list_remove(&shsurf->fullscreen.transform.link);
570 wl_list_insert(surface->geometry.transformation_list.prev,
571 &shsurf->fullscreen.transform.link);
572 weston_surface_set_position(surface, output->x, output->y);
573 break;
574 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
575 break;
576 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
577 break;
578 default:
579 break;
580 }
581}
582
583/* make the fullscreen and black surface at the top */
584static void
585shell_stack_fullscreen(struct shell_surface *shsurf)
586{
587 struct weston_surface *surface = shsurf->surface;
588 struct wl_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800589
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500590 wl_list_remove(&surface->layer_link);
591 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800592
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500593 wl_list_insert(&shell->fullscreen_layer.surface_list,
594 &surface->layer_link);
595 wl_list_insert(&surface->layer_link,
596 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800597
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500598 weston_surface_damage(surface);
599 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800600}
601
602static void
603shell_map_fullscreen(struct shell_surface *shsurf)
604{
605 shell_configure_fullscreen(shsurf);
606 shell_stack_fullscreen(shsurf);
607}
608
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400609static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200610shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500611 struct wl_resource *resource,
612 uint32_t method,
613 uint32_t framerate,
614 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400615{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200616 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500617 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800618
619 if (output_resource)
620 shsurf->output = output_resource->data;
621 else
622 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400623
Pekka Paalanen98262232011-12-01 10:42:22 +0200624 if (reset_shell_surface_type(shsurf))
625 return;
626
Alex Wu4539b082012-03-01 12:57:46 +0800627 shsurf->fullscreen_output = shsurf->output;
628 shsurf->fullscreen.type = method;
629 shsurf->fullscreen.framerate = framerate;
630 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400631
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200632 shsurf->saved_x = es->geometry.x;
633 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800634 shsurf->saved_position_valid = true;
635
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300636 if (weston_surface_is_mapped(es))
Alex Wu4539b082012-03-01 12:57:46 +0800637 shsurf->surface->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500638
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500639 wl_shell_surface_send_configure(&shsurf->resource,
640 weston_compositor_get_time(), 0,
641 shsurf->output->current->width,
642 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400643}
644
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500645static void
Scott Moreau447013d2012-02-18 05:05:29 -0700646popup_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500647 struct wl_surface *surface, int32_t x, int32_t y)
648{
649 struct wl_input_device *device = grab->input_device;
650 struct shell_surface *priv =
651 container_of(grab, struct shell_surface, popup.grab);
652 struct wl_client *client = priv->surface->surface.resource.client;
653
Pekka Paalanencb108432012-01-19 16:25:40 +0200654 if (surface && surface->resource.client == client) {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200655 wl_input_device_set_pointer_focus(device, surface, time, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500656 grab->focus = surface;
657 } else {
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200658 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500659 grab->focus = NULL;
660 }
661}
662
663static void
Scott Moreau447013d2012-02-18 05:05:29 -0700664popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200665 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500666{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500667 struct wl_resource *resource;
668
669 resource = grab->input_device->pointer_focus_resource;
670 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500671 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500672}
673
674static void
Scott Moreau447013d2012-02-18 05:05:29 -0700675popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500676 uint32_t time, int32_t button, int32_t state)
677{
678 struct wl_resource *resource;
679 struct shell_surface *shsurf =
680 container_of(grab, struct shell_surface, popup.grab);
681
682 resource = grab->input_device->pointer_focus_resource;
683 if (resource) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500684 wl_input_device_send_button(resource, time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500685 } else if (state == 0 &&
686 (shsurf->popup.initial_up ||
687 time - shsurf->popup.time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500688 wl_shell_surface_send_popup_done(&shsurf->resource);
Scott Moreau447013d2012-02-18 05:05:29 -0700689 wl_input_device_end_pointer_grab(grab->input_device, time);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500690 shsurf->popup.grab.input_device = NULL;
691 }
692
693 if (state == 0)
694 shsurf->popup.initial_up = 1;
695}
696
Scott Moreau447013d2012-02-18 05:05:29 -0700697static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500698 popup_grab_focus,
699 popup_grab_motion,
700 popup_grab_button,
701};
702
703static void
704shell_map_popup(struct shell_surface *shsurf, uint32_t time)
705{
706 struct wl_input_device *device;
707 struct weston_surface *es = shsurf->surface;
708 struct weston_surface *parent = shsurf->parent->surface;
709
710 es->output = parent->output;
711
712 shsurf->popup.grab.interface = &popup_grab_interface;
713 device = es->compositor->input_device;
714
Pekka Paalanen938269a2012-02-07 14:19:01 +0200715 weston_surface_update_transform(parent);
716 if (parent->transform.enabled) {
717 shsurf->popup.parent_transform.matrix =
718 parent->transform.matrix;
719 } else {
720 /* construct x, y translation matrix */
721 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
722 shsurf->popup.parent_transform.matrix.d[12] =
723 parent->geometry.x;
724 shsurf->popup.parent_transform.matrix.d[13] =
725 parent->geometry.y;
726 }
727 wl_list_insert(es->geometry.transformation_list.prev,
728 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200729 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500730
731 shsurf->popup.grab.input_device = device;
732 shsurf->popup.time = device->grab_time;
733 shsurf->popup.initial_up = 0;
734
Scott Moreau447013d2012-02-18 05:05:29 -0700735 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500736 &shsurf->popup.grab, shsurf->popup.time);
737}
738
739static void
740shell_surface_set_popup(struct wl_client *client,
741 struct wl_resource *resource,
742 struct wl_resource *input_device_resource,
743 uint32_t time,
744 struct wl_resource *parent_resource,
745 int32_t x, int32_t y, uint32_t flags)
746{
747 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500748
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500749 shsurf->type = SHELL_SURFACE_POPUP;
750 shsurf->parent = parent_resource->data;
751 shsurf->popup.x = x;
752 shsurf->popup.y = y;
753}
754
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200755static const struct wl_shell_surface_interface shell_surface_implementation = {
756 shell_surface_move,
757 shell_surface_resize,
758 shell_surface_set_toplevel,
759 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500760 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +0800761 shell_surface_set_popup,
762 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200763};
764
765static void
766destroy_shell_surface(struct wl_resource *resource)
767{
768 struct shell_surface *shsurf = resource->data;
769
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500770 if (shsurf->popup.grab.input_device)
Scott Moreau447013d2012-02-18 05:05:29 -0700771 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500772
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200773 /* in case cleaning up a dead client destroys shell_surface first */
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300774 if (shsurf->surface) {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200775 wl_list_remove(&shsurf->surface_destroy_listener.link);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300776 shsurf->surface->configure = NULL;
777 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200778
Alex Wuaa08e2d2012-03-05 11:01:40 +0800779 if (shsurf->fullscreen.black_surface)
780 weston_surface_destroy(shsurf->fullscreen.black_surface);
781
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200782 wl_list_remove(&shsurf->link);
783 free(shsurf);
784}
785
786static void
787shell_handle_surface_destroy(struct wl_listener *listener,
788 struct wl_resource *resource, uint32_t time)
789{
790 struct shell_surface *shsurf = container_of(listener,
791 struct shell_surface,
792 surface_destroy_listener);
793
794 shsurf->surface = NULL;
795 wl_resource_destroy(&shsurf->resource, time);
796}
797
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200798static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500799get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200800{
801 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
802 struct wl_listener *listener;
803
804 /* search the destroy listener list for our callback */
805 wl_list_for_each(listener, lst, link) {
806 if (listener->func == shell_handle_surface_destroy) {
807 return container_of(listener, struct shell_surface,
808 surface_destroy_listener);
809 }
810 }
811
812 return NULL;
813}
814
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200815static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300816shell_surface_configure(struct weston_surface *, int32_t, int32_t);
817
818static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200819shell_get_shell_surface(struct wl_client *client,
820 struct wl_resource *resource,
821 uint32_t id,
822 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200823{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500824 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200825 struct shell_surface *shsurf;
826
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200827 if (get_shell_surface(surface)) {
828 wl_resource_post_error(surface_resource,
829 WL_DISPLAY_ERROR_INVALID_OBJECT,
830 "wl_shell::get_shell_surface already requested");
831 return;
832 }
833
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300834 if (surface->configure) {
835 wl_resource_post_error(surface_resource,
836 WL_DISPLAY_ERROR_INVALID_OBJECT,
837 "surface->configure already set");
838 return;
839 }
840
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200841 shsurf = calloc(1, sizeof *shsurf);
842 if (!shsurf) {
843 wl_resource_post_no_memory(resource);
844 return;
845 }
846
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300847 surface->configure = shell_surface_configure;
848
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200849 shsurf->resource.destroy = destroy_shell_surface;
850 shsurf->resource.object.id = id;
851 shsurf->resource.object.interface = &wl_shell_surface_interface;
852 shsurf->resource.object.implementation =
853 (void (**)(void)) &shell_surface_implementation;
854 shsurf->resource.data = shsurf;
855
Alex Wu4539b082012-03-01 12:57:46 +0800856 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200857 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +0800858 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
859 shsurf->fullscreen.framerate = 0;
860 shsurf->fullscreen.black_surface = NULL;
861 wl_list_init(&shsurf->fullscreen.transform.link);
862
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200863 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
864 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
865 &shsurf->surface_destroy_listener.link);
866
867 /* init link so its safe to always remove it in destroy_shell_surface */
868 wl_list_init(&shsurf->link);
869
Pekka Paalanen460099f2012-01-20 16:48:25 +0200870 /* empty when not in use */
871 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500872 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +0200873
Pekka Paalanen98262232011-12-01 10:42:22 +0200874 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200875
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200876 wl_client_add_resource(client, &shsurf->resource);
877}
878
879static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200880 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500881};
882
Kristian Høgsberg07937562011-04-12 17:25:42 -0400883static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500884handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200885{
886 proc->pid = 0;
887}
888
889static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200890launch_screensaver(struct wl_shell *shell)
891{
892 if (shell->screensaver.binding)
893 return;
894
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200895 if (!shell->screensaver.path)
896 return;
897
Kristian Høgsberg32bed572012-03-01 17:11:36 -0500898 if (shell->screensaver.process.pid != 0) {
899 fprintf(stderr, "old screensaver still running\n");
900 return;
901 }
902
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500903 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200904 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200905 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200906 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200907}
908
909static void
910terminate_screensaver(struct wl_shell *shell)
911{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200912 if (shell->screensaver.process.pid == 0)
913 return;
914
915 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200916}
917
918static void
919show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
920{
921 struct wl_list *list;
922
923 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500924 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200925 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500926 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200927
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500928 wl_list_remove(&surface->surface->layer_link);
929 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200930 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +0200931 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200932}
933
934static void
935hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
936{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500937 wl_list_remove(&surface->surface->layer_link);
938 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200939 surface->surface->output = NULL;
940}
941
942static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400943desktop_shell_set_background(struct wl_client *client,
944 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100945 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400946 struct wl_resource *surface_resource)
947{
948 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200949 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500950 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200951 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400952
Pekka Paalanen98262232011-12-01 10:42:22 +0200953 if (reset_shell_surface_type(shsurf))
954 return;
955
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100956 wl_list_for_each(priv, &shell->backgrounds, link) {
957 if (priv->output == output_resource->data) {
958 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500959 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100960 wl_list_remove(&priv->link);
961 break;
962 }
963 }
964
Pekka Paalanen068ae942011-11-28 14:11:15 +0200965 shsurf->type = SHELL_SURFACE_BACKGROUND;
966 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100967
Pekka Paalanen068ae942011-11-28 14:11:15 +0200968 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100969
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200970 weston_surface_set_position(surface, shsurf->output->x,
971 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200972
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500973 desktop_shell_send_configure(resource,
974 weston_compositor_get_time(), 0,
975 surface_resource,
976 shsurf->output->current->width,
977 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400978}
979
980static void
981desktop_shell_set_panel(struct wl_client *client,
982 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100983 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400984 struct wl_resource *surface_resource)
985{
986 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200987 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500988 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200989 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400990
Pekka Paalanen98262232011-12-01 10:42:22 +0200991 if (reset_shell_surface_type(shsurf))
992 return;
993
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100994 wl_list_for_each(priv, &shell->panels, link) {
995 if (priv->output == output_resource->data) {
996 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500997 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100998 wl_list_remove(&priv->link);
999 break;
1000 }
1001 }
1002
Pekka Paalanen068ae942011-11-28 14:11:15 +02001003 shsurf->type = SHELL_SURFACE_PANEL;
1004 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001005
Pekka Paalanen068ae942011-11-28 14:11:15 +02001006 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001007
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001008 weston_surface_set_position(surface, shsurf->output->x,
1009 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001010
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001011 desktop_shell_send_configure(resource,
1012 weston_compositor_get_time(), 0,
1013 surface_resource,
1014 shsurf->output->current->width,
1015 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001016}
1017
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001018static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001019handle_lock_surface_destroy(struct wl_listener *listener,
1020 struct wl_resource *resource, uint32_t time)
1021{
1022 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +02001023 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001024
1025 fprintf(stderr, "lock surface gone\n");
1026 shell->lock_surface = NULL;
1027}
1028
1029static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001030desktop_shell_set_lock_surface(struct wl_client *client,
1031 struct wl_resource *resource,
1032 struct wl_resource *surface_resource)
1033{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001034 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001035 struct shell_surface *surface = surface_resource->data;
1036
1037 if (reset_shell_surface_type(surface))
1038 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001039
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001040 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001041
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001042 if (!shell->locked)
1043 return;
1044
Pekka Paalanen98262232011-12-01 10:42:22 +02001045 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001046
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001047 shell->lock_surface_listener.func = handle_lock_surface_destroy;
1048 wl_list_insert(&surface_resource->destroy_listener_list,
1049 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001050
Pekka Paalanen068ae942011-11-28 14:11:15 +02001051 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001052}
1053
1054static void
1055resume_desktop(struct wl_shell *shell)
1056{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001057 struct shell_surface *tmp;
1058
1059 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1060 hide_screensaver(shell, tmp);
1061
1062 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001063
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001064 wl_list_remove(&shell->lock_layer.link);
1065 wl_list_insert(&shell->compositor->cursor_layer.link,
1066 &shell->fullscreen_layer.link);
1067 wl_list_insert(&shell->fullscreen_layer.link,
1068 &shell->panel_layer.link);
1069 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001070
1071 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001072 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001073 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001074 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001075}
1076
1077static void
1078desktop_shell_unlock(struct wl_client *client,
1079 struct wl_resource *resource)
1080{
1081 struct wl_shell *shell = resource->data;
1082
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001083 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001084
1085 if (shell->locked)
1086 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001087}
1088
Kristian Høgsberg75840622011-09-06 13:48:16 -04001089static const struct desktop_shell_interface desktop_shell_implementation = {
1090 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001091 desktop_shell_set_panel,
1092 desktop_shell_set_lock_surface,
1093 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001094};
1095
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001096static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001097get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001098{
1099 struct shell_surface *shsurf;
1100
1101 shsurf = get_shell_surface(surface);
1102 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001103 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001104 return shsurf->type;
1105}
1106
Kristian Høgsberg75840622011-09-06 13:48:16 -04001107static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001108move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001109 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001110{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001111 struct weston_surface *surface =
1112 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001113
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001114 if (surface == NULL)
1115 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001116
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001117 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001118 case SHELL_SURFACE_PANEL:
1119 case SHELL_SURFACE_BACKGROUND:
1120 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001121 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001122 return;
1123 default:
1124 break;
1125 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001126
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001127 weston_surface_move(surface, (struct weston_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001128}
1129
1130static void
1131resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001132 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001133{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001134 struct weston_surface *surface =
1135 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001136 uint32_t edges = 0;
1137 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001138 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001139
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001140 if (surface == NULL)
1141 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001142
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001143 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001144 if (!shsurf)
1145 return;
1146
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001147 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001148 case SHELL_SURFACE_PANEL:
1149 case SHELL_SURFACE_BACKGROUND:
1150 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001151 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001152 return;
1153 default:
1154 break;
1155 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001156
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001157 weston_surface_from_global(surface,
1158 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001159
Pekka Paalanen60921e52012-01-25 15:55:43 +02001160 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001161 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001162 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001163 edges |= 0;
1164 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001165 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001166
Pekka Paalanen60921e52012-01-25 15:55:43 +02001167 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001168 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001169 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001170 edges |= 0;
1171 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001172 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001173
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001174 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001175 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001176}
1177
1178static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001179surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1180 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1181{
1182 int step = 15;
1183 struct shell_surface *shsurf;
1184 struct weston_surface *surface =
1185 (struct weston_surface *) device->pointer_focus;
1186
1187 if (surface == NULL)
1188 return;
1189
1190 shsurf = get_shell_surface(surface);
1191 if (!shsurf)
1192 return;
1193
1194 switch (shsurf->type) {
1195 case SHELL_SURFACE_BACKGROUND:
1196 case SHELL_SURFACE_SCREENSAVER:
1197 return;
1198 default:
1199 break;
1200 }
1201
1202 surface->alpha += value * step;
1203
1204 if (surface->alpha > 255)
1205 surface->alpha = 255;
1206 if (surface->alpha < step)
1207 surface->alpha = step;
1208
1209 surface->geometry.dirty = 1;
1210 weston_surface_damage(surface);
1211}
1212
1213static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001214zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001215 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001216{
1217 struct weston_input_device *wd = (struct weston_input_device *) device;
1218 struct weston_compositor *compositor = wd->compositor;
1219 struct weston_output *output;
1220
1221 wl_list_for_each(output, &compositor->output_list, link) {
1222 if (pixman_region32_contains_point(&output->region,
1223 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001224 output->zoom.active = 1;
1225 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001226
1227 if (output->zoom.level >= 1.0) {
1228 output->zoom.active = 0;
1229 output->zoom.level = 1.0;
1230 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001231
1232 if (output->zoom.level < output->zoom.increment)
1233 output->zoom.level = output->zoom.increment;
1234
1235 weston_output_update_zoom(output, device->x, device->y);
1236 }
1237 }
1238}
1239
Scott Moreauccbf29d2012-02-22 14:21:41 -07001240static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001241terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001242 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001243{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001244 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001245
1246 if (state)
1247 wl_display_terminate(compositor->wl_display);
1248}
1249
1250static void
Scott Moreau447013d2012-02-18 05:05:29 -07001251rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001252 uint32_t time, int32_t x, int32_t y)
1253{
1254 struct rotate_grab *rotate =
1255 container_of(grab, struct rotate_grab, grab);
1256 struct wl_input_device *device = grab->input_device;
1257 struct shell_surface *surface = rotate->surface;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001258 struct weston_surface *base_surface = surface->surface;
1259 GLfloat cx = 0.5f * base_surface->geometry.width;
1260 GLfloat cy = 0.5f * base_surface->geometry.height;
1261 GLfloat dx, dy, cposx, cposy, dposx, dposy;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001262 GLfloat r;
1263
1264 dx = device->x - rotate->center.x;
1265 dy = device->y - rotate->center.y;
1266 r = sqrtf(dx * dx + dy * dy);
1267
1268 wl_list_remove(&surface->rotation.transform.link);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001269 surface->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001270
1271 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001272 struct weston_matrix *matrix =
1273 &surface->rotation.transform.matrix;
1274
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001275 weston_matrix_init(&rotate->rotation);
1276 rotate->rotation.d[0] = dx / r;
1277 rotate->rotation.d[4] = -dy / r;
1278 rotate->rotation.d[1] = -rotate->rotation.d[4];
1279 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001280
1281 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001282 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001283 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1284 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001285 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001286
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001287 wl_list_insert(
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001288 &surface->surface->geometry.transformation_list,
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001289 &surface->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001290 } else {
1291 wl_list_init(&surface->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001292 weston_matrix_init(&surface->rotation.rotation);
1293 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001294 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001295
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001296 /* We need to adjust the position of the surface
1297 * in case it was resized in a rotated state before */
1298 cposx = base_surface->geometry.x + cx;
1299 cposy = base_surface->geometry.y + cy;
1300 dposx = rotate->center.x - cposx;
1301 dposy = rotate->center.y - cposy;
1302 if (dposx != 0.0f || dposy != 0.0f) {
1303 weston_surface_set_position(base_surface,
1304 base_surface->geometry.x + dposx,
1305 base_surface->geometry.y + dposy);
1306 }
1307
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001308 /* Repaint implies weston_surface_update_transform(), which
1309 * lazily applies the damage due to rotation update.
1310 */
1311 weston_compositor_schedule_repaint(surface->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001312}
1313
1314static void
Scott Moreau447013d2012-02-18 05:05:29 -07001315rotate_grab_button(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001316 uint32_t time, int32_t button, int32_t state)
1317{
1318 struct rotate_grab *rotate =
1319 container_of(grab, struct rotate_grab, grab);
1320 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001321 struct shell_surface *surface = rotate->surface;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001322
1323 if (device->button_count == 0 && state == 0) {
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001324 weston_matrix_multiply(&surface->rotation.rotation,
1325 &rotate->rotation);
Scott Moreau447013d2012-02-18 05:05:29 -07001326 wl_input_device_end_pointer_grab(device, time);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001327 free(rotate);
1328 }
1329}
1330
Scott Moreau447013d2012-02-18 05:05:29 -07001331static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001332 noop_grab_focus,
1333 rotate_grab_motion,
1334 rotate_grab_button,
1335};
1336
1337static void
1338rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001339 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001340{
1341 struct weston_surface *base_surface =
1342 (struct weston_surface *) device->pointer_focus;
1343 struct shell_surface *surface;
1344 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001345 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001346 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001347
1348 if (base_surface == NULL)
1349 return;
1350
1351 surface = get_shell_surface(base_surface);
1352 if (!surface)
1353 return;
1354
1355 switch (surface->type) {
1356 case SHELL_SURFACE_PANEL:
1357 case SHELL_SURFACE_BACKGROUND:
1358 case SHELL_SURFACE_FULLSCREEN:
1359 case SHELL_SURFACE_SCREENSAVER:
1360 return;
1361 default:
1362 break;
1363 }
1364
Pekka Paalanen460099f2012-01-20 16:48:25 +02001365 rotate = malloc(sizeof *rotate);
1366 if (!rotate)
1367 return;
1368
1369 rotate->grab.interface = &rotate_grab_interface;
1370 rotate->surface = surface;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001371
1372 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001373 surface->surface->geometry.width / 2,
1374 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001375 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001376
Scott Moreau447013d2012-02-18 05:05:29 -07001377 wl_input_device_start_pointer_grab(device, &rotate->grab, time);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001378
1379 dx = device->x - rotate->center.x;
1380 dy = device->y - rotate->center.y;
1381 r = sqrtf(dx * dx + dy * dy);
1382 if (r > 20.0f) {
1383 struct weston_matrix inverse;
1384
1385 weston_matrix_init(&inverse);
1386 inverse.d[0] = dx / r;
1387 inverse.d[4] = dy / r;
1388 inverse.d[1] = -inverse.d[4];
1389 inverse.d[5] = inverse.d[0];
1390 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001391
1392 weston_matrix_init(&rotate->rotation);
1393 rotate->rotation.d[0] = dx / r;
1394 rotate->rotation.d[4] = -dy / r;
1395 rotate->rotation.d[1] = -rotate->rotation.d[4];
1396 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001397 } else {
1398 weston_matrix_init(&surface->rotation.rotation);
1399 weston_matrix_init(&rotate->rotation);
1400 }
1401
Pekka Paalanenb29f4122012-02-14 14:59:18 +02001402 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001403}
1404
1405static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001406activate(struct weston_shell *base, struct weston_surface *es,
1407 struct weston_input_device *device, uint32_t time)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001408{
1409 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001410 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001411
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001412 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001413
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001414 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001415 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001416
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001417 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001418 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001419 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001420 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001421 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001422
Pekka Paalanen77346a62011-11-30 16:26:35 +02001423 case SHELL_SURFACE_SCREENSAVER:
1424 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001425 if (shell->lock_surface)
1426 weston_surface_restack(es,
1427 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001428 break;
Alex Wu4539b082012-03-01 12:57:46 +08001429 case SHELL_SURFACE_FULLSCREEN:
1430 /* should on top of panels */
1431 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001432 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001433 weston_surface_restack(es,
1434 &shell->toplevel_layer.surface_list);
1435 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001436 }
1437}
1438
1439static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001440click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001441 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001442 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001443{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001444 struct weston_input_device *wd = (struct weston_input_device *) device;
1445 struct weston_compositor *compositor = data;
1446 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001447 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001448
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001449 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001450 if (!focus)
1451 return;
1452
Alex Wu4539b082012-03-01 12:57:46 +08001453 upper = container_of(focus->link.prev, struct weston_surface, link);
1454 if (focus->link.prev != &compositor->surface_list &&
1455 get_shell_surface_type(upper) == SHELL_SURFACE_FULLSCREEN) {
1456 printf("%s: focus is black surface, raise its fullscreen surface\n", __func__);
1457 shell_stack_fullscreen(get_shell_surface(upper));
1458 focus = upper;
1459 }
1460
Alex Wu9c35e6b2012-03-05 14:13:13 +08001461 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001462 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001463}
1464
1465static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001466lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001467{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001468 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001469 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001470 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001471 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001472 uint32_t time;
1473
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001474 if (shell->locked) {
1475 wl_list_for_each(output, &shell->compositor->output_list, link)
1476 /* TODO: find a way to jump to other DPMS levels */
1477 if (output->set_dpms)
1478 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001479 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001480 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001481
1482 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001483
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001484 /* Hide all surfaces by removing the fullscreen, panel and
1485 * toplevel layers. This way nothing else can show or receive
1486 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001487
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001488 wl_list_remove(&shell->panel_layer.link);
1489 wl_list_remove(&shell->toplevel_layer.link);
1490 wl_list_remove(&shell->fullscreen_layer.link);
1491 wl_list_insert(&shell->compositor->cursor_layer.link,
1492 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001493
Pekka Paalanen77346a62011-11-30 16:26:35 +02001494 launch_screensaver(shell);
1495
1496 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1497 show_screensaver(shell, shsurf);
1498
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001499 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001500 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001501 weston_compositor_wake(shell->compositor);
1502 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001503 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001504
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001505 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001506 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001507
1508 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001509 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001510 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1511 wl_input_device_set_keyboard_focus(&device->input_device,
1512 NULL, time);
1513 }
1514
1515 /* TODO: disable bindings that should not work while locked. */
1516
1517 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001518}
1519
1520static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001521unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001522{
1523 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1524
Pekka Paalanend81c2162011-11-16 13:47:34 +02001525 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001526 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001527 return;
1528 }
1529
1530 /* If desktop-shell client has gone away, unlock immediately. */
1531 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001532 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001533 return;
1534 }
1535
1536 if (shell->prepare_event_sent)
1537 return;
1538
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001539 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001540 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001541}
1542
1543static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001544center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001545{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001546 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001547 GLfloat x = (mode->width - surface->geometry.width) / 2;
1548 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001549
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001550 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001551}
1552
1553static void
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001554map(struct weston_shell *base, struct weston_surface *surface,
1555 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001556{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001557 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001558 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001559 struct shell_surface *shsurf;
1560 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001561 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001562 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001563
Pekka Paalanen77346a62011-11-30 16:26:35 +02001564 shsurf = get_shell_surface(surface);
1565 if (shsurf)
1566 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001567
Pekka Paalanen60921e52012-01-25 15:55:43 +02001568 surface->geometry.width = width;
1569 surface->geometry.height = height;
1570 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001571
1572 /* initial positioning, see also configure() */
1573 switch (surface_type) {
1574 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001575 weston_surface_set_position(surface, 10 + random() % 400,
1576 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001577 break;
1578 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001579 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001580 break;
Alex Wu4539b082012-03-01 12:57:46 +08001581 case SHELL_SURFACE_FULLSCREEN:
1582 shell_map_fullscreen(shsurf);
1583 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001584 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001585 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001586 panel_height = get_output_panel_height(shell,surface->output);
1587 weston_surface_set_position(surface, surface->output->x,
1588 surface->output->y + panel_height);
1589 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001590 case SHELL_SURFACE_LOCK:
1591 center_on_output(surface, get_default_output(compositor));
1592 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001593 case SHELL_SURFACE_POPUP:
1594 shell_map_popup(shsurf, shsurf->popup.time);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001595 case SHELL_SURFACE_NONE:
1596 weston_surface_set_position(surface,
1597 surface->geometry.x + sx,
1598 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001599 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001600 default:
1601 ;
1602 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001603
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001604 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001605 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001606 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001607 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001608 wl_list_insert(&shell->background_layer.surface_list,
1609 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001610 break;
1611 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001612 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001613 wl_list_insert(&shell->panel_layer.surface_list,
1614 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001615 break;
1616 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001617 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001618 wl_list_insert(&shell->lock_layer.surface_list,
1619 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001620 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001621 break;
1622 case SHELL_SURFACE_SCREENSAVER:
1623 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001624 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001625 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001626 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001627 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001628 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001629 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001630 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001631 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001632 case SHELL_SURFACE_POPUP:
1633 case SHELL_SURFACE_TRANSIENT:
1634 parent = shsurf->parent->surface;
1635 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1636 break;
Alex Wu4539b082012-03-01 12:57:46 +08001637 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001638 case SHELL_SURFACE_NONE:
1639 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001640 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001641 wl_list_insert(&shell->toplevel_layer.surface_list,
1642 &surface->layer_link);
1643 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001644 }
1645
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001646 if (surface_type != SHELL_SURFACE_NONE) {
1647 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001648 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1649 surface->output = shsurf->output;
1650 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001651
Juan Zhao7bb92f02011-12-15 11:31:51 -05001652 switch (surface_type) {
1653 case SHELL_SURFACE_TOPLEVEL:
1654 case SHELL_SURFACE_TRANSIENT:
1655 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001656 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001657 if (!shell->locked)
1658 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001659 (struct weston_input_device *)
Alex Wu4539b082012-03-01 12:57:46 +08001660 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001661 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001662 break;
1663 default:
1664 break;
1665 }
1666
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001667 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001668 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001669}
1670
1671static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001672configure(struct weston_shell *base, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001673 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001674{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001675 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001676 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Alex Wu4539b082012-03-01 12:57:46 +08001677 enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001678 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001679
Pekka Paalanen77346a62011-11-30 16:26:35 +02001680 shsurf = get_shell_surface(surface);
1681 if (shsurf)
1682 surface_type = shsurf->type;
1683
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001684 surface->geometry.x = x;
1685 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001686 surface->geometry.width = width;
1687 surface->geometry.height = height;
1688 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001689
1690 switch (surface_type) {
1691 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001692 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001693 break;
Alex Wu4539b082012-03-01 12:57:46 +08001694 case SHELL_SURFACE_FULLSCREEN:
1695 shell_configure_fullscreen(shsurf);
1696 if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
1697 shell_stack_fullscreen(shsurf);
1698 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001699 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001700 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001701 surface->geometry.x = surface->output->x;
1702 surface->geometry.y = surface->output->y +
1703 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001704 break;
Alex Wu4539b082012-03-01 12:57:46 +08001705 case SHELL_SURFACE_TOPLEVEL:
1706 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001707 default:
1708 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001709 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001710
Alex Wu4539b082012-03-01 12:57:46 +08001711 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001712 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001713 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001714
1715 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1716 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001717 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1718 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001719 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001720}
1721
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001722static void
1723shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1724{
1725 struct weston_shell *shell = es->compositor->shell;
1726
1727 if (!weston_surface_is_mapped(es)) {
1728 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
1729 } else if (es->force_configure || sx != 0 || sy != 0 ||
1730 es->geometry.width != es->buffer->width ||
1731 es->geometry.height != es->buffer->height) {
1732 GLfloat from_x, from_y;
1733 GLfloat to_x, to_y;
1734
1735 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
1736 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
1737 configure(shell, es,
1738 es->geometry.x + to_x - from_x,
1739 es->geometry.y + to_y - from_y,
1740 es->buffer->width, es->buffer->height);
1741 es->force_configure = 0;
1742 }
1743}
1744
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001745static int launch_desktop_shell_process(struct wl_shell *shell);
1746
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001747static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001748desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001749{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001750 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001751 struct wl_shell *shell =
1752 container_of(process, struct wl_shell, child.process);
1753
1754 shell->child.process.pid = 0;
1755 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001756
1757 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1758 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001759 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001760 shell->child.deathstamp = time;
1761 shell->child.deathcount = 0;
1762 }
1763
1764 shell->child.deathcount++;
1765 if (shell->child.deathcount > 5) {
1766 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1767 return;
1768 }
1769
1770 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1771 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001772}
1773
1774static int
1775launch_desktop_shell_process(struct wl_shell *shell)
1776{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001777 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001778
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001779 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001780 &shell->child.process,
1781 shell_exe,
1782 desktop_shell_sigchld);
1783
1784 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001785 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001786 return 0;
1787}
1788
1789static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001790bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1791{
1792 struct wl_shell *shell = data;
1793
1794 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001795 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001796}
1797
Kristian Høgsberg75840622011-09-06 13:48:16 -04001798static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001799unbind_desktop_shell(struct wl_resource *resource)
1800{
1801 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001802
1803 if (shell->locked)
1804 resume_desktop(shell);
1805
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001806 shell->child.desktop_shell = NULL;
1807 shell->prepare_event_sent = false;
1808 free(resource);
1809}
1810
1811static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001812bind_desktop_shell(struct wl_client *client,
1813 void *data, uint32_t version, uint32_t id)
1814{
1815 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001816 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001817
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001818 resource = wl_client_add_object(client, &desktop_shell_interface,
1819 &desktop_shell_implementation,
1820 id, shell);
1821
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001822 if (client == shell->child.client) {
1823 resource->destroy = unbind_desktop_shell;
1824 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001825 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001826 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001827
1828 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1829 "permission to bind desktop_shell denied");
1830 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001831}
1832
Pekka Paalanen6e168112011-11-24 11:34:05 +02001833static void
1834screensaver_set_surface(struct wl_client *client,
1835 struct wl_resource *resource,
1836 struct wl_resource *shell_surface_resource,
1837 struct wl_resource *output_resource)
1838{
1839 struct wl_shell *shell = resource->data;
1840 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001841 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001842
Pekka Paalanen98262232011-12-01 10:42:22 +02001843 if (reset_shell_surface_type(surface))
1844 return;
1845
Pekka Paalanen77346a62011-11-30 16:26:35 +02001846 surface->type = SHELL_SURFACE_SCREENSAVER;
1847
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001848 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001849 surface->output = output;
1850 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001851}
1852
1853static const struct screensaver_interface screensaver_implementation = {
1854 screensaver_set_surface
1855};
1856
1857static void
1858unbind_screensaver(struct wl_resource *resource)
1859{
1860 struct wl_shell *shell = resource->data;
1861
Pekka Paalanen77346a62011-11-30 16:26:35 +02001862 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001863 free(resource);
1864}
1865
1866static void
1867bind_screensaver(struct wl_client *client,
1868 void *data, uint32_t version, uint32_t id)
1869{
1870 struct wl_shell *shell = data;
1871 struct wl_resource *resource;
1872
1873 resource = wl_client_add_object(client, &screensaver_interface,
1874 &screensaver_implementation,
1875 id, shell);
1876
Pekka Paalanen77346a62011-11-30 16:26:35 +02001877 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001878 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001879 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001880 return;
1881 }
1882
1883 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1884 "interface object already bound");
1885 wl_resource_destroy(resource, 0);
1886}
1887
Kristian Høgsberg07045392012-02-19 18:52:44 -05001888struct switcher {
1889 struct weston_compositor *compositor;
1890 struct weston_surface *current;
1891 struct wl_listener listener;
1892 struct wl_keyboard_grab grab;
1893};
1894
1895static void
1896switcher_next(struct switcher *switcher)
1897{
1898 struct weston_compositor *compositor = switcher->compositor;
1899 struct weston_surface *surface;
1900 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
1901
1902 wl_list_for_each(surface, &compositor->surface_list, link) {
1903 /* Workaround for cursor surfaces. */
1904 if (surface->surface.resource.destroy_listener_list.next == NULL)
1905 continue;
1906
1907 switch (get_shell_surface_type(surface)) {
1908 case SHELL_SURFACE_TOPLEVEL:
1909 case SHELL_SURFACE_FULLSCREEN:
1910 case SHELL_SURFACE_MAXIMIZED:
1911 if (first == NULL)
1912 first = surface;
1913 if (prev == switcher->current)
1914 next = surface;
1915 prev = surface;
1916 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05001917 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05001918 weston_surface_damage(surface);
1919 break;
1920 default:
1921 break;
1922 }
1923 }
1924
1925 if (next == NULL)
1926 next = first;
1927
Alex Wu07b26062012-03-12 16:06:01 +08001928 if (next == NULL)
1929 return;
1930
Kristian Høgsberg07045392012-02-19 18:52:44 -05001931 wl_list_remove(&switcher->listener.link);
1932 wl_list_insert(next->surface.resource.destroy_listener_list.prev,
1933 &switcher->listener.link);
1934
1935 switcher->current = next;
1936 next->alpha = 255;
1937}
1938
1939static void
1940switcher_handle_surface_destroy(struct wl_listener *listener,
1941 struct wl_resource *resource, uint32_t time)
1942{
1943 struct switcher *switcher =
1944 container_of(listener, struct switcher, listener);
1945
1946 switcher_next(switcher);
1947}
1948
1949static void
1950switcher_destroy(struct switcher *switcher, uint32_t time)
1951{
1952 struct weston_compositor *compositor = switcher->compositor;
1953 struct weston_surface *surface;
1954 struct weston_input_device *device =
1955 (struct weston_input_device *) switcher->grab.input_device;
1956
1957 wl_list_for_each(surface, &compositor->surface_list, link) {
1958 surface->alpha = 255;
1959 weston_surface_damage(surface);
1960 }
1961
Alex Wu07b26062012-03-12 16:06:01 +08001962 if (switcher->current)
1963 activate(compositor->shell, switcher->current, device, time);
Kristian Høgsberg07045392012-02-19 18:52:44 -05001964 wl_list_remove(&switcher->listener.link);
1965 wl_input_device_end_keyboard_grab(&device->input_device, time);
1966 free(switcher);
1967}
1968
1969static void
1970switcher_key(struct wl_keyboard_grab *grab,
1971 uint32_t time, uint32_t key, int32_t state)
1972{
1973 struct switcher *switcher = container_of(grab, struct switcher, grab);
1974 struct weston_input_device *device =
1975 (struct weston_input_device *) grab->input_device;
1976
1977 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
1978 switcher_destroy(switcher, time);
1979 } else if (key == KEY_TAB && state) {
1980 switcher_next(switcher);
1981 }
1982};
1983
1984static const struct wl_keyboard_grab_interface switcher_grab = {
1985 switcher_key
1986};
1987
1988static void
1989switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001990 uint32_t key, uint32_t button, uint32_t axis,
1991 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05001992{
1993 struct weston_compositor *compositor = data;
1994 struct switcher *switcher;
1995
1996 switcher = malloc(sizeof *switcher);
1997 switcher->compositor = compositor;
1998 switcher->current = NULL;
1999 switcher->listener.func = switcher_handle_surface_destroy;
2000 wl_list_init(&switcher->listener.link);
2001
2002 switcher->grab.interface = &switcher_grab;
2003 wl_input_device_start_keyboard_grab(device, &switcher->grab, time);
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05002004 wl_input_device_set_keyboard_focus(device, NULL,
2005 weston_compositor_get_time());
Kristian Høgsberg07045392012-02-19 18:52:44 -05002006 switcher_next(switcher);
2007}
2008
Pekka Paalanen3c647232011-12-22 13:43:43 +02002009static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002010backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002011 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002012{
2013 struct weston_compositor *compositor = data;
2014 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002015 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002016
2017 /* TODO: we're limiting to simple use cases, where we assume just
2018 * control on the primary display. We'd have to extend later if we
2019 * ever get support for setting backlights on random desktop LCD
2020 * panels though */
2021 output = get_default_output(compositor);
2022 if (!output)
2023 return;
2024
2025 if (!output->set_backlight)
2026 return;
2027
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002028 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2029 backlight_new = output->backlight_current - 25;
2030 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2031 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002032
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002033 if (backlight_new < 5)
2034 backlight_new = 5;
2035 if (backlight_new > 255)
2036 backlight_new = 255;
2037
2038 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002039 output->set_backlight(output, output->backlight_current);
2040}
2041
2042static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002043debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002044 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002045{
2046 struct weston_compositor *compositor = data;
2047 struct wl_shell *shell =
2048 container_of(compositor->shell, struct wl_shell, shell);
2049 struct weston_surface *surface;
2050
2051 if (shell->debug_repaint_surface) {
2052 weston_surface_destroy(shell->debug_repaint_surface);
2053 shell->debug_repaint_surface = NULL;
2054 } else {
2055 surface = weston_surface_create(compositor);
2056 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2057 weston_surface_configure(surface, 0, 0, 8192, 8192);
2058 wl_list_insert(&compositor->fade_layer.surface_list,
2059 &surface->layer_link);
2060 weston_surface_assign_output(surface);
2061 pixman_region32_init(&surface->input);
2062
2063 /* Here's the dirty little trick that makes the
2064 * repaint debugging work: we force an
2065 * update_transform first to update dependent state
2066 * and clear the geometry.dirty bit. Then we clear
2067 * the surface damage so it only gets repainted
2068 * piecewise as we repaint other things. */
2069
2070 weston_surface_update_transform(surface);
2071 pixman_region32_fini(&surface->damage);
2072 pixman_region32_init(&surface->damage);
2073 shell->debug_repaint_surface = surface;
2074 }
2075}
2076
2077static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05002078shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002079{
2080 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
2081
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002082 if (shell->child.client)
2083 wl_client_destroy(shell->child.client);
2084
Pekka Paalanen3c647232011-12-22 13:43:43 +02002085 free(shell->screensaver.path);
2086 free(shell);
2087}
2088
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002089int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002090shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002091
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002092WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002093shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002094{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002095 struct wl_shell *shell;
2096
2097 shell = malloc(sizeof *shell);
2098 if (shell == NULL)
2099 return -1;
2100
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002101 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002102 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002103 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02002104 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05002105 shell->shell.map = map;
2106 shell->shell.configure = configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +02002107 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002108
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002109 wl_list_init(&shell->backgrounds);
2110 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002111 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002112
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002113 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2114 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2115 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2116 weston_layer_init(&shell->background_layer,
2117 &shell->toplevel_layer.link);
2118 wl_list_init(&shell->lock_layer.surface_list);
2119
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002120 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002121
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002122 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2123 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002124 return -1;
2125
Kristian Høgsberg75840622011-09-06 13:48:16 -04002126 if (wl_display_add_global(ec->wl_display,
2127 &desktop_shell_interface,
2128 shell, bind_desktop_shell) == NULL)
2129 return -1;
2130
Pekka Paalanen6e168112011-11-24 11:34:05 +02002131 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2132 shell, bind_screensaver) == NULL)
2133 return -1;
2134
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002135 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002136 if (launch_desktop_shell_process(shell) != 0)
2137 return -1;
2138
Scott Moreau6a3633d2012-03-20 08:47:59 -06002139 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, MODIFIER_SUPER,
2140 move_binding, shell);
2141 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, MODIFIER_SUPER,
2142 resize_binding, shell);
2143 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2144 MODIFIER_CTRL | MODIFIER_ALT,
2145 terminate_binding, ec);
2146 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2147 click_to_activate_binding, ec);
Scott Moreau1b45a792012-03-22 10:58:23 -06002148 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002149 MODIFIER_SUPER | MODIFIER_ALT,
2150 surface_opacity_binding, NULL);
2151 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreau1b45a792012-03-22 10:58:23 -06002152 MODIFIER_SUPER, zoom_binding, NULL);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002153 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002154 MODIFIER_SUPER | MODIFIER_ALT,
2155 rotate_binding, NULL);
2156 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, MODIFIER_SUPER,
2157 switcher_binding, ec);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002158
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002159 /* brightness */
Scott Moreau6a3633d2012-03-20 08:47:59 -06002160 weston_compositor_add_binding(ec, KEY_F9, 0, 0, MODIFIER_CTRL,
2161 backlight_binding, ec);
2162 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2163 backlight_binding, ec);
2164 weston_compositor_add_binding(ec, KEY_F10, 0, 0, MODIFIER_CTRL,
2165 backlight_binding, ec);
2166 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2167 backlight_binding, ec);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002168
Scott Moreau6a3633d2012-03-20 08:47:59 -06002169 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, MODIFIER_SUPER,
2170 debug_repaint_binding, ec);
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002171
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002172 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04002173
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002174 return 0;
2175}