blob: 22004f9f6c98e58120b5461e2c153109fb698512 [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;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200315 struct shell_surface *pshsurf = parent_resource->data;
316 struct wlsc_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400317
318 /* assign to parents output */
319 es->output = pes->output;
320
321 es->x = pes->x + x;
322 es->y = pes->y + y;
323
324 wlsc_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200325 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400326}
327
328static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200329shell_surface_set_fullscreen(struct wl_client *client,
330 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400331
332{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200333 struct shell_surface *shsurf = resource->data;
334 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400335 struct wlsc_output *output;
336
337 /* FIXME: Fullscreen on first output */
338 /* FIXME: Handle output going away */
339 output = container_of(es->compositor->output_list.next,
340 struct wlsc_output, link);
341 es->output = output;
342
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200343 shsurf->saved_x = es->x;
344 shsurf->saved_y = es->y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400345 es->x = (output->current->width - es->width) / 2;
346 es->y = (output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400347 es->fullscreen_output = output;
348 wlsc_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200349 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400350}
351
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200352static const struct wl_shell_surface_interface shell_surface_implementation = {
353 shell_surface_move,
354 shell_surface_resize,
355 shell_surface_set_toplevel,
356 shell_surface_set_transient,
357 shell_surface_set_fullscreen
358};
359
360static void
361destroy_shell_surface(struct wl_resource *resource)
362{
363 struct shell_surface *shsurf = resource->data;
364
365 /* in case cleaning up a dead client destroys shell_surface first */
366 if (shsurf->surface)
367 wl_list_remove(&shsurf->surface_destroy_listener.link);
368
369 wl_list_remove(&shsurf->link);
370 free(shsurf);
371}
372
373static void
374shell_handle_surface_destroy(struct wl_listener *listener,
375 struct wl_resource *resource, uint32_t time)
376{
377 struct shell_surface *shsurf = container_of(listener,
378 struct shell_surface,
379 surface_destroy_listener);
380
381 shsurf->surface = NULL;
382 wl_resource_destroy(&shsurf->resource, time);
383}
384
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200385static struct shell_surface *
386get_shell_surface(struct wlsc_surface *surface)
387{
388 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
389 struct wl_listener *listener;
390
391 /* search the destroy listener list for our callback */
392 wl_list_for_each(listener, lst, link) {
393 if (listener->func == shell_handle_surface_destroy) {
394 return container_of(listener, struct shell_surface,
395 surface_destroy_listener);
396 }
397 }
398
399 return NULL;
400}
401
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200402static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200403shell_get_shell_surface(struct wl_client *client,
404 struct wl_resource *resource,
405 uint32_t id,
406 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200407{
408 struct wlsc_surface *surface = surface_resource->data;
409 struct shell_surface *shsurf;
410
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200411 if (get_shell_surface(surface)) {
412 wl_resource_post_error(surface_resource,
413 WL_DISPLAY_ERROR_INVALID_OBJECT,
414 "wl_shell::get_shell_surface already requested");
415 return;
416 }
417
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200418 shsurf = calloc(1, sizeof *shsurf);
419 if (!shsurf) {
420 wl_resource_post_no_memory(resource);
421 return;
422 }
423
424 shsurf->resource.destroy = destroy_shell_surface;
425 shsurf->resource.object.id = id;
426 shsurf->resource.object.interface = &wl_shell_surface_interface;
427 shsurf->resource.object.implementation =
428 (void (**)(void)) &shell_surface_implementation;
429 shsurf->resource.data = shsurf;
430
431 shsurf->surface = surface;
432 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
433 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
434 &shsurf->surface_destroy_listener.link);
435
436 /* init link so its safe to always remove it in destroy_shell_surface */
437 wl_list_init(&shsurf->link);
438
439 shsurf->type = SHELL_SURFACE_NORMAL;
440
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200441 wl_client_add_resource(client, &shsurf->resource);
442}
443
444static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200445 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500446};
447
Kristian Høgsberg07937562011-04-12 17:25:42 -0400448static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400449desktop_shell_set_background(struct wl_client *client,
450 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100451 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400452 struct wl_resource *surface_resource)
453{
454 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200455 struct shell_surface *shsurf = surface_resource->data;
456 struct wlsc_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200457 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400458
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100459 wl_list_for_each(priv, &shell->backgrounds, link) {
460 if (priv->output == output_resource->data) {
461 priv->surface->output = NULL;
462 wl_list_remove(&priv->surface->link);
463 wl_list_remove(&priv->link);
464 break;
465 }
466 }
467
Pekka Paalanen068ae942011-11-28 14:11:15 +0200468 shsurf->type = SHELL_SURFACE_BACKGROUND;
469 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100470
Pekka Paalanen068ae942011-11-28 14:11:15 +0200471 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100472
Pekka Paalanen068ae942011-11-28 14:11:15 +0200473 surface->x = shsurf->output->x;
474 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200475
Kristian Høgsberg75840622011-09-06 13:48:16 -0400476 wl_resource_post_event(resource,
477 DESKTOP_SHELL_CONFIGURE,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200478 wlsc_compositor_get_time(), 0, surface_resource,
479 shsurf->output->current->width,
480 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400481}
482
483static void
484desktop_shell_set_panel(struct wl_client *client,
485 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100486 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400487 struct wl_resource *surface_resource)
488{
489 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200490 struct shell_surface *shsurf = surface_resource->data;
491 struct wlsc_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200492 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400493
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100494 wl_list_for_each(priv, &shell->panels, link) {
495 if (priv->output == output_resource->data) {
496 priv->surface->output = NULL;
497 wl_list_remove(&priv->surface->link);
498 wl_list_remove(&priv->link);
499 break;
500 }
501 }
502
Pekka Paalanen068ae942011-11-28 14:11:15 +0200503 shsurf->type = SHELL_SURFACE_PANEL;
504 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100505
Pekka Paalanen068ae942011-11-28 14:11:15 +0200506 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100507
Pekka Paalanen068ae942011-11-28 14:11:15 +0200508 surface->x = shsurf->output->x;
509 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200510
Kristian Høgsberg75840622011-09-06 13:48:16 -0400511 wl_resource_post_event(resource,
512 DESKTOP_SHELL_CONFIGURE,
513 wlsc_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200514 shsurf->output->current->width,
515 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400516}
517
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200518static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500519handle_lock_surface_destroy(struct wl_listener *listener,
520 struct wl_resource *resource, uint32_t time)
521{
522 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200523 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500524
525 fprintf(stderr, "lock surface gone\n");
526 shell->lock_surface = NULL;
527}
528
529static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200530desktop_shell_set_lock_surface(struct wl_client *client,
531 struct wl_resource *resource,
532 struct wl_resource *surface_resource)
533{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200534 struct wl_shell *shell = resource->data;
535
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200536 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200537
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200538 if (!shell->locked)
539 return;
540
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500541 shell->lock_surface = surface_resource->data;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200542
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500543 shell->lock_surface_listener.func = handle_lock_surface_destroy;
544 wl_list_insert(&surface_resource->destroy_listener_list,
545 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200546
Pekka Paalanen068ae942011-11-28 14:11:15 +0200547 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200548}
549
550static void
551resume_desktop(struct wl_shell *shell)
552{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500553 struct wlsc_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200554 struct wl_list *list;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200555
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500556 wl_list_for_each(surface, &shell->hidden_surface_list, link)
557 wlsc_surface_configure(surface, surface->x, surface->y,
558 surface->width, surface->height);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200559
Pekka Paalanenfe340832011-11-25 16:07:52 +0200560 if (wl_list_empty(&shell->backgrounds)) {
561 list = &shell->compositor->surface_list;
562 } else {
563 struct shell_surface *background;
564 background = container_of(shell->backgrounds.prev,
565 struct shell_surface, link);
566 list = background->surface->link.prev;
567 }
568
569 if (!wl_list_empty(&shell->hidden_surface_list))
570 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500571 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200572
573 shell->locked = false;
574 wlsc_compositor_repick(shell->compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200575 wlsc_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200576}
577
578static void
579desktop_shell_unlock(struct wl_client *client,
580 struct wl_resource *resource)
581{
582 struct wl_shell *shell = resource->data;
583
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200584 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200585
586 if (shell->locked)
587 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200588}
589
Kristian Høgsberg75840622011-09-06 13:48:16 -0400590static const struct desktop_shell_interface desktop_shell_implementation = {
591 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200592 desktop_shell_set_panel,
593 desktop_shell_set_lock_surface,
594 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400595};
596
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200597static enum shell_surface_type
598get_shell_surface_type(struct wlsc_surface *surface)
599{
600 struct shell_surface *shsurf;
601
602 shsurf = get_shell_surface(surface);
603 if (!shsurf)
604 return SHELL_SURFACE_NORMAL;
605 return shsurf->type;
606}
607
Kristian Høgsberg75840622011-09-06 13:48:16 -0400608static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400609move_binding(struct wl_input_device *device, uint32_t time,
610 uint32_t key, uint32_t button, uint32_t state, void *data)
611{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400612 struct wlsc_surface *surface =
613 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500614
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100615 if (surface == NULL)
616 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400617
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200618 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100619 case SHELL_SURFACE_PANEL:
620 case SHELL_SURFACE_BACKGROUND:
621 case SHELL_SURFACE_FULLSCREEN:
622 return;
623 default:
624 break;
625 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400626
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400627 wlsc_surface_move(surface, (struct wlsc_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400628}
629
630static void
631resize_binding(struct wl_input_device *device, uint32_t time,
632 uint32_t key, uint32_t button, uint32_t state, void *data)
633{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400634 struct wlsc_surface *surface =
635 (struct wlsc_surface *) device->pointer_focus;
636 uint32_t edges = 0;
637 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200638 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500639
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100640 if (surface == NULL)
641 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200642
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200643 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200644 if (!shsurf)
645 return;
646
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200647 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100648 case SHELL_SURFACE_PANEL:
649 case SHELL_SURFACE_BACKGROUND:
650 case SHELL_SURFACE_FULLSCREEN:
651 return;
652 default:
653 break;
654 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400655
Kristian Høgsberg07937562011-04-12 17:25:42 -0400656 x = device->grab_x - surface->x;
657 y = device->grab_y - surface->y;
658
659 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200660 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400661 else if (x < 2 * surface->width / 3)
662 edges |= 0;
663 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200664 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400665
666 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200667 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400668 else if (y < 2 * surface->height / 3)
669 edges |= 0;
670 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200671 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400672
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200673 wlsc_surface_resize(shsurf, (struct wlsc_input_device *) device,
674 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400675}
676
677static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400678activate(struct wlsc_shell *base, struct wlsc_surface *es,
679 struct wlsc_input_device *device, uint32_t time)
680{
681 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
682 struct wlsc_compositor *compositor = shell->compositor;
683
684 wlsc_surface_activate(es, device, time);
685
Kristian Høgsbergd6e55252011-10-11 23:41:17 -0400686 if (compositor->wxs)
687 wlsc_xserver_surface_activate(es);
688
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200689 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200690 case SHELL_SURFACE_BACKGROUND:
691 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -0400692 wl_list_remove(&es->link);
693 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200694 break;
695 case SHELL_SURFACE_PANEL:
696 /* already put on top */
697 break;
698 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100699 if (!shell->locked) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200700 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100701 struct shell_surface *panel;
702 wl_list_for_each(panel, &shell->panels, link) {
703 wl_list_remove(&panel->surface->link);
704 wl_list_insert(&compositor->surface_list,
705 &panel->surface->link);
706 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200707 }
Kristian Høgsberg75840622011-09-06 13:48:16 -0400708 }
709}
710
711static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200712lock(struct wlsc_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400713{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200714 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200715 struct wl_list *surface_list = &shell->compositor->surface_list;
716 struct wlsc_surface *cur;
717 struct wlsc_surface *tmp;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200718 struct wlsc_input_device *device;
719 uint32_t time;
720
721 if (shell->locked)
722 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200723
724 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200725
726 /* Move all surfaces from compositor's list to our hidden list,
727 * except the background. This way nothing else can show or
728 * receive input events while we are locked. */
729
730 if (!wl_list_empty(&shell->hidden_surface_list)) {
731 fprintf(stderr,
732 "%s: Assertion failed: hidden_surface_list is not empty.\n",
733 __func__);
734 }
735
736 wl_list_for_each_safe(cur, tmp, surface_list, link) {
737 /* skip input device sprites, cur->surface is uninitialised */
738 if (cur->surface.resource.client == NULL)
739 continue;
740
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200741 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200742 continue;
743
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500744 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200745 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500746 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200747 }
748
749 /* reset pointer foci */
750 wlsc_compositor_repick(shell->compositor);
751
752 /* reset keyboard foci */
753 time = wlsc_compositor_get_time();
754 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
755 wl_input_device_set_keyboard_focus(&device->input_device,
756 NULL, time);
757 }
758
759 /* TODO: disable bindings that should not work while locked. */
760
761 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200762}
763
764static void
765unlock(struct wlsc_shell *base)
766{
767 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
768
Pekka Paalanend81c2162011-11-16 13:47:34 +0200769 if (!shell->locked || shell->lock_surface) {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200770 wlsc_compositor_wake(shell->compositor);
771 return;
772 }
773
774 /* If desktop-shell client has gone away, unlock immediately. */
775 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200776 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200777 return;
778 }
779
780 if (shell->prepare_event_sent)
781 return;
782
783 wl_resource_post_event(shell->child.desktop_shell,
784 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
785 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400786}
787
788static void
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500789map(struct wlsc_shell *base,
790 struct wlsc_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400791{
Kristian Høgsberg75840622011-09-06 13:48:16 -0400792 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
793 struct wlsc_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500794 struct wl_list *list;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200795 enum shell_surface_type surface_type;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200796
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200797 surface_type = get_shell_surface_type(surface);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500798
799 if (shell->locked)
800 list = &shell->hidden_surface_list;
801 else
802 list = &compositor->surface_list;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400803
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200804 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200805 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200806 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200807 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500808 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200809 break;
810 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200811 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500812 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200813 break;
814 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200815 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500816 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200817
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500818 wlsc_compositor_repick(compositor);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200819 wlsc_compositor_wake(compositor);
820 break;
821 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200822 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100823 if (!wl_list_empty(&shell->panels)) {
824 struct shell_surface *panel =
825 container_of(shell->panels.prev,
826 struct shell_surface, link);
827 wl_list_insert(&panel->surface->link, &surface->link);
828 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200829 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100830 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200831 }
832
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200833 if (surface_type == SHELL_SURFACE_TOPLEVEL) {
Kristian Høgsberg46770132011-11-09 12:38:53 -0500834 surface->x = 10 + random() % 400;
835 surface->y = 10 + random() % 400;
836 }
837
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500838 surface->width = width;
839 surface->height = height;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200840 if (!shell->locked || surface_type == SHELL_SURFACE_LOCK)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500841 wlsc_surface_configure(surface,
842 surface->x, surface->y, width, height);
Kristian Høgsberg2f88a402011-12-04 15:32:59 -0500843
844 if (surface_type == SHELL_SURFACE_TOPLEVEL)
845 wlsc_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500846}
847
848static void
849configure(struct wlsc_shell *shell, struct wlsc_surface *surface,
850 int32_t x, int32_t y, int32_t width, int32_t height)
851{
852 struct wlsc_mode *current;
853
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200854 switch (get_shell_surface_type(surface)) {
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500855 case SHELL_SURFACE_FULLSCREEN:
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500856 current = surface->fullscreen_output->current;
857 x = (current->width - surface->width) / 2;
858 y = (current->height - surface->height) / 2;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500859 break;
860 default:
861 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400862 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500863
864 wlsc_surface_configure(surface, x, y, width, height);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400865}
866
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400867static void
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200868desktop_shell_sigchld(struct wlsc_process *process, int status)
869{
870 struct wl_shell *shell =
871 container_of(process, struct wl_shell, child.process);
872
873 shell->child.process.pid = 0;
874 shell->child.client = NULL; /* already destroyed by wayland */
875}
876
877static int
878launch_desktop_shell_process(struct wl_shell *shell)
879{
Kristian Høgsbergc4693c42011-11-14 14:57:17 -0500880 const char *shell_exe = LIBEXECDIR "/wayland-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200881
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200882 shell->child.client = wlsc_client_launch(shell->compositor,
883 &shell->child.process,
884 shell_exe,
885 desktop_shell_sigchld);
886
887 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200888 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200889 return 0;
890}
891
892static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400893bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
894{
895 struct wl_shell *shell = data;
896
897 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200898 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400899}
900
Kristian Høgsberg75840622011-09-06 13:48:16 -0400901static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200902unbind_desktop_shell(struct wl_resource *resource)
903{
904 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500905
906 if (shell->locked)
907 resume_desktop(shell);
908
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200909 shell->child.desktop_shell = NULL;
910 shell->prepare_event_sent = false;
911 free(resource);
912}
913
914static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400915bind_desktop_shell(struct wl_client *client,
916 void *data, uint32_t version, uint32_t id)
917{
918 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +0200919 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400920
Pekka Paalanenbbe60522011-11-03 14:11:33 +0200921 resource = wl_client_add_object(client, &desktop_shell_interface,
922 &desktop_shell_implementation,
923 id, shell);
924
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200925 if (client == shell->child.client) {
926 resource->destroy = unbind_desktop_shell;
927 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +0200928 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200929 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +0200930
931 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
932 "permission to bind desktop_shell denied");
933 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400934}
935
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400936int
937shell_init(struct wlsc_compositor *ec);
938
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400939WL_EXPORT int
940shell_init(struct wlsc_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500941{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400942 struct wl_shell *shell;
943
944 shell = malloc(sizeof *shell);
945 if (shell == NULL)
946 return -1;
947
Kristian Høgsbergf0d91162011-10-11 22:44:23 -0400948 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400949 shell->compositor = ec;
950 shell->shell.activate = activate;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400951 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200952 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500953 shell->shell.map = map;
954 shell->shell.configure = configure;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500955
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200956 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100957 wl_list_init(&shell->backgrounds);
958 wl_list_init(&shell->panels);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200959
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400960 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
961 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500962 return -1;
963
Kristian Høgsberg75840622011-09-06 13:48:16 -0400964 if (wl_display_add_global(ec->wl_display,
965 &desktop_shell_interface,
966 shell, bind_desktop_shell) == NULL)
967 return -1;
968
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200969 if (launch_desktop_shell_process(shell) != 0)
970 return -1;
971
Kristian Høgsberg07937562011-04-12 17:25:42 -0400972 wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400973 move_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400974 wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400975 resize_binding, shell);
976
977 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400978
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500979 return 0;
980}