blob: e1049417b5fb10a83e49cc1e67c8935ded9e2f42 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
2 * Copyright © 2010 Intel Corporation
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003 * Copyright © 2011 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>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050031
Pekka Paalanen50719bc2011-11-22 14:18:50 +020032#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050033#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040034#include "desktop-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050035
Pekka Paalanen068ae942011-11-28 14:11:15 +020036struct shell_surface;
37
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040038struct wl_shell {
Kristian Høgsberg75840622011-09-06 13:48:16 -040039 struct wlsc_compositor *compositor;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040040 struct wlsc_shell shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020041
42 struct {
43 struct wlsc_process process;
44 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020045 struct wl_resource *desktop_shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020046 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020047
48 bool locked;
49 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020050
Pekka Paalanen068ae942011-11-28 14:11:15 +020051 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050052 struct wl_listener lock_surface_listener;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020053 struct wl_list hidden_surface_list;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010054
55 struct wl_list backgrounds;
56 struct wl_list panels;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040057};
58
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050059enum shell_surface_type {
Pekka Paalanen57da4a82011-11-23 16:42:16 +020060 SHELL_SURFACE_NORMAL,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050061
Pekka Paalanen57da4a82011-11-23 16:42:16 +020062 SHELL_SURFACE_PANEL,
63 SHELL_SURFACE_BACKGROUND,
64 SHELL_SURFACE_LOCK,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050065
66 SHELL_SURFACE_TOPLEVEL,
67 SHELL_SURFACE_TRANSIENT,
68 SHELL_SURFACE_FULLSCREEN
Pekka Paalanen57da4a82011-11-23 16:42:16 +020069};
70
Pekka Paalanen56cdea92011-11-23 16:14:12 +020071struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020072 struct wl_resource resource;
73
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010074 struct wlsc_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020075 struct wl_listener surface_destroy_listener;
Pekka Paalanen57da4a82011-11-23 16:42:16 +020076
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050077 enum shell_surface_type type;
78 int32_t saved_x, saved_y;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010079
80 struct wlsc_output *output;
81 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +020082};
83
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050084struct wlsc_move_grab {
85 struct wl_grab grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050086 struct wlsc_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050087 int32_t dx, dy;
88};
89
Pekka Paalanen56cdea92011-11-23 16:14:12 +020090static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050091move_grab_motion(struct wl_grab *grab,
92 uint32_t time, int32_t x, int32_t y)
93{
94 struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050095 struct wlsc_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050096
Kristian Høgsberga691aee2011-06-23 21:43:50 -040097 wlsc_surface_configure(es, x + move->dx, y + move->dy,
98 es->width, es->height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050099}
100
101static void
102move_grab_button(struct wl_grab *grab,
103 uint32_t time, int32_t button, int32_t state)
104{
105}
106
107static void
108move_grab_end(struct wl_grab *grab, uint32_t time)
109{
110 free(grab);
111}
112
113static const struct wl_grab_interface move_grab_interface = {
114 move_grab_motion,
115 move_grab_button,
116 move_grab_end
117};
118
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400119static int
120wlsc_surface_move(struct wlsc_surface *es,
121 struct wlsc_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500122{
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500123 struct wlsc_move_grab *move;
124
125 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400126 if (!move)
127 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500128
129 move->grab.interface = &move_grab_interface;
130 move->dx = es->x - wd->input_device.grab_x;
131 move->dy = es->y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500132 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500133
134 if (wl_input_device_update_grab(&wd->input_device,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400135 &move->grab, &es->surface, time) < 0)
136 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500137
138 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400139 wl_input_device_set_pointer_focus(&wd->input_device,
140 NULL, time, 0, 0, 0, 0);
141
142 return 0;
143}
144
145static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200146shell_surface_move(struct wl_client *client, struct wl_resource *resource,
147 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400148{
149 struct wlsc_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200150 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400151
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200152 if (wlsc_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400153 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500154}
155
156struct wlsc_resize_grab {
157 struct wl_grab grab;
158 uint32_t edges;
159 int32_t dx, dy, width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200160 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500161};
162
163static void
164resize_grab_motion(struct wl_grab *grab,
165 uint32_t time, int32_t x, int32_t y)
166{
167 struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab;
168 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500169 int32_t width, height;
170
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200171 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500172 width = device->grab_x - x + resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200173 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500174 width = x - device->grab_x + resize->width;
175 } else {
176 width = resize->width;
177 }
178
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200179 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500180 height = device->grab_y - y + resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200181 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500182 height = y - device->grab_y + resize->height;
183 } else {
184 height = resize->height;
185 }
186
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200187 wl_resource_post_event(&resize->shsurf->resource,
188 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
189 width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500190}
191
192static void
193resize_grab_button(struct wl_grab *grab,
194 uint32_t time, int32_t button, int32_t state)
195{
196}
197
198static void
199resize_grab_end(struct wl_grab *grab, uint32_t time)
200{
201 free(grab);
202}
203
204static const struct wl_grab_interface resize_grab_interface = {
205 resize_grab_motion,
206 resize_grab_button,
207 resize_grab_end
208};
209
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400210static int
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200211wlsc_surface_resize(struct shell_surface *shsurf,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400212 struct wlsc_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200213 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500214{
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500215 struct wlsc_resize_grab *resize;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200216 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500217 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500218
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500219 /* FIXME: Reject if fullscreen */
220
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500221 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400222 if (!resize)
223 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500224
225 resize->grab.interface = &resize_grab_interface;
226 resize->edges = edges;
227 resize->dx = es->x - wd->input_device.grab_x;
228 resize->dy = es->y - wd->input_device.grab_y;
229 resize->width = es->width;
230 resize->height = es->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200231 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400232
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500233 if (edges == 0 || edges > 15 ||
234 (edges & 3) == 3 || (edges & 12) == 12)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400235 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500236
237 switch (edges) {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200238 case WL_SHELL_SURFACE_RESIZE_TOP:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500239 pointer = WLSC_POINTER_TOP;
240 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200241 case WL_SHELL_SURFACE_RESIZE_BOTTOM:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500242 pointer = WLSC_POINTER_BOTTOM;
243 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200244 case WL_SHELL_SURFACE_RESIZE_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500245 pointer = WLSC_POINTER_LEFT;
246 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200247 case WL_SHELL_SURFACE_RESIZE_TOP_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500248 pointer = WLSC_POINTER_TOP_LEFT;
249 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200250 case WL_SHELL_SURFACE_RESIZE_BOTTOM_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500251 pointer = WLSC_POINTER_BOTTOM_LEFT;
252 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200253 case WL_SHELL_SURFACE_RESIZE_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500254 pointer = WLSC_POINTER_RIGHT;
255 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200256 case WL_SHELL_SURFACE_RESIZE_TOP_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500257 pointer = WLSC_POINTER_TOP_RIGHT;
258 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200259 case WL_SHELL_SURFACE_RESIZE_BOTTOM_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500260 pointer = WLSC_POINTER_BOTTOM_RIGHT;
261 break;
262 }
263
264 if (wl_input_device_update_grab(&wd->input_device,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400265 &resize->grab, &es->surface, time) < 0)
266 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500267
268 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400269 wl_input_device_set_pointer_focus(&wd->input_device,
270 NULL, time, 0, 0, 0, 0);
271
272 return 0;
273}
274
275static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200276shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
277 struct wl_resource *input_resource, uint32_t time,
278 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400279{
280 struct wlsc_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200281 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400282
283 /* FIXME: Reject if fullscreen */
284
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200285 if (wlsc_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400286 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500287}
288
289static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200290shell_surface_set_toplevel(struct wl_client *client,
291 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400292
293{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200294 struct shell_surface *shsurf = resource->data;
295 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400296
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200297 if (shsurf->type == SHELL_SURFACE_FULLSCREEN) {
298 es->x = shsurf->saved_x;
299 es->y = shsurf->saved_y;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400300 }
301
302 wlsc_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200303 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400304 es->fullscreen_output = NULL;
305}
306
307static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200308shell_surface_set_transient(struct wl_client *client,
309 struct wl_resource *resource,
310 struct wl_resource *parent_resource,
311 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400312{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200313 struct shell_surface *shsurf = resource->data;
314 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400315 struct wlsc_surface *pes = parent_resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400316
317 /* assign to parents output */
318 es->output = pes->output;
319
320 es->x = pes->x + x;
321 es->y = pes->y + y;
322
323 wlsc_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200324 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400325}
326
327static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200328shell_surface_set_fullscreen(struct wl_client *client,
329 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400330
331{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200332 struct shell_surface *shsurf = resource->data;
333 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400334 struct wlsc_output *output;
335
336 /* FIXME: Fullscreen on first output */
337 /* FIXME: Handle output going away */
338 output = container_of(es->compositor->output_list.next,
339 struct wlsc_output, link);
340 es->output = output;
341
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200342 shsurf->saved_x = es->x;
343 shsurf->saved_y = es->y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400344 es->x = (output->current->width - es->width) / 2;
345 es->y = (output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400346 es->fullscreen_output = output;
347 wlsc_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200348 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400349}
350
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200351static const struct wl_shell_surface_interface shell_surface_implementation = {
352 shell_surface_move,
353 shell_surface_resize,
354 shell_surface_set_toplevel,
355 shell_surface_set_transient,
356 shell_surface_set_fullscreen
357};
358
359static void
360destroy_shell_surface(struct wl_resource *resource)
361{
362 struct shell_surface *shsurf = resource->data;
363
364 /* in case cleaning up a dead client destroys shell_surface first */
365 if (shsurf->surface)
366 wl_list_remove(&shsurf->surface_destroy_listener.link);
367
368 wl_list_remove(&shsurf->link);
369 free(shsurf);
370}
371
372static void
373shell_handle_surface_destroy(struct wl_listener *listener,
374 struct wl_resource *resource, uint32_t time)
375{
376 struct shell_surface *shsurf = container_of(listener,
377 struct shell_surface,
378 surface_destroy_listener);
379
380 shsurf->surface = NULL;
381 wl_resource_destroy(&shsurf->resource, time);
382}
383
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200384static struct shell_surface *
385get_shell_surface(struct wlsc_surface *surface)
386{
387 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
388 struct wl_listener *listener;
389
390 /* search the destroy listener list for our callback */
391 wl_list_for_each(listener, lst, link) {
392 if (listener->func == shell_handle_surface_destroy) {
393 return container_of(listener, struct shell_surface,
394 surface_destroy_listener);
395 }
396 }
397
398 return NULL;
399}
400
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200401static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200402shell_get_shell_surface(struct wl_client *client,
403 struct wl_resource *resource,
404 uint32_t id,
405 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200406{
407 struct wlsc_surface *surface = surface_resource->data;
408 struct shell_surface *shsurf;
409
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200410 if (get_shell_surface(surface)) {
411 wl_resource_post_error(surface_resource,
412 WL_DISPLAY_ERROR_INVALID_OBJECT,
413 "wl_shell::get_shell_surface already requested");
414 return;
415 }
416
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200417 shsurf = calloc(1, sizeof *shsurf);
418 if (!shsurf) {
419 wl_resource_post_no_memory(resource);
420 return;
421 }
422
423 shsurf->resource.destroy = destroy_shell_surface;
424 shsurf->resource.object.id = id;
425 shsurf->resource.object.interface = &wl_shell_surface_interface;
426 shsurf->resource.object.implementation =
427 (void (**)(void)) &shell_surface_implementation;
428 shsurf->resource.data = shsurf;
429
430 shsurf->surface = surface;
431 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
432 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
433 &shsurf->surface_destroy_listener.link);
434
435 /* init link so its safe to always remove it in destroy_shell_surface */
436 wl_list_init(&shsurf->link);
437
438 shsurf->type = SHELL_SURFACE_NORMAL;
439
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200440 wl_client_add_resource(client, &shsurf->resource);
441}
442
443static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200444 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500445};
446
Kristian Høgsberg07937562011-04-12 17:25:42 -0400447static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400448desktop_shell_set_background(struct wl_client *client,
449 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100450 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400451 struct wl_resource *surface_resource)
452{
453 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200454 struct shell_surface *shsurf = surface_resource->data;
455 struct wlsc_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200456 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400457
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100458 wl_list_for_each(priv, &shell->backgrounds, link) {
459 if (priv->output == output_resource->data) {
460 priv->surface->output = NULL;
461 wl_list_remove(&priv->surface->link);
462 wl_list_remove(&priv->link);
463 break;
464 }
465 }
466
Pekka Paalanen068ae942011-11-28 14:11:15 +0200467 shsurf->type = SHELL_SURFACE_BACKGROUND;
468 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100469
Pekka Paalanen068ae942011-11-28 14:11:15 +0200470 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100471
Pekka Paalanen068ae942011-11-28 14:11:15 +0200472 surface->x = shsurf->output->x;
473 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200474
Kristian Høgsberg75840622011-09-06 13:48:16 -0400475 wl_resource_post_event(resource,
476 DESKTOP_SHELL_CONFIGURE,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200477 wlsc_compositor_get_time(), 0, surface_resource,
478 shsurf->output->current->width,
479 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400480}
481
482static void
483desktop_shell_set_panel(struct wl_client *client,
484 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100485 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400486 struct wl_resource *surface_resource)
487{
488 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200489 struct shell_surface *shsurf = surface_resource->data;
490 struct wlsc_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200491 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400492
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100493 wl_list_for_each(priv, &shell->panels, link) {
494 if (priv->output == output_resource->data) {
495 priv->surface->output = NULL;
496 wl_list_remove(&priv->surface->link);
497 wl_list_remove(&priv->link);
498 break;
499 }
500 }
501
Pekka Paalanen068ae942011-11-28 14:11:15 +0200502 shsurf->type = SHELL_SURFACE_PANEL;
503 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100504
Pekka Paalanen068ae942011-11-28 14:11:15 +0200505 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100506
Pekka Paalanen068ae942011-11-28 14:11:15 +0200507 surface->x = shsurf->output->x;
508 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200509
Kristian Høgsberg75840622011-09-06 13:48:16 -0400510 wl_resource_post_event(resource,
511 DESKTOP_SHELL_CONFIGURE,
512 wlsc_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200513 shsurf->output->current->width,
514 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400515}
516
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200517static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500518handle_lock_surface_destroy(struct wl_listener *listener,
519 struct wl_resource *resource, uint32_t time)
520{
521 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200522 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500523
524 fprintf(stderr, "lock surface gone\n");
525 shell->lock_surface = NULL;
526}
527
528static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200529desktop_shell_set_lock_surface(struct wl_client *client,
530 struct wl_resource *resource,
531 struct wl_resource *surface_resource)
532{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200533 struct wl_shell *shell = resource->data;
534
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200535 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200536
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200537 if (!shell->locked)
538 return;
539
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500540 shell->lock_surface = surface_resource->data;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200541
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500542 shell->lock_surface_listener.func = handle_lock_surface_destroy;
543 wl_list_insert(&surface_resource->destroy_listener_list,
544 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200545
Pekka Paalanen068ae942011-11-28 14:11:15 +0200546 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200547}
548
549static void
550resume_desktop(struct wl_shell *shell)
551{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500552 struct wlsc_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200553 struct wl_list *list;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200554
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500555 wl_list_for_each(surface, &shell->hidden_surface_list, link)
556 wlsc_surface_configure(surface, surface->x, surface->y,
557 surface->width, surface->height);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200558
Pekka Paalanenfe340832011-11-25 16:07:52 +0200559 if (wl_list_empty(&shell->backgrounds)) {
560 list = &shell->compositor->surface_list;
561 } else {
562 struct shell_surface *background;
563 background = container_of(shell->backgrounds.prev,
564 struct shell_surface, link);
565 list = background->surface->link.prev;
566 }
567
568 if (!wl_list_empty(&shell->hidden_surface_list))
569 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500570 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200571
572 shell->locked = false;
573 wlsc_compositor_repick(shell->compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200574 wlsc_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200575}
576
577static void
578desktop_shell_unlock(struct wl_client *client,
579 struct wl_resource *resource)
580{
581 struct wl_shell *shell = resource->data;
582
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200583 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200584
585 if (shell->locked)
586 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200587}
588
Kristian Høgsberg75840622011-09-06 13:48:16 -0400589static const struct desktop_shell_interface desktop_shell_implementation = {
590 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200591 desktop_shell_set_panel,
592 desktop_shell_set_lock_surface,
593 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400594};
595
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200596static enum shell_surface_type
597get_shell_surface_type(struct wlsc_surface *surface)
598{
599 struct shell_surface *shsurf;
600
601 shsurf = get_shell_surface(surface);
602 if (!shsurf)
603 return SHELL_SURFACE_NORMAL;
604 return shsurf->type;
605}
606
Kristian Høgsberg75840622011-09-06 13:48:16 -0400607static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400608move_binding(struct wl_input_device *device, uint32_t time,
609 uint32_t key, uint32_t button, uint32_t state, void *data)
610{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400611 struct wlsc_surface *surface =
612 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500613
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100614 if (surface == NULL)
615 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400616
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200617 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100618 case SHELL_SURFACE_PANEL:
619 case SHELL_SURFACE_BACKGROUND:
620 case SHELL_SURFACE_FULLSCREEN:
621 return;
622 default:
623 break;
624 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400625
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400626 wlsc_surface_move(surface, (struct wlsc_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400627}
628
629static void
630resize_binding(struct wl_input_device *device, uint32_t time,
631 uint32_t key, uint32_t button, uint32_t state, void *data)
632{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400633 struct wlsc_surface *surface =
634 (struct wlsc_surface *) device->pointer_focus;
635 uint32_t edges = 0;
636 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200637 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500638
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100639 if (surface == NULL)
640 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200641
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200642 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200643 if (!shsurf)
644 return;
645
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200646 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100647 case SHELL_SURFACE_PANEL:
648 case SHELL_SURFACE_BACKGROUND:
649 case SHELL_SURFACE_FULLSCREEN:
650 return;
651 default:
652 break;
653 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400654
Kristian Høgsberg07937562011-04-12 17:25:42 -0400655 x = device->grab_x - surface->x;
656 y = device->grab_y - surface->y;
657
658 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200659 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400660 else if (x < 2 * surface->width / 3)
661 edges |= 0;
662 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200663 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400664
665 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200666 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400667 else if (y < 2 * surface->height / 3)
668 edges |= 0;
669 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200670 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400671
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200672 wlsc_surface_resize(shsurf, (struct wlsc_input_device *) device,
673 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400674}
675
676static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400677activate(struct wlsc_shell *base, struct wlsc_surface *es,
678 struct wlsc_input_device *device, uint32_t time)
679{
680 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
681 struct wlsc_compositor *compositor = shell->compositor;
682
683 wlsc_surface_activate(es, device, time);
684
Kristian Høgsbergd6e55252011-10-11 23:41:17 -0400685 if (compositor->wxs)
686 wlsc_xserver_surface_activate(es);
687
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200688 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200689 case SHELL_SURFACE_BACKGROUND:
690 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -0400691 wl_list_remove(&es->link);
692 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200693 break;
694 case SHELL_SURFACE_PANEL:
695 /* already put on top */
696 break;
697 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100698 if (!shell->locked) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200699 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100700 struct shell_surface *panel;
701 wl_list_for_each(panel, &shell->panels, link) {
702 wl_list_remove(&panel->surface->link);
703 wl_list_insert(&compositor->surface_list,
704 &panel->surface->link);
705 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200706 }
Kristian Høgsberg75840622011-09-06 13:48:16 -0400707 }
708}
709
710static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200711lock(struct wlsc_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400712{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200713 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200714 struct wl_list *surface_list = &shell->compositor->surface_list;
715 struct wlsc_surface *cur;
716 struct wlsc_surface *tmp;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200717 struct wlsc_input_device *device;
718 uint32_t time;
719
720 if (shell->locked)
721 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200722
723 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200724
725 /* Move all surfaces from compositor's list to our hidden list,
726 * except the background. This way nothing else can show or
727 * receive input events while we are locked. */
728
729 if (!wl_list_empty(&shell->hidden_surface_list)) {
730 fprintf(stderr,
731 "%s: Assertion failed: hidden_surface_list is not empty.\n",
732 __func__);
733 }
734
735 wl_list_for_each_safe(cur, tmp, surface_list, link) {
736 /* skip input device sprites, cur->surface is uninitialised */
737 if (cur->surface.resource.client == NULL)
738 continue;
739
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200740 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200741 continue;
742
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500743 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200744 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500745 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200746 }
747
748 /* reset pointer foci */
749 wlsc_compositor_repick(shell->compositor);
750
751 /* reset keyboard foci */
752 time = wlsc_compositor_get_time();
753 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
754 wl_input_device_set_keyboard_focus(&device->input_device,
755 NULL, time);
756 }
757
758 /* TODO: disable bindings that should not work while locked. */
759
760 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200761}
762
763static void
764unlock(struct wlsc_shell *base)
765{
766 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
767
Pekka Paalanend81c2162011-11-16 13:47:34 +0200768 if (!shell->locked || shell->lock_surface) {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200769 wlsc_compositor_wake(shell->compositor);
770 return;
771 }
772
773 /* If desktop-shell client has gone away, unlock immediately. */
774 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200775 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200776 return;
777 }
778
779 if (shell->prepare_event_sent)
780 return;
781
782 wl_resource_post_event(shell->child.desktop_shell,
783 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
784 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400785}
786
787static void
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500788map(struct wlsc_shell *base,
789 struct wlsc_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400790{
Kristian Høgsberg75840622011-09-06 13:48:16 -0400791 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
792 struct wlsc_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500793 struct wl_list *list;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200794 enum shell_surface_type surface_type;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200795
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200796 surface_type = get_shell_surface_type(surface);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500797
798 if (shell->locked)
799 list = &shell->hidden_surface_list;
800 else
801 list = &compositor->surface_list;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400802
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200803 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200804 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200805 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200806 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500807 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200808 break;
809 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200810 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500811 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200812 break;
813 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200814 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500815 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200816
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500817 wlsc_compositor_repick(compositor);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200818 wlsc_compositor_wake(compositor);
819 break;
820 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200821 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100822 if (!wl_list_empty(&shell->panels)) {
823 struct shell_surface *panel =
824 container_of(shell->panels.prev,
825 struct shell_surface, link);
826 wl_list_insert(&panel->surface->link, &surface->link);
827 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200828 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100829 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200830 }
831
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200832 if (surface_type == SHELL_SURFACE_TOPLEVEL) {
Kristian Høgsberg46770132011-11-09 12:38:53 -0500833 surface->x = 10 + random() % 400;
834 surface->y = 10 + random() % 400;
835 }
836
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500837 surface->width = width;
838 surface->height = height;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200839 if (!shell->locked || surface_type == SHELL_SURFACE_LOCK)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500840 wlsc_surface_configure(surface,
841 surface->x, surface->y, width, height);
Kristian Høgsberg2f88a402011-12-04 15:32:59 -0500842
843 if (surface_type == SHELL_SURFACE_TOPLEVEL)
844 wlsc_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500845}
846
847static void
848configure(struct wlsc_shell *shell, struct wlsc_surface *surface,
849 int32_t x, int32_t y, int32_t width, int32_t height)
850{
851 struct wlsc_mode *current;
852
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200853 switch (get_shell_surface_type(surface)) {
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500854 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500855 current = surface->fullscreen_output->current;
856 x = (current->width - surface->width) / 2;
857 y = (current->height - surface->height) / 2;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500858 break;
859 default:
860 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400861 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500862
863 wlsc_surface_configure(surface, x, y, width, height);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400864}
865
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400866static void
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200867desktop_shell_sigchld(struct wlsc_process *process, int status)
868{
869 struct wl_shell *shell =
870 container_of(process, struct wl_shell, child.process);
871
872 shell->child.process.pid = 0;
873 shell->child.client = NULL; /* already destroyed by wayland */
874}
875
876static int
877launch_desktop_shell_process(struct wl_shell *shell)
878{
Kristian Høgsbergc4693c42011-11-14 14:57:17 -0500879 const char *shell_exe = LIBEXECDIR "/wayland-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200880
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200881 shell->child.client = wlsc_client_launch(shell->compositor,
882 &shell->child.process,
883 shell_exe,
884 desktop_shell_sigchld);
885
886 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200887 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200888 return 0;
889}
890
891static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400892bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
893{
894 struct wl_shell *shell = data;
895
896 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200897 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400898}
899
Kristian Høgsberg75840622011-09-06 13:48:16 -0400900static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200901unbind_desktop_shell(struct wl_resource *resource)
902{
903 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500904
905 if (shell->locked)
906 resume_desktop(shell);
907
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200908 shell->child.desktop_shell = NULL;
909 shell->prepare_event_sent = false;
910 free(resource);
911}
912
913static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400914bind_desktop_shell(struct wl_client *client,
915 void *data, uint32_t version, uint32_t id)
916{
917 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +0200918 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400919
Pekka Paalanenbbe60522011-11-03 14:11:33 +0200920 resource = wl_client_add_object(client, &desktop_shell_interface,
921 &desktop_shell_implementation,
922 id, shell);
923
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200924 if (client == shell->child.client) {
925 resource->destroy = unbind_desktop_shell;
926 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +0200927 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200928 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +0200929
930 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
931 "permission to bind desktop_shell denied");
932 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400933}
934
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400935int
936shell_init(struct wlsc_compositor *ec);
937
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400938WL_EXPORT int
939shell_init(struct wlsc_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500940{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400941 struct wl_shell *shell;
942
943 shell = malloc(sizeof *shell);
944 if (shell == NULL)
945 return -1;
946
Kristian Høgsbergf0d91162011-10-11 22:44:23 -0400947 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400948 shell->compositor = ec;
949 shell->shell.activate = activate;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400950 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200951 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500952 shell->shell.map = map;
953 shell->shell.configure = configure;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500954
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200955 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100956 wl_list_init(&shell->backgrounds);
957 wl_list_init(&shell->panels);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200958
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400959 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
960 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500961 return -1;
962
Kristian Høgsberg75840622011-09-06 13:48:16 -0400963 if (wl_display_add_global(ec->wl_display,
964 &desktop_shell_interface,
965 shell, bind_desktop_shell) == NULL)
966 return -1;
967
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200968 if (launch_desktop_shell_process(shell) != 0)
969 return -1;
970
Kristian Høgsberg07937562011-04-12 17:25:42 -0400971 wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400972 move_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400973 wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400974 resize_binding, shell);
975
976 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400977
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500978 return 0;
979}