blob: f4942a2d4aafedbf933012f2d48ac2df29016a28 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040026#include <linux/input.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027
28#include "wayland-server.h"
29#include "compositor.h"
30
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040031struct wl_shell {
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040032 struct wlsc_shell shell;
33};
34
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050035struct wlsc_move_grab {
36 struct wl_grab grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050037 struct wlsc_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038 int32_t dx, dy;
39};
40
41static void
42move_grab_motion(struct wl_grab *grab,
43 uint32_t time, int32_t x, int32_t y)
44{
45 struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050046 struct wlsc_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050047
Kristian Høgsberga691aee2011-06-23 21:43:50 -040048 wlsc_surface_configure(es, x + move->dx, y + move->dy,
49 es->width, es->height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050050}
51
52static void
53move_grab_button(struct wl_grab *grab,
54 uint32_t time, int32_t button, int32_t state)
55{
56}
57
58static void
59move_grab_end(struct wl_grab *grab, uint32_t time)
60{
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050061 struct wlsc_surface *es;
62 struct wl_input_device *device = grab->input_device;
63 int32_t sx, sy;
64
65 es = pick_surface(grab->input_device, &sx, &sy);
66 wl_input_device_set_pointer_focus(device,
67 &es->surface, time,
68 device->x, device->y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050069 free(grab);
70}
71
72static const struct wl_grab_interface move_grab_interface = {
73 move_grab_motion,
74 move_grab_button,
75 move_grab_end
76};
77
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040078static int
79wlsc_surface_move(struct wlsc_surface *es,
80 struct wlsc_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050081{
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050082 struct wlsc_move_grab *move;
83
Kristian Høgsberg0ce24572011-01-28 15:18:33 -050084 /* FIXME: Reject if fullscreen */
85
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050086 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040087 if (!move)
88 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050089
90 move->grab.interface = &move_grab_interface;
91 move->dx = es->x - wd->input_device.grab_x;
92 move->dy = es->y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050093 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050094
95 if (wl_input_device_update_grab(&wd->input_device,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040096 &move->grab, &es->surface, time) < 0)
97 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050098
99 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400100 wl_input_device_set_pointer_focus(&wd->input_device,
101 NULL, time, 0, 0, 0, 0);
102
103 return 0;
104}
105
106static void
107shell_move(struct wl_client *client, struct wl_resource *resource,
108 struct wl_resource *surface_resource,
109 struct wl_resource *input_resource, uint32_t time)
110{
111 struct wlsc_input_device *wd = input_resource->data;
112 struct wlsc_surface *es = surface_resource->data;
113
114 if (wlsc_surface_move(es, wd, time) < 0)
115 wl_client_post_no_memory(client);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500116}
117
118struct wlsc_resize_grab {
119 struct wl_grab grab;
120 uint32_t edges;
121 int32_t dx, dy, width, height;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500122 struct wlsc_surface *surface;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400123 struct wl_resource *resource;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500124};
125
126static void
127resize_grab_motion(struct wl_grab *grab,
128 uint32_t time, int32_t x, int32_t y)
129{
130 struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab;
131 struct wl_input_device *device = grab->input_device;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500132 struct wl_surface *surface = &resize->surface->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500133 int32_t width, height;
134
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500135 if (resize->edges & WL_SHELL_RESIZE_LEFT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500136 width = device->grab_x - x + resize->width;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500137 } else if (resize->edges & WL_SHELL_RESIZE_RIGHT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500138 width = x - device->grab_x + resize->width;
139 } else {
140 width = resize->width;
141 }
142
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500143 if (resize->edges & WL_SHELL_RESIZE_TOP) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500144 height = device->grab_y - y + resize->height;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500145 } else if (resize->edges & WL_SHELL_RESIZE_BOTTOM) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500146 height = y - device->grab_y + resize->height;
147 } else {
148 height = resize->height;
149 }
150
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400151 wl_resource_post_event(resize->resource,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400152 WL_SHELL_CONFIGURE, time, resize->edges,
153 surface, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500154}
155
156static void
157resize_grab_button(struct wl_grab *grab,
158 uint32_t time, int32_t button, int32_t state)
159{
160}
161
162static void
163resize_grab_end(struct wl_grab *grab, uint32_t time)
164{
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500165 struct wlsc_surface *es;
166 struct wl_input_device *device = grab->input_device;
167 int32_t sx, sy;
168
169 es = pick_surface(grab->input_device, &sx, &sy);
170 wl_input_device_set_pointer_focus(device,
171 &es->surface, time,
172 device->x, device->y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500173 free(grab);
174}
175
176static const struct wl_grab_interface resize_grab_interface = {
177 resize_grab_motion,
178 resize_grab_button,
179 resize_grab_end
180};
181
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400182static int
183wlsc_surface_resize(struct wlsc_surface *es,
184 struct wlsc_input_device *wd,
185 uint32_t time, uint32_t edges,
186 struct wl_resource *resource)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500187{
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500188 struct wlsc_resize_grab *resize;
189 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500190
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500191 /* FIXME: Reject if fullscreen */
192
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500193 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400194 if (!resize)
195 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500196
197 resize->grab.interface = &resize_grab_interface;
198 resize->edges = edges;
199 resize->dx = es->x - wd->input_device.grab_x;
200 resize->dy = es->y - wd->input_device.grab_y;
201 resize->width = es->width;
202 resize->height = es->height;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500203 resize->surface = es;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400204 resize->resource = resource;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400205
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500206 if (edges == 0 || edges > 15 ||
207 (edges & 3) == 3 || (edges & 12) == 12)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400208 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500209
210 switch (edges) {
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500211 case WL_SHELL_RESIZE_TOP:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500212 pointer = WLSC_POINTER_TOP;
213 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500214 case WL_SHELL_RESIZE_BOTTOM:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500215 pointer = WLSC_POINTER_BOTTOM;
216 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500217 case WL_SHELL_RESIZE_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500218 pointer = WLSC_POINTER_LEFT;
219 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500220 case WL_SHELL_RESIZE_TOP_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500221 pointer = WLSC_POINTER_TOP_LEFT;
222 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500223 case WL_SHELL_RESIZE_BOTTOM_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500224 pointer = WLSC_POINTER_BOTTOM_LEFT;
225 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500226 case WL_SHELL_RESIZE_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500227 pointer = WLSC_POINTER_RIGHT;
228 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500229 case WL_SHELL_RESIZE_TOP_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500230 pointer = WLSC_POINTER_TOP_RIGHT;
231 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500232 case WL_SHELL_RESIZE_BOTTOM_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500233 pointer = WLSC_POINTER_BOTTOM_RIGHT;
234 break;
235 }
236
237 if (wl_input_device_update_grab(&wd->input_device,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400238 &resize->grab, &es->surface, time) < 0)
239 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500240
241 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400242 wl_input_device_set_pointer_focus(&wd->input_device,
243 NULL, time, 0, 0, 0, 0);
244
245 return 0;
246}
247
248static void
249shell_resize(struct wl_client *client, struct wl_resource *resource,
250 struct wl_resource *surface_resource,
251 struct wl_resource *input_resource, uint32_t time, uint32_t edges)
252{
253 struct wlsc_input_device *wd = input_resource->data;
254 struct wlsc_surface *es = surface_resource->data;
255
256 /* FIXME: Reject if fullscreen */
257
258 if (wlsc_surface_resize(es, wd, time, edges, resource) < 0)
259 wl_client_post_no_memory(client);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500260}
261
262static void
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400263shell_set_toplevel(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400264 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400265 struct wl_resource *surface_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400266
267{
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400268 struct wlsc_surface *es = surface_resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400269 struct wlsc_compositor *ec = es->compositor;
270
271 if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN) {
272 es->x = es->saved_x;
273 es->y = es->saved_y;
274 } else if (es->map_type == WLSC_SURFACE_MAP_UNMAPPED) {
275 es->x = 10 + random() % 400;
276 es->y = 10 + random() % 400;
277 /* assign to first output */
278 es->output = container_of(ec->output_list.next,
279 struct wlsc_output, link);
280 }
281
282 wlsc_surface_damage(es);
283 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
284 es->fullscreen_output = NULL;
285}
286
287static void
288shell_set_transient(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400289 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400290 struct wl_resource *surface_resource,
291 struct wl_resource *parent_resource,
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400292 int x, int y, uint32_t flags)
293{
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400294 struct wlsc_surface *es = surface_resource->data;
295 struct wlsc_surface *pes = parent_resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400296
297 /* assign to parents output */
298 es->output = pes->output;
299
300 es->x = pes->x + x;
301 es->y = pes->y + y;
302
303 wlsc_surface_damage(es);
304 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
305}
306
307static void
308shell_set_fullscreen(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400309 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400310 struct wl_resource *surface_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400311
312{
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400313 struct wlsc_surface *es = surface_resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400314 struct wlsc_output *output;
315
316 /* FIXME: Fullscreen on first output */
317 /* FIXME: Handle output going away */
318 output = container_of(es->compositor->output_list.next,
319 struct wlsc_output, link);
320 es->output = output;
321
322 es->saved_x = es->x;
323 es->saved_y = es->y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400324 es->x = (output->current->width - es->width) / 2;
325 es->y = (output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400326 es->fullscreen_output = output;
327 wlsc_surface_damage(es);
328 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
329}
330
331static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400332destroy_drag(struct wl_resource *resource)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500333{
334 struct wl_drag *drag =
335 container_of(resource, struct wl_drag, resource);
336
337 wl_list_remove(&drag->drag_focus_listener.link);
338 if (drag->grab.input_device)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400339 wl_input_device_end_grab(drag->grab.input_device,
340 wlsc_compositor_get_time());
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500341
342 free(drag);
343}
344
345
346static void
347wl_drag_set_pointer_focus(struct wl_drag *drag,
348 struct wl_surface *surface, uint32_t time,
349 int32_t x, int32_t y, int32_t sx, int32_t sy)
350{
351 char **p, **end;
352
353 if (drag->drag_focus == surface)
354 return;
355
356 if (drag->drag_focus &&
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400357 (!surface ||
358 drag->drag_focus->resource.client != surface->resource.client))
359 wl_resource_post_event(&drag->drag_offer.resource,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500360 WL_DRAG_OFFER_POINTER_FOCUS,
361 time, NULL, 0, 0, 0, 0);
362
363 if (surface &&
364 (!drag->drag_focus ||
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400365 drag->drag_focus->resource.client != surface->resource.client)) {
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400366
367 drag->drag_offer.resource.client = surface->resource.client;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500368 end = drag->types.data + drag->types.size;
369 for (p = drag->types.data; p < end; p++)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400370 wl_resource_post_event(&drag->drag_offer.resource,
371 WL_DRAG_OFFER_OFFER, *p);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500372 }
373
374 if (surface) {
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400375 wl_resource_post_event(&drag->drag_offer.resource,
376 WL_DRAG_OFFER_POINTER_FOCUS,
377 time, surface,
378 x, y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500379
380 }
381
382 drag->drag_focus = surface;
383 drag->pointer_focus_time = time;
384 drag->target = NULL;
385
386 wl_list_remove(&drag->drag_focus_listener.link);
387 if (surface)
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200388 wl_list_insert(surface->resource.destroy_listener_list.prev,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500389 &drag->drag_focus_listener.link);
390}
391
392static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400393drag_offer_accept(struct wl_client *client, struct wl_resource *resource,
394 uint32_t time, const char *type)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500395{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400396 struct wl_drag_offer *offer = resource->data;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500397 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
398 char **p, **end;
399
400 /* If the client responds to drag pointer_focus or motion
401 * events after the pointer has left the surface, we just
402 * discard the accept requests. The drag source just won't
403 * get the corresponding 'target' events and eventually the
404 * next surface/root will start sending events. */
405 if (time < drag->pointer_focus_time)
406 return;
407
408 drag->target = client;
409 drag->type = NULL;
410 end = drag->types.data + drag->types.size;
411 for (p = drag->types.data; p < end; p++)
412 if (type && strcmp(*p, type) == 0)
413 drag->type = *p;
414
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400415 wl_resource_post_event(&drag->resource, WL_DRAG_TARGET, drag->type);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500416}
417
418static void
419drag_offer_receive(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400420 struct wl_resource *resource, int fd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500421{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400422 struct wl_drag_offer *offer = resource->data;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500423 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
424
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400425 wl_resource_post_event(&drag->resource, WL_DRAG_FINISH, fd);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500426 close(fd);
427}
428
429static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400430drag_offer_reject(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500431{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400432 struct wl_drag_offer *offer = resource->data;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500433 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
434
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400435 wl_resource_post_event(&drag->resource, WL_DRAG_REJECT);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500436}
437
438static const struct wl_drag_offer_interface drag_offer_interface = {
439 drag_offer_accept,
440 drag_offer_receive,
441 drag_offer_reject
442};
443
444static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400445drag_offer(struct wl_client *client,
446 struct wl_resource *resource, const char *type)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500447{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400448 struct wl_drag *drag = resource->data;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500449 char **p;
450
451 p = wl_array_add(&drag->types, sizeof *p);
452 if (p)
453 *p = strdup(type);
454 if (!p || !*p)
455 wl_client_post_no_memory(client);
456}
457
458static void
459drag_grab_motion(struct wl_grab *grab,
460 uint32_t time, int32_t x, int32_t y)
461{
462 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
463 struct wlsc_surface *es;
464 int32_t sx, sy;
465
466 es = pick_surface(grab->input_device, &sx, &sy);
467 wl_drag_set_pointer_focus(drag, &es->surface, time, x, y, sx, sy);
468 if (es)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400469 wl_resource_post_event(&drag->drag_offer.resource,
470 WL_DRAG_OFFER_MOTION,
471 time, x, y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500472}
473
474static void
475drag_grab_button(struct wl_grab *grab,
476 uint32_t time, int32_t button, int32_t state)
477{
478}
479
480static void
481drag_grab_end(struct wl_grab *grab, uint32_t time)
482{
483 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500484 struct wlsc_surface *es;
485 struct wl_input_device *device = grab->input_device;
486 int32_t sx, sy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500487
488 if (drag->target)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400489 wl_resource_post_event(&drag->drag_offer.resource,
490 WL_DRAG_OFFER_DROP);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500491
492 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500493
494 es = pick_surface(grab->input_device, &sx, &sy);
495 wl_input_device_set_pointer_focus(device,
496 &es->surface, time,
497 device->x, device->y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500498}
499
500static const struct wl_grab_interface drag_grab_interface = {
501 drag_grab_motion,
502 drag_grab_button,
503 drag_grab_end
504};
505
506static void
507drag_activate(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400508 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400509 struct wl_resource *surface_resource,
510 struct wl_resource *device_resource, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500511{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400512 struct wl_drag *drag = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400513 struct wl_surface *surface = surface_resource->data;
514 struct wl_input_device *device = device_resource->data;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500515 struct wl_display *display = wl_client_get_display (client);
516 struct wlsc_surface *target;
517 int32_t sx, sy;
518
519 if (wl_input_device_update_grab(device,
520 &drag->grab, surface, time) < 0)
521 return;
522
523 drag->grab.interface = &drag_grab_interface;
524
525 drag->source = surface;
526
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400527 drag->drag_offer.resource.object.interface = &wl_drag_offer_interface;
528 drag->drag_offer.resource.object.implementation =
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500529 (void (**)(void)) &drag_offer_interface;
530
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400531 wl_display_add_global(display, &wl_drag_offer_interface, drag, NULL);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500532
533 target = pick_surface(device, &sx, &sy);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500534 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500535 wl_drag_set_pointer_focus(drag, &target->surface, time,
536 device->x, device->y, sx, sy);
537}
538
539static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400540drag_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500541{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400542 wl_resource_destroy(resource, wlsc_compositor_get_time());
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500543}
544
545static const struct wl_drag_interface drag_interface = {
546 drag_offer,
547 drag_activate,
548 drag_destroy,
549};
550
551static void
552drag_handle_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200553 struct wl_resource *resource, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500554{
555 struct wl_drag *drag =
556 container_of(listener, struct wl_drag, drag_focus_listener);
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200557 struct wl_surface *surface = (struct wl_surface *) resource;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500558
559 if (drag->drag_focus == surface)
560 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
561}
562
563static void
564shell_create_drag(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400565 struct wl_resource *resource, uint32_t id)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500566{
567 struct wl_drag *drag;
568
569 drag = malloc(sizeof *drag);
570 if (drag == NULL) {
571 wl_client_post_no_memory(client);
572 return;
573 }
574
575 memset(drag, 0, sizeof *drag);
576 drag->resource.object.id = id;
577 drag->resource.object.interface = &wl_drag_interface;
578 drag->resource.object.implementation =
579 (void (**)(void)) &drag_interface;
580
581 drag->resource.destroy = destroy_drag;
582
583 drag->drag_focus_listener.func = drag_handle_surface_destroy;
584 wl_list_init(&drag->drag_focus_listener.link);
585
586 wl_client_add_resource(client, &drag->resource);
587}
588
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400589static void
590wlsc_selection_set_focus(struct wlsc_shell *shell,
591 struct wl_selection *selection,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500592 struct wl_surface *surface, uint32_t time)
593{
594 char **p, **end;
595
596 if (selection->selection_focus == surface)
597 return;
598
599 if (selection->selection_focus != NULL)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400600 wl_resource_post_event(&selection->selection_offer.resource,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500601 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
602 NULL);
603
604 if (surface) {
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500605
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400606 selection->selection_offer.resource.client = surface->resource.client;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500607 end = selection->types.data + selection->types.size;
608 for (p = selection->types.data; p < end; p++)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400609 wl_resource_post_event(&selection->selection_offer.resource,
610 WL_SELECTION_OFFER_OFFER, *p);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500611
612 wl_list_remove(&selection->selection_focus_listener.link);
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200613 wl_list_insert(surface->resource.destroy_listener_list.prev,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500614 &selection->selection_focus_listener.link);
615
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400616 wl_resource_post_event(&selection->selection_offer.resource,
617 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
618 selection->input_device);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500619 }
620
621 selection->selection_focus = surface;
622
623 wl_list_remove(&selection->selection_focus_listener.link);
624 if (surface)
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200625 wl_list_insert(surface->resource.destroy_listener_list.prev,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500626 &selection->selection_focus_listener.link);
627}
628
629static void
630selection_offer_receive(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400631 struct wl_resource *resource,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500632 const char *mime_type, int fd)
633{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400634 struct wl_selection_offer *offer = resource->data;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500635 struct wl_selection *selection =
636 container_of(offer, struct wl_selection, selection_offer);
637
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400638 wl_resource_post_event(&selection->resource,
639 WL_SELECTION_SEND, mime_type, fd);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500640 close(fd);
641}
642
643static const struct wl_selection_offer_interface selection_offer_interface = {
644 selection_offer_receive
645};
646
647static void
648selection_offer(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400649 struct wl_resource *resource, const char *type)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500650{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400651 struct wl_selection *selection = resource->data;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500652 char **p;
653
654 p = wl_array_add(&selection->types, sizeof *p);
655 if (p)
656 *p = strdup(type);
657 if (!p || !*p)
658 wl_client_post_no_memory(client);
659}
660
661static void
662selection_activate(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400663 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400664 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500665{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400666 struct wl_selection *selection = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400667 struct wlsc_input_device *wd = input_resource->data;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500668 struct wl_display *display = wl_client_get_display (client);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400669 struct wlsc_compositor *compositor =
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400670 (struct wlsc_compositor *) wd->input_device.compositor;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500671
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400672 selection->input_device = &wd->input_device;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500673
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400674 selection->selection_offer.resource.object.interface =
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500675 &wl_selection_offer_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400676 selection->selection_offer.resource.object.implementation =
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500677 (void (**)(void)) &selection_offer_interface;
678
Kristian Høgsbergd9551a32011-08-19 12:07:44 -0400679 wl_display_add_global(display,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400680 &wl_selection_offer_interface, selection, NULL);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500681
682 if (wd->selection) {
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400683 wl_resource_post_event(&wd->selection->resource,
684 WL_SELECTION_CANCELLED);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500685 }
686 wd->selection = selection;
687
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400688 wlsc_selection_set_focus(compositor->shell, selection,
689 wd->input_device.keyboard_focus, time);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500690}
691
692static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400693selection_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500694{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400695 wl_resource_destroy(resource, wlsc_compositor_get_time());
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500696}
697
698static const struct wl_selection_interface selection_interface = {
699 selection_offer,
700 selection_activate,
701 selection_destroy
702};
703
704static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400705destroy_selection(struct wl_resource *resource)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500706{
707 struct wl_selection *selection =
708 container_of(resource, struct wl_selection, resource);
709 struct wlsc_input_device *wd =
710 (struct wlsc_input_device *) selection->input_device;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400711 struct wlsc_compositor *compositor =
712 (struct wlsc_compositor *) wd->input_device.compositor;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500713
714 if (wd && wd->selection == selection) {
715 wd->selection = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400716 wlsc_selection_set_focus(compositor->shell,
717 selection, NULL,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400718 wlsc_compositor_get_time());
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500719 }
720
721 wl_list_remove(&selection->selection_focus_listener.link);
722 free(selection);
723}
724
725static void
726selection_handle_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200727 struct wl_resource *resource, uint32_t time)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500728{
729}
730
731static void
732shell_create_selection(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400733 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500734{
735 struct wl_selection *selection;
736
737 selection = malloc(sizeof *selection);
738 if (selection == NULL) {
739 wl_client_post_no_memory(client);
740 return;
741 }
742
743 memset(selection, 0, sizeof *selection);
744 selection->resource.object.id = id;
745 selection->resource.object.interface = &wl_selection_interface;
746 selection->resource.object.implementation =
747 (void (**)(void)) &selection_interface;
748
749 selection->client = client;
750 selection->resource.destroy = destroy_selection;
751 selection->selection_focus = NULL;
752
753 selection->selection_focus_listener.func =
754 selection_handle_surface_destroy;
755 wl_list_init(&selection->selection_focus_listener.link);
756
757 wl_client_add_resource(client, &selection->resource);
758}
759
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500760const static struct wl_shell_interface shell_interface = {
761 shell_move,
762 shell_resize,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500763 shell_create_drag,
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400764 shell_create_selection,
765 shell_set_toplevel,
766 shell_set_transient,
767 shell_set_fullscreen
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500768};
769
Kristian Høgsberg07937562011-04-12 17:25:42 -0400770static void
771move_binding(struct wl_input_device *device, uint32_t time,
772 uint32_t key, uint32_t button, uint32_t state, void *data)
773{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400774 struct wlsc_surface *surface =
775 (struct wlsc_surface *) device->pointer_focus;
776
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400777 if (surface == NULL)
778 return;
779
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400780 wlsc_surface_move(surface, (struct wlsc_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400781}
782
783static void
784resize_binding(struct wl_input_device *device, uint32_t time,
785 uint32_t key, uint32_t button, uint32_t state, void *data)
786{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400787 struct wlsc_surface *surface =
788 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400789 struct wl_resource *resource;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400790 uint32_t edges = 0;
791 int32_t x, y;
792
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400793 if (surface == NULL)
794 return;
795
Kristian Høgsberg07937562011-04-12 17:25:42 -0400796 x = device->grab_x - surface->x;
797 y = device->grab_y - surface->y;
798
799 if (x < surface->width / 3)
800 edges |= WL_SHELL_RESIZE_LEFT;
801 else if (x < 2 * surface->width / 3)
802 edges |= 0;
803 else
804 edges |= WL_SHELL_RESIZE_RIGHT;
805
806 if (y < surface->height / 3)
807 edges |= WL_SHELL_RESIZE_TOP;
808 else if (y < 2 * surface->height / 3)
809 edges |= 0;
810 else
811 edges |= WL_SHELL_RESIZE_BOTTOM;
812
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400813 resource = /* Find shell resource for surface client */ 0;
814
815 /* ... or use wl_shell_surface */
816
817 wlsc_surface_resize(surface, (struct wlsc_input_device *) device,
818 time, edges, resource);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400819}
820
821static void
822lock(struct wlsc_shell *shell)
823{
824}
825
826static void
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400827attach(struct wlsc_shell *shell, struct wlsc_surface *es)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400828{
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400829 if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN) {
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400830 es->x = (es->fullscreen_output->current->width - es->width) / 2;
831 es->y = (es->fullscreen_output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400832 }
Kristian Høgsberg07937562011-04-12 17:25:42 -0400833}
834
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400835static void
836bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
837{
838 struct wl_shell *shell = data;
839
840 wl_client_add_object(client, &wl_shell_interface,
841 &shell_interface, id, shell);
842}
843
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400844int
845shell_init(struct wlsc_compositor *ec);
846
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400847WL_EXPORT int
848shell_init(struct wlsc_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500849{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400850 struct wl_shell *shell;
851
852 shell = malloc(sizeof *shell);
853 if (shell == NULL)
854 return -1;
855
856 shell->shell.lock = lock;
857 shell->shell.attach = attach;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400858 shell->shell.set_selection_focus = wlsc_selection_set_focus;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500859
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400860 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
861 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500862 return -1;
863
Kristian Høgsberg07937562011-04-12 17:25:42 -0400864 wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400865 move_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400866 wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400867 resize_binding, shell);
868
869 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400870
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500871 return 0;
872}