blob: ab6218b7aca8f67978701812cf51059fcd47e0b0 [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 {
32 struct wl_object object;
33 struct wlsc_shell shell;
34};
35
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036struct wlsc_move_grab {
37 struct wl_grab grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050038 struct wlsc_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050039 int32_t dx, dy;
40};
41
42static void
43move_grab_motion(struct wl_grab *grab,
44 uint32_t time, int32_t x, int32_t y)
45{
46 struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050047 struct wlsc_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050048
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -050049 wlsc_surface_damage(es);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050050 es->x = x + move->dx;
51 es->y = y + move->dy;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +010052 wlsc_surface_assign_output(es);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -050053 wlsc_surface_damage(es);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050054}
55
56static void
57move_grab_button(struct wl_grab *grab,
58 uint32_t time, int32_t button, int32_t state)
59{
60}
61
62static void
63move_grab_end(struct wl_grab *grab, uint32_t time)
64{
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050065 struct wlsc_surface *es;
66 struct wl_input_device *device = grab->input_device;
67 int32_t sx, sy;
68
69 es = pick_surface(grab->input_device, &sx, &sy);
70 wl_input_device_set_pointer_focus(device,
71 &es->surface, time,
72 device->x, device->y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050073 free(grab);
74}
75
76static const struct wl_grab_interface move_grab_interface = {
77 move_grab_motion,
78 move_grab_button,
79 move_grab_end
80};
81
Kristian Høgsberg07937562011-04-12 17:25:42 -040082static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050083shell_move(struct wl_client *client, struct wl_shell *shell,
84 struct wl_surface *surface,
85 struct wl_input_device *device, uint32_t time)
86{
87 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
88 struct wlsc_surface *es = (struct wlsc_surface *) surface;
89 struct wlsc_move_grab *move;
90
Kristian Høgsberg0ce24572011-01-28 15:18:33 -050091 /* FIXME: Reject if fullscreen */
92
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050093 move = malloc(sizeof *move);
94 if (!move) {
95 wl_client_post_no_memory(client);
96 return;
97 }
98
99 move->grab.interface = &move_grab_interface;
100 move->dx = es->x - wd->input_device.grab_x;
101 move->dy = es->y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500102 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500103
104 if (wl_input_device_update_grab(&wd->input_device,
105 &move->grab, surface, time) < 0)
106 return;
107
108 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500109 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500110}
111
112struct wlsc_resize_grab {
113 struct wl_grab grab;
114 uint32_t edges;
115 int32_t dx, dy, width, height;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500116 struct wlsc_surface *surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400117 struct wl_shell *shell;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500118};
119
120static void
121resize_grab_motion(struct wl_grab *grab,
122 uint32_t time, int32_t x, int32_t y)
123{
124 struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab;
125 struct wl_input_device *device = grab->input_device;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500126 struct wl_surface *surface = &resize->surface->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500127 int32_t width, height;
128
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500129 if (resize->edges & WL_SHELL_RESIZE_LEFT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500130 width = device->grab_x - x + resize->width;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500131 } else if (resize->edges & WL_SHELL_RESIZE_RIGHT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500132 width = x - device->grab_x + resize->width;
133 } else {
134 width = resize->width;
135 }
136
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500137 if (resize->edges & WL_SHELL_RESIZE_TOP) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500138 height = device->grab_y - y + resize->height;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500139 } else if (resize->edges & WL_SHELL_RESIZE_BOTTOM) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500140 height = y - device->grab_y + resize->height;
141 } else {
142 height = resize->height;
143 }
144
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400145 wl_client_post_event(surface->client, &resize->shell->object,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500146 WL_SHELL_CONFIGURE, time, resize->edges,
147 surface, width, height);
148}
149
150static void
151resize_grab_button(struct wl_grab *grab,
152 uint32_t time, int32_t button, int32_t state)
153{
154}
155
156static void
157resize_grab_end(struct wl_grab *grab, uint32_t time)
158{
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500159 struct wlsc_surface *es;
160 struct wl_input_device *device = grab->input_device;
161 int32_t sx, sy;
162
163 es = pick_surface(grab->input_device, &sx, &sy);
164 wl_input_device_set_pointer_focus(device,
165 &es->surface, time,
166 device->x, device->y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500167 free(grab);
168}
169
170static const struct wl_grab_interface resize_grab_interface = {
171 resize_grab_motion,
172 resize_grab_button,
173 resize_grab_end
174};
175
Kristian Høgsberg07937562011-04-12 17:25:42 -0400176static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500177shell_resize(struct wl_client *client, struct wl_shell *shell,
178 struct wl_surface *surface,
179 struct wl_input_device *device, uint32_t time, uint32_t edges)
180{
181 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
182 struct wlsc_resize_grab *resize;
183 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
184 struct wlsc_surface *es = (struct wlsc_surface *) surface;
185
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500186 /* FIXME: Reject if fullscreen */
187
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500188 resize = malloc(sizeof *resize);
189 if (!resize) {
190 wl_client_post_no_memory(client);
191 return;
192 }
193
194 resize->grab.interface = &resize_grab_interface;
195 resize->edges = edges;
196 resize->dx = es->x - wd->input_device.grab_x;
197 resize->dy = es->y - wd->input_device.grab_y;
198 resize->width = es->width;
199 resize->height = es->height;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500200 resize->surface = es;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400201 resize->shell = shell;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500202
203 if (edges == 0 || edges > 15 ||
204 (edges & 3) == 3 || (edges & 12) == 12)
205 return;
206
207 switch (edges) {
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500208 case WL_SHELL_RESIZE_TOP:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500209 pointer = WLSC_POINTER_TOP;
210 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500211 case WL_SHELL_RESIZE_BOTTOM:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500212 pointer = WLSC_POINTER_BOTTOM;
213 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500214 case WL_SHELL_RESIZE_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500215 pointer = WLSC_POINTER_LEFT;
216 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500217 case WL_SHELL_RESIZE_TOP_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500218 pointer = WLSC_POINTER_TOP_LEFT;
219 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500220 case WL_SHELL_RESIZE_BOTTOM_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500221 pointer = WLSC_POINTER_BOTTOM_LEFT;
222 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500223 case WL_SHELL_RESIZE_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500224 pointer = WLSC_POINTER_RIGHT;
225 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500226 case WL_SHELL_RESIZE_TOP_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500227 pointer = WLSC_POINTER_TOP_RIGHT;
228 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500229 case WL_SHELL_RESIZE_BOTTOM_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500230 pointer = WLSC_POINTER_BOTTOM_RIGHT;
231 break;
232 }
233
234 if (wl_input_device_update_grab(&wd->input_device,
235 &resize->grab, surface, time) < 0)
236 return;
237
238 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500239 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500240}
241
242static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500243destroy_drag(struct wl_resource *resource, struct wl_client *client)
244{
245 struct wl_drag *drag =
246 container_of(resource, struct wl_drag, resource);
247
248 wl_list_remove(&drag->drag_focus_listener.link);
249 if (drag->grab.input_device)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400250 wl_input_device_end_grab(drag->grab.input_device,
251 wlsc_compositor_get_time());
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500252
253 free(drag);
254}
255
256
257static void
258wl_drag_set_pointer_focus(struct wl_drag *drag,
259 struct wl_surface *surface, uint32_t time,
260 int32_t x, int32_t y, int32_t sx, int32_t sy)
261{
262 char **p, **end;
263
264 if (drag->drag_focus == surface)
265 return;
266
267 if (drag->drag_focus &&
268 (!surface || drag->drag_focus->client != surface->client))
269 wl_client_post_event(drag->drag_focus->client,
270 &drag->drag_offer.object,
271 WL_DRAG_OFFER_POINTER_FOCUS,
272 time, NULL, 0, 0, 0, 0);
273
274 if (surface &&
275 (!drag->drag_focus ||
276 drag->drag_focus->client != surface->client)) {
277 wl_client_post_global(surface->client,
278 &drag->drag_offer.object);
279
280 end = drag->types.data + drag->types.size;
281 for (p = drag->types.data; p < end; p++)
282 wl_client_post_event(surface->client,
283 &drag->drag_offer.object,
284 WL_DRAG_OFFER_OFFER, *p);
285 }
286
287 if (surface) {
288 wl_client_post_event(surface->client,
289 &drag->drag_offer.object,
290 WL_DRAG_OFFER_POINTER_FOCUS,
291 time, surface,
292 x, y, sx, sy);
293
294 }
295
296 drag->drag_focus = surface;
297 drag->pointer_focus_time = time;
298 drag->target = NULL;
299
300 wl_list_remove(&drag->drag_focus_listener.link);
301 if (surface)
302 wl_list_insert(surface->destroy_listener_list.prev,
303 &drag->drag_focus_listener.link);
304}
305
306static void
307drag_offer_accept(struct wl_client *client,
308 struct wl_drag_offer *offer, uint32_t time, const char *type)
309{
310 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
311 char **p, **end;
312
313 /* If the client responds to drag pointer_focus or motion
314 * events after the pointer has left the surface, we just
315 * discard the accept requests. The drag source just won't
316 * get the corresponding 'target' events and eventually the
317 * next surface/root will start sending events. */
318 if (time < drag->pointer_focus_time)
319 return;
320
321 drag->target = client;
322 drag->type = NULL;
323 end = drag->types.data + drag->types.size;
324 for (p = drag->types.data; p < end; p++)
325 if (type && strcmp(*p, type) == 0)
326 drag->type = *p;
327
328 wl_client_post_event(drag->source->client, &drag->resource.object,
329 WL_DRAG_TARGET, drag->type);
330}
331
332static void
333drag_offer_receive(struct wl_client *client,
334 struct wl_drag_offer *offer, int fd)
335{
336 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
337
338 wl_client_post_event(drag->source->client, &drag->resource.object,
339 WL_DRAG_FINISH, fd);
340 close(fd);
341}
342
343static void
344drag_offer_reject(struct wl_client *client, struct wl_drag_offer *offer)
345{
346 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
347
348 wl_client_post_event(drag->source->client, &drag->resource.object,
349 WL_DRAG_REJECT);
350}
351
352static const struct wl_drag_offer_interface drag_offer_interface = {
353 drag_offer_accept,
354 drag_offer_receive,
355 drag_offer_reject
356};
357
358static void
359drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
360{
361 char **p;
362
363 p = wl_array_add(&drag->types, sizeof *p);
364 if (p)
365 *p = strdup(type);
366 if (!p || !*p)
367 wl_client_post_no_memory(client);
368}
369
370static void
371drag_grab_motion(struct wl_grab *grab,
372 uint32_t time, int32_t x, int32_t y)
373{
374 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
375 struct wlsc_surface *es;
376 int32_t sx, sy;
377
378 es = pick_surface(grab->input_device, &sx, &sy);
379 wl_drag_set_pointer_focus(drag, &es->surface, time, x, y, sx, sy);
380 if (es)
381 wl_client_post_event(es->surface.client,
382 &drag->drag_offer.object,
383 WL_DRAG_OFFER_MOTION,
384 time, x, y, sx, sy);
385}
386
387static void
388drag_grab_button(struct wl_grab *grab,
389 uint32_t time, int32_t button, int32_t state)
390{
391}
392
393static void
394drag_grab_end(struct wl_grab *grab, uint32_t time)
395{
396 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500397 struct wlsc_surface *es;
398 struct wl_input_device *device = grab->input_device;
399 int32_t sx, sy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500400
401 if (drag->target)
402 wl_client_post_event(drag->target,
403 &drag->drag_offer.object,
404 WL_DRAG_OFFER_DROP);
405
406 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500407
408 es = pick_surface(grab->input_device, &sx, &sy);
409 wl_input_device_set_pointer_focus(device,
410 &es->surface, time,
411 device->x, device->y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500412}
413
414static const struct wl_grab_interface drag_grab_interface = {
415 drag_grab_motion,
416 drag_grab_button,
417 drag_grab_end
418};
419
420static void
421drag_activate(struct wl_client *client,
422 struct wl_drag *drag,
423 struct wl_surface *surface,
424 struct wl_input_device *device, uint32_t time)
425{
426 struct wl_display *display = wl_client_get_display (client);
427 struct wlsc_surface *target;
428 int32_t sx, sy;
429
430 if (wl_input_device_update_grab(device,
431 &drag->grab, surface, time) < 0)
432 return;
433
434 drag->grab.interface = &drag_grab_interface;
435
436 drag->source = surface;
437
438 drag->drag_offer.object.interface = &wl_drag_offer_interface;
439 drag->drag_offer.object.implementation =
440 (void (**)(void)) &drag_offer_interface;
441
442 wl_display_add_object(display, &drag->drag_offer.object);
443
444 target = pick_surface(device, &sx, &sy);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500445 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500446 wl_drag_set_pointer_focus(drag, &target->surface, time,
447 device->x, device->y, sx, sy);
448}
449
450static void
451drag_destroy(struct wl_client *client, struct wl_drag *drag)
452{
453 wl_resource_destroy(&drag->resource, client);
454}
455
456static const struct wl_drag_interface drag_interface = {
457 drag_offer,
458 drag_activate,
459 drag_destroy,
460};
461
462static void
463drag_handle_surface_destroy(struct wl_listener *listener,
464 struct wl_surface *surface, uint32_t time)
465{
466 struct wl_drag *drag =
467 container_of(listener, struct wl_drag, drag_focus_listener);
468
469 if (drag->drag_focus == surface)
470 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
471}
472
473static void
474shell_create_drag(struct wl_client *client,
475 struct wl_shell *shell, uint32_t id)
476{
477 struct wl_drag *drag;
478
479 drag = malloc(sizeof *drag);
480 if (drag == NULL) {
481 wl_client_post_no_memory(client);
482 return;
483 }
484
485 memset(drag, 0, sizeof *drag);
486 drag->resource.object.id = id;
487 drag->resource.object.interface = &wl_drag_interface;
488 drag->resource.object.implementation =
489 (void (**)(void)) &drag_interface;
490
491 drag->resource.destroy = destroy_drag;
492
493 drag->drag_focus_listener.func = drag_handle_surface_destroy;
494 wl_list_init(&drag->drag_focus_listener.link);
495
496 wl_client_add_resource(client, &drag->resource);
497}
498
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400499static void
500wlsc_selection_set_focus(struct wlsc_shell *shell,
501 struct wl_selection *selection,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500502 struct wl_surface *surface, uint32_t time)
503{
504 char **p, **end;
505
506 if (selection->selection_focus == surface)
507 return;
508
509 if (selection->selection_focus != NULL)
510 wl_client_post_event(selection->selection_focus->client,
511 &selection->selection_offer.object,
512 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
513 NULL);
514
515 if (surface) {
516 wl_client_post_global(surface->client,
517 &selection->selection_offer.object);
518
519 end = selection->types.data + selection->types.size;
520 for (p = selection->types.data; p < end; p++)
521 wl_client_post_event(surface->client,
522 &selection->selection_offer.object,
523 WL_SELECTION_OFFER_OFFER, *p);
524
525 wl_list_remove(&selection->selection_focus_listener.link);
526 wl_list_insert(surface->destroy_listener_list.prev,
527 &selection->selection_focus_listener.link);
528
529 wl_client_post_event(surface->client,
530 &selection->selection_offer.object,
531 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
532 selection->input_device);
533 }
534
535 selection->selection_focus = surface;
536
537 wl_list_remove(&selection->selection_focus_listener.link);
538 if (surface)
539 wl_list_insert(surface->destroy_listener_list.prev,
540 &selection->selection_focus_listener.link);
541}
542
543static void
544selection_offer_receive(struct wl_client *client,
545 struct wl_selection_offer *offer,
546 const char *mime_type, int fd)
547{
548 struct wl_selection *selection =
549 container_of(offer, struct wl_selection, selection_offer);
550
551 wl_client_post_event(selection->client,
552 &selection->resource.object,
553 WL_SELECTION_SEND, mime_type, fd);
554 close(fd);
555}
556
557static const struct wl_selection_offer_interface selection_offer_interface = {
558 selection_offer_receive
559};
560
561static void
562selection_offer(struct wl_client *client,
563 struct wl_selection *selection, const char *type)
564{
565 char **p;
566
567 p = wl_array_add(&selection->types, sizeof *p);
568 if (p)
569 *p = strdup(type);
570 if (!p || !*p)
571 wl_client_post_no_memory(client);
572}
573
574static void
575selection_activate(struct wl_client *client,
576 struct wl_selection *selection,
577 struct wl_input_device *device, uint32_t time)
578{
579 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
580 struct wl_display *display = wl_client_get_display (client);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400581 struct wlsc_compositor *compositor =
582 (struct wlsc_compositor *) device->compositor;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500583
584 selection->input_device = device;
585
586 selection->selection_offer.object.interface =
587 &wl_selection_offer_interface;
588 selection->selection_offer.object.implementation =
589 (void (**)(void)) &selection_offer_interface;
590
591 wl_display_add_object(display, &selection->selection_offer.object);
592
593 if (wd->selection) {
594 wl_client_post_event(wd->selection->client,
595 &wd->selection->resource.object,
596 WL_SELECTION_CANCELLED);
597 }
598 wd->selection = selection;
599
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400600 wlsc_selection_set_focus(compositor->shell,
601 selection, device->keyboard_focus, time);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500602}
603
604static void
605selection_destroy(struct wl_client *client, struct wl_selection *selection)
606{
607 wl_resource_destroy(&selection->resource, client);
608}
609
610static const struct wl_selection_interface selection_interface = {
611 selection_offer,
612 selection_activate,
613 selection_destroy
614};
615
616static void
617destroy_selection(struct wl_resource *resource, struct wl_client *client)
618{
619 struct wl_selection *selection =
620 container_of(resource, struct wl_selection, resource);
621 struct wlsc_input_device *wd =
622 (struct wlsc_input_device *) selection->input_device;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400623 struct wlsc_compositor *compositor =
624 (struct wlsc_compositor *) wd->input_device.compositor;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500625
626 if (wd && wd->selection == selection) {
627 wd->selection = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400628 wlsc_selection_set_focus(compositor->shell,
629 selection, NULL,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400630 wlsc_compositor_get_time());
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500631 }
632
633 wl_list_remove(&selection->selection_focus_listener.link);
634 free(selection);
635}
636
637static void
638selection_handle_surface_destroy(struct wl_listener *listener,
639 struct wl_surface *surface, uint32_t time)
640{
641}
642
643static void
644shell_create_selection(struct wl_client *client,
645 struct wl_shell *shell, uint32_t id)
646{
647 struct wl_selection *selection;
648
649 selection = malloc(sizeof *selection);
650 if (selection == NULL) {
651 wl_client_post_no_memory(client);
652 return;
653 }
654
655 memset(selection, 0, sizeof *selection);
656 selection->resource.object.id = id;
657 selection->resource.object.interface = &wl_selection_interface;
658 selection->resource.object.implementation =
659 (void (**)(void)) &selection_interface;
660
661 selection->client = client;
662 selection->resource.destroy = destroy_selection;
663 selection->selection_focus = NULL;
664
665 selection->selection_focus_listener.func =
666 selection_handle_surface_destroy;
667 wl_list_init(&selection->selection_focus_listener.link);
668
669 wl_client_add_resource(client, &selection->resource);
670}
671
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500672const static struct wl_shell_interface shell_interface = {
673 shell_move,
674 shell_resize,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500675 shell_create_drag,
676 shell_create_selection
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500677};
678
Kristian Høgsberg07937562011-04-12 17:25:42 -0400679static void
680move_binding(struct wl_input_device *device, uint32_t time,
681 uint32_t key, uint32_t button, uint32_t state, void *data)
682{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400683 struct wl_shell *shell = data;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400684 struct wlsc_surface *surface =
685 (struct wlsc_surface *) device->pointer_focus;
686
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400687 if (surface == NULL)
688 return;
689
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400690 shell_move(NULL, shell, &surface->surface, device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400691}
692
693static void
694resize_binding(struct wl_input_device *device, uint32_t time,
695 uint32_t key, uint32_t button, uint32_t state, void *data)
696{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400697 struct wl_shell *shell = data;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400698 struct wlsc_surface *surface =
699 (struct wlsc_surface *) device->pointer_focus;
700 uint32_t edges = 0;
701 int32_t x, y;
702
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400703 if (surface == NULL)
704 return;
705
Kristian Høgsberg07937562011-04-12 17:25:42 -0400706 x = device->grab_x - surface->x;
707 y = device->grab_y - surface->y;
708
709 if (x < surface->width / 3)
710 edges |= WL_SHELL_RESIZE_LEFT;
711 else if (x < 2 * surface->width / 3)
712 edges |= 0;
713 else
714 edges |= WL_SHELL_RESIZE_RIGHT;
715
716 if (y < surface->height / 3)
717 edges |= WL_SHELL_RESIZE_TOP;
718 else if (y < 2 * surface->height / 3)
719 edges |= 0;
720 else
721 edges |= WL_SHELL_RESIZE_BOTTOM;
722
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400723 shell_resize(NULL, shell, &surface->surface, device, time, edges);
724}
725
726static void
727lock(struct wlsc_shell *shell)
728{
729}
730
731static void
732attach(struct wlsc_shell *shell, struct wlsc_surface *surface)
733{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400734}
735
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400736int
737shell_init(struct wlsc_compositor *ec);
738
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400739WL_EXPORT int
740shell_init(struct wlsc_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500741{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400742 struct wl_shell *shell;
743
744 shell = malloc(sizeof *shell);
745 if (shell == NULL)
746 return -1;
747
748 shell->shell.lock = lock;
749 shell->shell.attach = attach;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400750 shell->shell.set_selection_focus = wlsc_selection_set_focus;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500751
752 shell->object.interface = &wl_shell_interface;
753 shell->object.implementation = (void (**)(void)) &shell_interface;
754 wl_display_add_object(ec->wl_display, &shell->object);
755 if (wl_display_add_global(ec->wl_display, &shell->object, NULL))
756 return -1;
757
Kristian Høgsberg07937562011-04-12 17:25:42 -0400758 wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400759 move_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400760 wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400761 resize_binding, shell);
762
763 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400764
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500765 return 0;
766}