blob: fd2d4fcabeeddf26931483d45456f56d3735c78f [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 Paalanen6cd281a2011-11-03 14:11:32 +020030#include <sys/types.h>
31#include <sys/socket.h>
32#include <fcntl.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020033#include <assert.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050034
Pekka Paalanen50719bc2011-11-22 14:18:50 +020035#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040037#include "desktop-shell-server-protocol.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øgsberg75840622011-09-06 13:48:16 -040042 struct wlsc_compositor *compositor;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040043 struct wlsc_shell shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020044
45 struct {
46 struct wlsc_process process;
47 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020048 struct wl_resource *desktop_shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020049 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020050
51 bool locked;
52 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020053
Pekka Paalanen068ae942011-11-28 14:11:15 +020054 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050055 struct wl_listener lock_surface_listener;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020056 struct wl_list hidden_surface_list;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010057
58 struct wl_list backgrounds;
59 struct wl_list panels;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040060};
61
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050062enum shell_surface_type {
Pekka Paalanen57da4a82011-11-23 16:42:16 +020063 SHELL_SURFACE_NORMAL,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050064
Pekka Paalanen57da4a82011-11-23 16:42:16 +020065 SHELL_SURFACE_PANEL,
66 SHELL_SURFACE_BACKGROUND,
67 SHELL_SURFACE_LOCK,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050068
69 SHELL_SURFACE_TOPLEVEL,
70 SHELL_SURFACE_TRANSIENT,
71 SHELL_SURFACE_FULLSCREEN
Pekka Paalanen57da4a82011-11-23 16:42:16 +020072};
73
Pekka Paalanen56cdea92011-11-23 16:14:12 +020074struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020075 struct wl_resource resource;
76
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010077 struct wlsc_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020078 struct wl_listener surface_destroy_listener;
Pekka Paalanen57da4a82011-11-23 16:42:16 +020079
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050080 enum shell_surface_type type;
81 int32_t saved_x, saved_y;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010082
83 struct wlsc_output *output;
84 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +020085};
86
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050087struct wlsc_move_grab {
88 struct wl_grab grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050089 struct wlsc_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050090 int32_t dx, dy;
91};
92
Pekka Paalanen56cdea92011-11-23 16:14:12 +020093static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050094move_grab_motion(struct wl_grab *grab,
95 uint32_t time, int32_t x, int32_t y)
96{
97 struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050098 struct wlsc_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050099
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400100 wlsc_surface_configure(es, x + move->dx, y + move->dy,
101 es->width, es->height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500102}
103
104static void
105move_grab_button(struct wl_grab *grab,
106 uint32_t time, int32_t button, int32_t state)
107{
108}
109
110static void
111move_grab_end(struct wl_grab *grab, uint32_t time)
112{
113 free(grab);
114}
115
116static const struct wl_grab_interface move_grab_interface = {
117 move_grab_motion,
118 move_grab_button,
119 move_grab_end
120};
121
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400122static int
123wlsc_surface_move(struct wlsc_surface *es,
124 struct wlsc_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500125{
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500126 struct wlsc_move_grab *move;
127
128 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400129 if (!move)
130 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500131
132 move->grab.interface = &move_grab_interface;
133 move->dx = es->x - wd->input_device.grab_x;
134 move->dy = es->y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500135 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500136
137 if (wl_input_device_update_grab(&wd->input_device,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400138 &move->grab, &es->surface, time) < 0)
139 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500140
141 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400142 wl_input_device_set_pointer_focus(&wd->input_device,
143 NULL, time, 0, 0, 0, 0);
144
145 return 0;
146}
147
148static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200149shell_surface_move(struct wl_client *client, struct wl_resource *resource,
150 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400151{
152 struct wlsc_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200153 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400154
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200155 if (wlsc_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400156 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500157}
158
159struct wlsc_resize_grab {
160 struct wl_grab grab;
161 uint32_t edges;
162 int32_t dx, dy, width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200163 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500164};
165
166static void
167resize_grab_motion(struct wl_grab *grab,
168 uint32_t time, int32_t x, int32_t y)
169{
170 struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab;
171 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500172 int32_t width, height;
173
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200174 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500175 width = device->grab_x - x + resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200176 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500177 width = x - device->grab_x + resize->width;
178 } else {
179 width = resize->width;
180 }
181
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200182 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500183 height = device->grab_y - y + resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200184 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500185 height = y - device->grab_y + resize->height;
186 } else {
187 height = resize->height;
188 }
189
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200190 wl_resource_post_event(&resize->shsurf->resource,
191 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
192 width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500193}
194
195static void
196resize_grab_button(struct wl_grab *grab,
197 uint32_t time, int32_t button, int32_t state)
198{
199}
200
201static void
202resize_grab_end(struct wl_grab *grab, uint32_t time)
203{
204 free(grab);
205}
206
207static const struct wl_grab_interface resize_grab_interface = {
208 resize_grab_motion,
209 resize_grab_button,
210 resize_grab_end
211};
212
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400213static int
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200214wlsc_surface_resize(struct shell_surface *shsurf,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400215 struct wlsc_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200216 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500217{
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500218 struct wlsc_resize_grab *resize;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200219 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500220 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500221
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500222 /* FIXME: Reject if fullscreen */
223
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500224 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400225 if (!resize)
226 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500227
228 resize->grab.interface = &resize_grab_interface;
229 resize->edges = edges;
230 resize->dx = es->x - wd->input_device.grab_x;
231 resize->dy = es->y - wd->input_device.grab_y;
232 resize->width = es->width;
233 resize->height = es->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200234 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400235
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500236 if (edges == 0 || edges > 15 ||
237 (edges & 3) == 3 || (edges & 12) == 12)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400238 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500239
240 switch (edges) {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200241 case WL_SHELL_SURFACE_RESIZE_TOP:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500242 pointer = WLSC_POINTER_TOP;
243 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200244 case WL_SHELL_SURFACE_RESIZE_BOTTOM:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500245 pointer = WLSC_POINTER_BOTTOM;
246 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200247 case WL_SHELL_SURFACE_RESIZE_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500248 pointer = WLSC_POINTER_LEFT;
249 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200250 case WL_SHELL_SURFACE_RESIZE_TOP_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500251 pointer = WLSC_POINTER_TOP_LEFT;
252 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200253 case WL_SHELL_SURFACE_RESIZE_BOTTOM_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500254 pointer = WLSC_POINTER_BOTTOM_LEFT;
255 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200256 case WL_SHELL_SURFACE_RESIZE_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500257 pointer = WLSC_POINTER_RIGHT;
258 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200259 case WL_SHELL_SURFACE_RESIZE_TOP_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500260 pointer = WLSC_POINTER_TOP_RIGHT;
261 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200262 case WL_SHELL_SURFACE_RESIZE_BOTTOM_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500263 pointer = WLSC_POINTER_BOTTOM_RIGHT;
264 break;
265 }
266
267 if (wl_input_device_update_grab(&wd->input_device,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400268 &resize->grab, &es->surface, time) < 0)
269 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500270
271 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400272 wl_input_device_set_pointer_focus(&wd->input_device,
273 NULL, time, 0, 0, 0, 0);
274
275 return 0;
276}
277
278static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200279shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
280 struct wl_resource *input_resource, uint32_t time,
281 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400282{
283 struct wlsc_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200284 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400285
286 /* FIXME: Reject if fullscreen */
287
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200288 if (wlsc_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400289 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500290}
291
292static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200293shell_surface_set_toplevel(struct wl_client *client,
294 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400295
296{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200297 struct shell_surface *shsurf = resource->data;
298 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400299
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200300 if (shsurf->type == SHELL_SURFACE_FULLSCREEN) {
301 es->x = shsurf->saved_x;
302 es->y = shsurf->saved_y;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400303 }
304
305 wlsc_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200306 shsurf->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400307 es->fullscreen_output = NULL;
308}
309
310static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200311shell_surface_set_transient(struct wl_client *client,
312 struct wl_resource *resource,
313 struct wl_resource *parent_resource,
314 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400315{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200316 struct shell_surface *shsurf = resource->data;
317 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400318 struct wlsc_surface *pes = parent_resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400319
320 /* assign to parents output */
321 es->output = pes->output;
322
323 es->x = pes->x + x;
324 es->y = pes->y + y;
325
326 wlsc_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200327 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400328}
329
330static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200331shell_surface_set_fullscreen(struct wl_client *client,
332 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400333
334{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200335 struct shell_surface *shsurf = resource->data;
336 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400337 struct wlsc_output *output;
338
339 /* FIXME: Fullscreen on first output */
340 /* FIXME: Handle output going away */
341 output = container_of(es->compositor->output_list.next,
342 struct wlsc_output, link);
343 es->output = output;
344
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200345 shsurf->saved_x = es->x;
346 shsurf->saved_y = es->y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400347 es->x = (output->current->width - es->width) / 2;
348 es->y = (output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400349 es->fullscreen_output = output;
350 wlsc_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200351 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400352}
353
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200354static const struct wl_shell_surface_interface shell_surface_implementation = {
355 shell_surface_move,
356 shell_surface_resize,
357 shell_surface_set_toplevel,
358 shell_surface_set_transient,
359 shell_surface_set_fullscreen
360};
361
362static void
363destroy_shell_surface(struct wl_resource *resource)
364{
365 struct shell_surface *shsurf = resource->data;
366
367 /* in case cleaning up a dead client destroys shell_surface first */
368 if (shsurf->surface)
369 wl_list_remove(&shsurf->surface_destroy_listener.link);
370
371 wl_list_remove(&shsurf->link);
372 free(shsurf);
373}
374
375static void
376shell_handle_surface_destroy(struct wl_listener *listener,
377 struct wl_resource *resource, uint32_t time)
378{
379 struct shell_surface *shsurf = container_of(listener,
380 struct shell_surface,
381 surface_destroy_listener);
382
383 shsurf->surface = NULL;
384 wl_resource_destroy(&shsurf->resource, time);
385}
386
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200387static struct shell_surface *
388get_shell_surface(struct wlsc_surface *surface)
389{
390 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
391 struct wl_listener *listener;
392
393 /* search the destroy listener list for our callback */
394 wl_list_for_each(listener, lst, link) {
395 if (listener->func == shell_handle_surface_destroy) {
396 return container_of(listener, struct shell_surface,
397 surface_destroy_listener);
398 }
399 }
400
401 return NULL;
402}
403
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200404static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200405shell_get_shell_surface(struct wl_client *client,
406 struct wl_resource *resource,
407 uint32_t id,
408 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200409{
410 struct wlsc_surface *surface = surface_resource->data;
411 struct shell_surface *shsurf;
412
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200413 if (get_shell_surface(surface)) {
414 wl_resource_post_error(surface_resource,
415 WL_DISPLAY_ERROR_INVALID_OBJECT,
416 "wl_shell::get_shell_surface already requested");
417 return;
418 }
419
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200420 shsurf = calloc(1, sizeof *shsurf);
421 if (!shsurf) {
422 wl_resource_post_no_memory(resource);
423 return;
424 }
425
426 shsurf->resource.destroy = destroy_shell_surface;
427 shsurf->resource.object.id = id;
428 shsurf->resource.object.interface = &wl_shell_surface_interface;
429 shsurf->resource.object.implementation =
430 (void (**)(void)) &shell_surface_implementation;
431 shsurf->resource.data = shsurf;
432
433 shsurf->surface = surface;
434 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
435 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
436 &shsurf->surface_destroy_listener.link);
437
438 /* init link so its safe to always remove it in destroy_shell_surface */
439 wl_list_init(&shsurf->link);
440
441 shsurf->type = SHELL_SURFACE_NORMAL;
442
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200443 wl_client_add_resource(client, &shsurf->resource);
444}
445
446static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200447 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500448};
449
Kristian Høgsberg07937562011-04-12 17:25:42 -0400450static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400451desktop_shell_set_background(struct wl_client *client,
452 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100453 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400454 struct wl_resource *surface_resource)
455{
456 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200457 struct shell_surface *shsurf = surface_resource->data;
458 struct wlsc_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200459 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400460
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100461 wl_list_for_each(priv, &shell->backgrounds, link) {
462 if (priv->output == output_resource->data) {
463 priv->surface->output = NULL;
464 wl_list_remove(&priv->surface->link);
465 wl_list_remove(&priv->link);
466 break;
467 }
468 }
469
Pekka Paalanen068ae942011-11-28 14:11:15 +0200470 shsurf->type = SHELL_SURFACE_BACKGROUND;
471 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100472
Pekka Paalanen068ae942011-11-28 14:11:15 +0200473 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100474
Pekka Paalanen068ae942011-11-28 14:11:15 +0200475 surface->x = shsurf->output->x;
476 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200477
Kristian Høgsberg75840622011-09-06 13:48:16 -0400478 wl_resource_post_event(resource,
479 DESKTOP_SHELL_CONFIGURE,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200480 wlsc_compositor_get_time(), 0, surface_resource,
481 shsurf->output->current->width,
482 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400483}
484
485static void
486desktop_shell_set_panel(struct wl_client *client,
487 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100488 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400489 struct wl_resource *surface_resource)
490{
491 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200492 struct shell_surface *shsurf = surface_resource->data;
493 struct wlsc_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200494 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400495
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100496 wl_list_for_each(priv, &shell->panels, link) {
497 if (priv->output == output_resource->data) {
498 priv->surface->output = NULL;
499 wl_list_remove(&priv->surface->link);
500 wl_list_remove(&priv->link);
501 break;
502 }
503 }
504
Pekka Paalanen068ae942011-11-28 14:11:15 +0200505 shsurf->type = SHELL_SURFACE_PANEL;
506 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100507
Pekka Paalanen068ae942011-11-28 14:11:15 +0200508 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100509
Pekka Paalanen068ae942011-11-28 14:11:15 +0200510 surface->x = shsurf->output->x;
511 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200512
Kristian Høgsberg75840622011-09-06 13:48:16 -0400513 wl_resource_post_event(resource,
514 DESKTOP_SHELL_CONFIGURE,
515 wlsc_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200516 shsurf->output->current->width,
517 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400518}
519
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200520static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500521handle_lock_surface_destroy(struct wl_listener *listener,
522 struct wl_resource *resource, uint32_t time)
523{
524 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200525 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500526
527 fprintf(stderr, "lock surface gone\n");
528 shell->lock_surface = NULL;
529}
530
531static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200532desktop_shell_set_lock_surface(struct wl_client *client,
533 struct wl_resource *resource,
534 struct wl_resource *surface_resource)
535{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200536 struct wl_shell *shell = resource->data;
537
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200538 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200539
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200540 if (!shell->locked)
541 return;
542
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500543 shell->lock_surface = surface_resource->data;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200544
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500545 shell->lock_surface_listener.func = handle_lock_surface_destroy;
546 wl_list_insert(&surface_resource->destroy_listener_list,
547 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200548
Pekka Paalanen068ae942011-11-28 14:11:15 +0200549 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200550}
551
552static void
553resume_desktop(struct wl_shell *shell)
554{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500555 struct wlsc_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200556 struct wl_list *list;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200557
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500558 wl_list_for_each(surface, &shell->hidden_surface_list, link)
559 wlsc_surface_configure(surface, surface->x, surface->y,
560 surface->width, surface->height);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200561
Pekka Paalanenfe340832011-11-25 16:07:52 +0200562 if (wl_list_empty(&shell->backgrounds)) {
563 list = &shell->compositor->surface_list;
564 } else {
565 struct shell_surface *background;
566 background = container_of(shell->backgrounds.prev,
567 struct shell_surface, link);
568 list = background->surface->link.prev;
569 }
570
571 if (!wl_list_empty(&shell->hidden_surface_list))
572 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500573 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200574
575 shell->locked = false;
576 wlsc_compositor_repick(shell->compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200577 wlsc_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200578}
579
580static void
581desktop_shell_unlock(struct wl_client *client,
582 struct wl_resource *resource)
583{
584 struct wl_shell *shell = resource->data;
585
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200586 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200587
588 if (shell->locked)
589 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200590}
591
Kristian Høgsberg75840622011-09-06 13:48:16 -0400592static const struct desktop_shell_interface desktop_shell_implementation = {
593 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200594 desktop_shell_set_panel,
595 desktop_shell_set_lock_surface,
596 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400597};
598
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200599static enum shell_surface_type
600get_shell_surface_type(struct wlsc_surface *surface)
601{
602 struct shell_surface *shsurf;
603
604 shsurf = get_shell_surface(surface);
605 if (!shsurf)
606 return SHELL_SURFACE_NORMAL;
607 return shsurf->type;
608}
609
Kristian Høgsberg75840622011-09-06 13:48:16 -0400610static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400611move_binding(struct wl_input_device *device, uint32_t time,
612 uint32_t key, uint32_t button, uint32_t state, void *data)
613{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400614 struct wlsc_surface *surface =
615 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500616
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100617 if (surface == NULL)
618 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400619
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200620 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100621 case SHELL_SURFACE_PANEL:
622 case SHELL_SURFACE_BACKGROUND:
623 case SHELL_SURFACE_FULLSCREEN:
624 return;
625 default:
626 break;
627 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400628
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400629 wlsc_surface_move(surface, (struct wlsc_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400630}
631
632static void
633resize_binding(struct wl_input_device *device, uint32_t time,
634 uint32_t key, uint32_t button, uint32_t state, void *data)
635{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400636 struct wlsc_surface *surface =
637 (struct wlsc_surface *) device->pointer_focus;
638 uint32_t edges = 0;
639 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200640 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500641
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100642 if (surface == NULL)
643 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200644
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200645 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200646 if (!shsurf)
647 return;
648
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200649 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100650 case SHELL_SURFACE_PANEL:
651 case SHELL_SURFACE_BACKGROUND:
652 case SHELL_SURFACE_FULLSCREEN:
653 return;
654 default:
655 break;
656 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400657
Kristian Høgsberg07937562011-04-12 17:25:42 -0400658 x = device->grab_x - surface->x;
659 y = device->grab_y - surface->y;
660
661 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200662 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400663 else if (x < 2 * surface->width / 3)
664 edges |= 0;
665 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200666 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400667
668 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200669 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400670 else if (y < 2 * surface->height / 3)
671 edges |= 0;
672 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200673 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400674
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200675 wlsc_surface_resize(shsurf, (struct wlsc_input_device *) device,
676 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400677}
678
679static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400680activate(struct wlsc_shell *base, struct wlsc_surface *es,
681 struct wlsc_input_device *device, uint32_t time)
682{
683 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
684 struct wlsc_compositor *compositor = shell->compositor;
685
686 wlsc_surface_activate(es, device, time);
687
Kristian Høgsbergd6e55252011-10-11 23:41:17 -0400688 if (compositor->wxs)
689 wlsc_xserver_surface_activate(es);
690
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200691 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200692 case SHELL_SURFACE_BACKGROUND:
693 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -0400694 wl_list_remove(&es->link);
695 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200696 break;
697 case SHELL_SURFACE_PANEL:
698 /* already put on top */
699 break;
700 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100701 if (!shell->locked) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200702 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100703 struct shell_surface *panel;
704 wl_list_for_each(panel, &shell->panels, link) {
705 wl_list_remove(&panel->surface->link);
706 wl_list_insert(&compositor->surface_list,
707 &panel->surface->link);
708 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200709 }
Kristian Høgsberg75840622011-09-06 13:48:16 -0400710 }
711}
712
713static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200714lock(struct wlsc_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400715{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200716 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200717 struct wl_list *surface_list = &shell->compositor->surface_list;
718 struct wlsc_surface *cur;
719 struct wlsc_surface *tmp;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200720 struct wlsc_input_device *device;
721 uint32_t time;
722
723 if (shell->locked)
724 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200725
726 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200727
728 /* Move all surfaces from compositor's list to our hidden list,
729 * except the background. This way nothing else can show or
730 * receive input events while we are locked. */
731
732 if (!wl_list_empty(&shell->hidden_surface_list)) {
733 fprintf(stderr,
734 "%s: Assertion failed: hidden_surface_list is not empty.\n",
735 __func__);
736 }
737
738 wl_list_for_each_safe(cur, tmp, surface_list, link) {
739 /* skip input device sprites, cur->surface is uninitialised */
740 if (cur->surface.resource.client == NULL)
741 continue;
742
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200743 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200744 continue;
745
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500746 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200747 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500748 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200749 }
750
751 /* reset pointer foci */
752 wlsc_compositor_repick(shell->compositor);
753
754 /* reset keyboard foci */
755 time = wlsc_compositor_get_time();
756 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
757 wl_input_device_set_keyboard_focus(&device->input_device,
758 NULL, time);
759 }
760
761 /* TODO: disable bindings that should not work while locked. */
762
763 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200764}
765
766static void
767unlock(struct wlsc_shell *base)
768{
769 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
770
Pekka Paalanend81c2162011-11-16 13:47:34 +0200771 if (!shell->locked || shell->lock_surface) {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200772 wlsc_compositor_wake(shell->compositor);
773 return;
774 }
775
776 /* If desktop-shell client has gone away, unlock immediately. */
777 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200778 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200779 return;
780 }
781
782 if (shell->prepare_event_sent)
783 return;
784
785 wl_resource_post_event(shell->child.desktop_shell,
786 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
787 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400788}
789
790static void
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500791map(struct wlsc_shell *base,
792 struct wlsc_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400793{
Kristian Høgsberg75840622011-09-06 13:48:16 -0400794 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
795 struct wlsc_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500796 struct wl_list *list;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200797 enum shell_surface_type surface_type;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200798
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200799 surface_type = get_shell_surface_type(surface);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500800
801 if (shell->locked)
802 list = &shell->hidden_surface_list;
803 else
804 list = &compositor->surface_list;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400805
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200806 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200807 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200808 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200809 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500810 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200811 break;
812 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200813 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500814 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200815 break;
816 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200817 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500818 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200819
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500820 wlsc_compositor_repick(compositor);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200821 wlsc_compositor_wake(compositor);
822 break;
823 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200824 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100825 if (!wl_list_empty(&shell->panels)) {
826 struct shell_surface *panel =
827 container_of(shell->panels.prev,
828 struct shell_surface, link);
829 wl_list_insert(&panel->surface->link, &surface->link);
830 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200831 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100832 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200833 }
834
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200835 if (surface_type == SHELL_SURFACE_TOPLEVEL) {
Kristian Høgsberg46770132011-11-09 12:38:53 -0500836 surface->x = 10 + random() % 400;
837 surface->y = 10 + random() % 400;
838 }
839
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500840 surface->width = width;
841 surface->height = height;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200842 if (!shell->locked || surface_type == SHELL_SURFACE_LOCK)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500843 wlsc_surface_configure(surface,
844 surface->x, surface->y, width, height);
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 struct wlsc_compositor *compositor = shell->compositor;
881 char s[32];
882 int sv[2], flags;
883
884 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
885 fprintf(stderr, "socketpair failed\n");
886 return -1;
887 }
888
889 shell->child.process.pid = fork();
890 shell->child.process.cleanup = desktop_shell_sigchld;
891
892 switch (shell->child.process.pid) {
893 case 0:
894 /* SOCK_CLOEXEC closes both ends, so we need to unset
895 * the flag on the client fd. */
896 flags = fcntl(sv[1], F_GETFD);
897 if (flags != -1)
898 fcntl(sv[1], F_SETFD, flags & ~FD_CLOEXEC);
899
900 snprintf(s, sizeof s, "%d", sv[1]);
901 setenv("WAYLAND_SOCKET", s, 1);
902 if (execl(shell_exe, shell_exe, NULL) < 0)
903 fprintf(stderr, "%s: running '%s' failed: %m\n",
904 __func__, shell_exe);
905 exit(-1);
906
907 default:
908 close(sv[1]);
909 shell->child.client =
910 wl_client_create(compositor->wl_display, sv[0]);
911 wlsc_watch_process(&shell->child.process);
912 break;
913
914 case -1:
915 fprintf(stderr, "%s: fork failed: %m\n", __func__);
916 return -1;
917 }
918 return 0;
919}
920
921static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400922bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
923{
924 struct wl_shell *shell = data;
925
926 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200927 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400928}
929
Kristian Høgsberg75840622011-09-06 13:48:16 -0400930static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200931unbind_desktop_shell(struct wl_resource *resource)
932{
933 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500934
935 if (shell->locked)
936 resume_desktop(shell);
937
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200938 shell->child.desktop_shell = NULL;
939 shell->prepare_event_sent = false;
940 free(resource);
941}
942
943static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400944bind_desktop_shell(struct wl_client *client,
945 void *data, uint32_t version, uint32_t id)
946{
947 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +0200948 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400949
Pekka Paalanenbbe60522011-11-03 14:11:33 +0200950 resource = wl_client_add_object(client, &desktop_shell_interface,
951 &desktop_shell_implementation,
952 id, shell);
953
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200954 if (client == shell->child.client) {
955 resource->destroy = unbind_desktop_shell;
956 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +0200957 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200958 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +0200959
960 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
961 "permission to bind desktop_shell denied");
962 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400963}
964
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400965int
966shell_init(struct wlsc_compositor *ec);
967
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400968WL_EXPORT int
969shell_init(struct wlsc_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500970{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400971 struct wl_shell *shell;
972
973 shell = malloc(sizeof *shell);
974 if (shell == NULL)
975 return -1;
976
Kristian Høgsbergf0d91162011-10-11 22:44:23 -0400977 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400978 shell->compositor = ec;
979 shell->shell.activate = activate;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400980 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200981 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500982 shell->shell.map = map;
983 shell->shell.configure = configure;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500984
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200985 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100986 wl_list_init(&shell->backgrounds);
987 wl_list_init(&shell->panels);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200988
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400989 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
990 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500991 return -1;
992
Kristian Høgsberg75840622011-09-06 13:48:16 -0400993 if (wl_display_add_global(ec->wl_display,
994 &desktop_shell_interface,
995 shell, bind_desktop_shell) == NULL)
996 return -1;
997
Pekka Paalanen6cd281a2011-11-03 14:11:32 +0200998 if (launch_desktop_shell_process(shell) != 0)
999 return -1;
1000
Kristian Høgsberg07937562011-04-12 17:25:42 -04001001 wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001002 move_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001003 wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001004 resize_binding, shell);
1005
1006 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001007
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001008 return 0;
1009}